密码保护 SQLite 数据库。可以吗?

问题描述 投票:0回答:8

我必须面对一个新的小项目。它将有大约 7 或 9 个表,其中最大的表将以每月 1000 行的最大速度增长。

我认为 SQLite 作为我的数据库...但我需要保护数据库,以防有人想要更改数据库中的数据

我的主要问题是:

是否可以像访问时那样使用密码保护 sqlite 数据库?

开发将在 C# 上进行,但我正在寻找免费的东西。

c# sqlite
8个回答
89
投票

您可以使用密码保护 SQLite3 DB。在进行任何操作之前,请按如下方式设置密码。

SQLiteConnection conn = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
conn.SetPassword("password");
conn.Open();

然后下次你就可以像这样访问它了

conn = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;Password=password;");
conn.Open();

这将不允许任何 GUI 编辑器查看您的数据。如果您提供密码,某些编辑器可以解密数据库。使用的算法是RSA。

稍后如果您想更改密码,请使用

conn.ChangePassword("new_password");

要重置或删除密码,请使用

conn.ChangePassword(String.Empty);

37
投票

您可以使用 sqlite .net 提供程序 (System.Data.SQLite) 的内置加密。查看更多详细信息http://web.archive.org/web/20070813071554/http://sqlite.phxsoftware.com/forums/t/130.aspx

加密现有的未加密数据库,或更改加密数据库的密码,请打开数据库,然后使用 SQLiteConnection 的 ChangePassword() 函数:

// Opens an unencrypted database
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.Open();
// Encrypts the database. The connection remains valid and usable afterwards.
cnn.ChangePassword("mypassword");

解密现有的加密数据库,请使用

ChangePassword()
NULL
密码调用
""

// Opens an encrypted database
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3;Password=mypassword");
cnn.Open();
// Removes the encryption on an encrypted database.
cnn.ChangePassword(null);

要打开现有的加密数据库,或创建新的加密数据库,请在

ConnectionString
中指定密码(如上例所示),或在打开新的
SetPassword()
之前调用
SQLiteConnection
函数。
ConnectionString
中指定的密码必须是明文,但
SetPassword()
函数中提供的密码可以是二进制字节数组。

// Opens an encrypted database by calling SetPassword()
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.SetPassword(new byte[] { 0xFF, 0xEE, 0xDD, 0x10, 0x20, 0x30 });
cnn.Open();
// The connection is now usable

默认情况下,当将另一个数据库文件附加到现有连接时,ATTACH 关键字将使用与主数据库相同的加密密钥。要更改此行为,请使用 KEY 修饰符,如下所示:

如果您使用明文密码附加加密数据库:

// Attach to a database using a different key than the main database
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.Open();
cmd = new SQLiteCommand("ATTACH DATABASE 'c:\\pwd.db3' AS [Protected] KEY 'mypassword'", cnn);
cmd.ExecuteNonQuery();

要使用二进制密码附加加密数据库:

// Attach to a database encrypted with a binary key
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.Open();
cmd = new SQLiteCommand("ATTACH DATABASE 'c:\\pwd.db3' AS [Protected] KEY X'FFEEDD102030'", cnn);
cmd.ExecuteNonQuery();

16
投票

使用 SQLCipher,它是 SQLite 的开源扩展,可为数据库文件提供透明的 256 位 AES 加密。 http://sqlcipher.net


7
投票

您可以使用 SEE 插件加密您的 SQLite 数据库。这样您就可以防止未经授权的访问/修改。

引用SQLite文档:

SQLite 加密扩展 (SEE) 是 SQLite 的增强版本,它使用 128 位或 256 位 AES 加密数据库文件,以帮助防止未经授权的访问或修改。整个数据库文件都经过加密,因此对于外部观察者来说,数据库文件似乎包含白噪声。没有任何内容可以将该文件标识为 SQLite 数据库。

您可以在此链接中找到有关此插件的更多信息。


3
投票

如果您使用FluentNHibernate,您可以使用以下配置代码:

private ISessionFactory createSessionFactory()
{
    return Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.UsingFileWithPassword(filename, password))
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<DBManager>())
            .ExposeConfiguration(this.buildSchema)
            .BuildSessionFactory();    
}

private void buildSchema(Configuration config)
{
        if (filename_not_exists == true)
        {
            new SchemaExport(config).Create(false, true);
        }
}    

方法 UsingFileWithPassword(文件名, 密码) 加密数据库文件并设置密码。
它仅在创建新数据库文件时运行。旧的未加密的用此方法打开失败。


3
投票

一个选择是VistaDB。它们允许数据库(甚至表)受到密码保护(并且可以选择加密)。


2
投票

我知道这是一个老问题,但简单的解决方案不是只在操作系统级别保护文件吗?只要阻止用户访问该文件,他们就不能碰它。这只是一个猜测,我不确定这是否是一个理想的解决方案。


0
投票

为什么需要加密数据库?用户可以轻松地反汇编你的程序并找出密钥。如果您要对其进行加密以进行网络传输,请考虑使用 PGP,而不是将加密层压缩到数据库层中。

© www.soinside.com 2019 - 2024. All rights reserved.