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 XFFEEDD102030", cnn);

cmd.ExecuteNonQuery();

要 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 XFFEEDD102030", cnn); cmd.ExecuteNonQuery();
经验分享 程序员 微信小程序 职场和发展