用 C# 加密 SQLite 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1259561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:20:30  来源:igfitidea点击:

Encrypt SQLite database in C#

c#.netfilesqliteencryption

提问by dbkk

What is the best approach to encrypting a SQLite database file in .Net/C#? I'm using sqlite-dotnet2wrapper.

在 .Net/C# 中加密 SQLite 数据库文件的最佳方法是什么?我正在使用sqlite-dotnet2包装器。

There are tools like SQLite Encryption Extensionand SQLite Crypt, but both are non-free, while my project is under GPL.

有像SQLite Encryption ExtensionSQLite Crypt这样的工具,但两者都是非免费的,而我的项目是在 GPL 下的。

The naive approach I thought of using was to let SQLite handle a temporary file, then to encrypt it on program exit, and overwrite (zero-out) the original. The obvious drawback is that if program crashes (and while it is running), the plain text DB is accessible.

我想到使用的幼稚方法是让 SQLite 处理一个临时文件,然后在程序退出时对其进行加密,并覆盖(清零)原始文件。明显的缺点是如果程序崩溃(并且在它运行时),纯文本数据库是可访问的。

Is there a better way to approach this? Can I pass an encrypted stream to the wrapper (instead of using SQLiteConnection.CreateFile) ?

有没有更好的方法来解决这个问题?我可以将加密流传递给包装器(而不是使用 SQLiteConnection.CreateFile)吗?

[edit] Maybe I am overthinking this. Is is sufficient to use Password option in the connection string? Would the file be encrypted properly in that case (or is it some weaker protection)?

[编辑] 也许我想多了。在连接字符串中使用密码选项就足够了吗?在这种情况下,文件是否会被正确加密(或者是某种较弱的保护)?

采纳答案by Ed Power

I recommend using the System.Data.Sqlitewrapper, which includes encryption. It works great, it's easy to use, and it's a complete ADO.Net implementation. You can get the wrapper from https://system.data.sqlite.org, and the developer describes how to use the encryption on this forum at: https://web.archive.org/web/20100207030625/http://sqlite.phxsoftware.com/forums/t/130.aspx. Hint - you just set the password property. He also describes how he does the encryption using the Microsoft Crypto API elsewhere in the forum.

我建议使用System.Data.Sqlite包装器,其中包括加密。它运行良好,易于使用,并且是一个完整的 ADO.Net 实现。您可以从https://system.data.sqlite.org获取包装器,开发人员在此论坛上描述了如何使用加密:https: //web.archive.org/web/20100207030625/http: // sqlite.phxsoftware.com/forums/t/130.aspx。提示 - 您只需设置密码属性。他还在论坛的其他地方描述了他如何使用 Microsoft Crypto API 进行加密。

回答by Yakeen

I would try http://code.google.com/p/csharp-sqlite/, it's rewrite of SQLite 3.6.16 in C#, under MIT License. I suppose it will be easy to tweak it.

我会尝试http://code.google.com/p/csharp-sqlite/,它是在 MIT 许可下用 C# 重写 SQLite 3.6.16。我想调整它会很容易。

EDIT:As mentioned in the note below, it also support sqlcipherencryption

编辑:如下面的注释所述,它还支持sqlcipher加密

UPDATE:Since Google Code went read only the project has moved to it's own website https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

更新:由于谷歌代码变为只读,该项目已移至其自己的网站https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

回答by Yakeen

Take a look at:

看一眼:

http://zetetic.net/software/sqlcipher

http://zetetic.net/software/sqlcipher

It is open source.

它是开源的。

You can chek also the code for the wxsqlite3.

您还可以查看 wxsqlite3 的代码。