java sqlite 密码保护_密码保护SQLite数据库 . 可能吗?

要 encrypt an existing unencrypted database 或 to change the password of an encrypted database ,打开数据库,然后使用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");

要 decrypt an existing encrypted database 使用 NULL 或 "" 密码调用 ChangePassword() :

// 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 中指定密码,如上例所示,或在打开新 SQLiteConnection 之前调用 SetPassword() 函数 . 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();

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值