Sqlite資料庫的加密
阿新 • • 發佈:2019-02-06
最近在做一個winform的程式,考慮用Sqlite的資料庫,小巧而實用,比Access強多了,不過需要加密,不過free版本沒有實現加密,有一些c++的實現:比如:http://www.sqlite.com.cn/MySqlite/3/253.Html 和http://www.cppblog.com/niewenlong/archive/2007/06/01/25261.html
。不過,鑑於我對c++不是很精通,於是就採用了ADO.NET 2.0 SQLite Data Provider 這樣可以直接利用它來建立一個加密的sqlite資料庫。
有關c#程式碼如下:
1、建立空的sqlite資料庫。
//資料庫名的字尾你可以直接指定,甚至沒有後綴都可以
//方法 一:建立一個空sqlite資料庫,用IO的方式
FileStream fs = File.Create("c:\\test.db");
//方法二:用SQLiteConnection
SQLiteConnection.CreateFile("c:\\test.db");
建立的資料庫是個0位元組的檔案。
2、建立加密的空sqlite資料庫
//建立一個密碼為password的空的sqlite資料庫
SQLiteConnection.CreateFile("c:\\test2.db");
SQLiteConnection cnn
= new SQLiteConnection("Data Source=c:\\test2.db");
SQLiteConnection cnn
= new SQLiteConnection("Data Source=D:\\test2.db");
cnn.Open();
cnn.ChangePassword(
"password");
3、給未加密的資料庫加密
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db");
cnn.Open();
cnn.ChangePassword(
"password");
4、開啟加密sqlite資料庫
//方法一
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test2.db");
cnn.SetPassword("password");
cnn.Open();
//方法二
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.DataSource = @"c:\test.db";
builder.Password = @"password";
SQLiteConnection cnn = new SQLiteConnection(builder.ConnectionString);
cnn .Open();
注:
A、因為加密的函式 是利用windows api,故加密後的資料庫只能適用在windows平臺,加密的方式是整體檔案加密。
B、加密的演算法是RC4,如果你想採用別的加密演算法來加密,請參考ADO.NET 2.0 SQLite Data Provider 的原始碼來修改。
c、相關sqlite資料庫操作類似ADO.NET 2.0。詳見ADO.NET 2.0 SQLite Data Provider的幫助文件。
c、ADO.NET 2.0 SQLite Data Provider 版本為:1.0.53.0 ,SQLite版本 : 3.6.0。
d、開發環境為vs2008。
ADO.NET 2.0 SQLite Data Provider的下載地址: http://sourceforge.net/project/s ... p;package_id=145568
有關sqlite的中文介紹,你可以看看這裡:http://www.cnblogs.com/shanyou/archive/2007/01/08/615245.html
有關c#程式碼如下:
1、建立空的sqlite資料庫。
//資料庫名的字尾你可以直接指定,甚至沒有後綴都可以
//方法
FileStream fs = File.Create("c:\\test.db");
//方法二:用SQLiteConnection
SQLiteConnection.CreateFile("c:\\test.db");
建立的資料庫是個0位元組的檔案。
2、建立加密的空sqlite資料庫
//建立一個密碼為password的空的sqlite資料庫
SQLiteConnection.CreateFile("c:\\test2.db");
SQLiteConnection cnn
= new SQLiteConnection("Data Source=c:\\test2.db");
SQLiteConnection cnn
= new SQLiteConnection("Data Source=D:\\test2.db");
cnn.Open();
cnn.ChangePassword(
"password");
3、給未加密的資料庫加密
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db");
cnn.Open();
cnn.ChangePassword(
"password");
4、開啟加密sqlite資料庫
//方法一
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test2.db");
cnn.SetPassword("password");
cnn.Open();
//方法二
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.DataSource = @"c:\test.db";
builder.Password = @"password";
SQLiteConnection cnn = new SQLiteConnection(builder.ConnectionString);
cnn .Open();
注:
A、因為加密的函式
B、加密的演算法是RC4,如果你想採用別的加密演算法來加密,請參考ADO.NET 2.0 SQLite Data Provider 的原始碼來修改。
c、相關sqlite資料庫操作類似ADO.NET 2.0。詳見ADO.NET 2.0 SQLite Data Provider的幫助文件。
c、ADO.NET 2.0 SQLite Data Provider 版本為:1.0.53.0 ,SQLite版本 : 3.6.0。
d、開發環境為vs2008。
ADO.NET 2.0 SQLite Data Provider的下載地址:
有關sqlite的中文介紹,你可以看看這裡:http://www.cnblogs.com/shanyou/archive/2007/01/08/615245.html