暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

.Net Core平台下读取轻量加密sqlite数据库的解决方案

DotNet开发跳槽 2021-05-14
3618

一、相关工具

1、SQLiteStudio

     地址:https://sqlitestudio.pl/index.rvt

       也可自行百度下载破解的汉化版本。

2、sqlcipher-shell(也叫sqlcipher-windows版本

     下载地址:https://github.com/CovenantEyes/sqlcipher-windows/releases

二、给现有数据库加密。

       使用sqlcipher-shell给现有数据库进行加密。

       以下内容摘抄自:https://blog.csdn.net/wengpanfeng/article/details/78406172

       在CMD窗口下,并定位到sqlcipher-shell的bin文件夹下。

        sqlcipher-shell64.exe origin.db

        说明:指定源数据库并打开。

      ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'thisiskey';

       说明:在当前目录下生成一个以‘thisiskey’为密码的encrypted.db数据库文件。

     SELECT sqlcipher_export('encrypted');

       说明:将源数据库数据导出到这个加密的数据库。

     DETACH DATABASE encrypted;

     说明:断开加密数据库的连接。

三、其他操作如解密、打开加密数据库等

   内容摘抄自:https://blog.csdn.net/jiyafeng/article/details/89634681

    1、创建加密数据库

$ sqlcipher encrypted.db
SQLCipher version 3.8.4.3 2014-04-03 16:53:12
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite> PRAGMA key = ‘thisiskey’;
sqlite> create table encrypted (id integer, name text);
sqlite> .schema
CREATE TABLE encrypted (id integer, name text);
sqlite> .q

2、打开加密数据库
$ sqlcipher encrypted.db
SQLCipher version 3.8.4.3 2014-04-03 16:53:12
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite> PRAGMA key = ‘thisiskey’;
sqlite> .schema
CREATE TABLE encrypted (id integer, name text);

3、修改数据库密码

sqlite> PRAGMA rekey = ‘newkey’;

4、加密已有的数据库
$ sqlcipher banklist.sqlite3
SQLCipher version 3.8.4.3 2014-04-03 16:53:12
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite> ATTACH DATABASE ‘encrypted.db’ AS encrypted KEY ‘thisiskey’;
sqlite> SELECT sqlcipher_export(‘encrypted’);
sqlite> DETACH DATABASE encrypted;

5、解密数据库(生成无密码的数据库:plaintext.db)
$ sqlcipher-shell32 encrypted.db

sqlite> PRAGMA key = ‘thisiskey’;
sqlite> ATTACH DATABASE ‘plaintext.db’ AS plaintext KEY ‘’;
sqlite> SELECT sqlcipher_export(‘plaintext’);
sqlite> DETACH DATABASE plaintext;

以上命令是在linux下完成,在windows中,也是一样用的,只不过把脚本exe文件名,要输入完全。

四、使用.net core 进行读取加密数据库的操作。

 1、 使用Microsoft.Data.Sqlite.dll 类库进行读取基于sqlcipher加密过的数据库。


 


        关键代码         

using System;
using System.Data;
using Microsoft.Data.Sqlite;

string path=@"C:\Users\Administrator\Desktop\SQLCipher.db3";
string key="123456";//密码
SqliteConnection client=new SqliteConnection($"Data Source={path}");
client.Open();
SqliteCommand command = client.CreateCommand();
command.CommandText = "PRAGMA key = " + key;
command.ExecuteNonQuery();
command.CommandText = "select name from user";
DataTable dt = new DataTable();
SqliteDataAdapter adapter = new SqliteDataAdapter(command);
adapter.Fill(dt);
var result = dt.Rows[0][0];
client.Close();

    也可以使用基于Microsoft.Data.Sqlite.dll类库而制的第三方orm框架SqlSugar 这样可以省事儿,只需要加一句话,就可以完成。

using SqlSugar;

namespace xxxxx.xxxxx
{
public class xxDbContext
{
public SqlSugarClient Client { get; set; }
public xxDbContext(string connectionString, DbType DBType)
{
Client = new SqlSugarClient(new ConnectionConfig()
{
DbType = DBType,
ConnectionString = connectionString,
IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
InitKeyType = InitKeyType.Attribute 从实体特性中读取主键自增列信息
}); ;
Client.Ado.ExecuteCommand($"PRAGMA key =" + LsDbContext.KEY);
}
public const string KEY = "xZt8CVCpm1";
}
}


    2、使用SQLiteStudio工具中,基于system.data.sqlie为加密类型生成的数据库文件。

          此种类别可以使用不能和sqlcipher相关的类库通用。 




using System;
using System.Data;
using System.Data.SQLite;

SQLiteConnection clinet = new SQLiteConnection($"Data Source={path3}");
string key = "123456";
clinet.Open();
SQLiteCommand command = clinet.CreateCommand();
command.CommandText = "PRAGMA key = " + key;
command.ExecuteNonQuery();
command.CommandText = "select name from user";
DataTable dt = new DataTable();
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
adapter.Fill(dt);
var result = dt.Rows[0][0];
clinet.Close();

 第三方ORM Freesql 类库跟上面是一样的。 

上代码 

IFreeSql db = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.Sqlite, connstring).UseAutoSyncStructure(true).Build();
db.Ado.ExecuteNonQuery($"PRAGMA key =" + key);
var list = db.Select<User>().ToList();

foreach (var item in list)
{
Console.WriteLine(item.ToUserString());
}

db.Insert<User>(new User() { Account = "adsasdfsadffasdf@153.com", Name = "sdfsdf" }).ExecuteIdentity();

出处:http://jinlingxiaozhou.com/Blog/41413529471.html

支持小微:

腾讯云 搞活动了?玩服务器的可以搞搞。  就这几天时间。

轻量 1C2G 50GB SSD盘 255元/3年

链接:https://curl.qcloud.com/qINmPBX9


版权申明:本文来源于网友收集或网友提供,如果有侵权,请转告版主或者留言,本公众号立即删除。

右下角,您点一下在看图片

小微工资涨1毛

商务合作QQ:185601686


文章转载自DotNet开发跳槽,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论