SQLite加密

SQLite加密,第1张

概述SQLiteCrypt API SQLiteCrypt is very easy to use. SQLiteCrypt is based on SQLite with all API functions remain unchanged. All encryption/ decryption routines are performed transparently. SQLiteCrypt us

sqliteCrypt API

sqliteCrypt is very easy to use. sqliteCrypt is based on sqlite with all API functions remain unchanged. All encryption/ decryption routines are performed transparently. sqliteCrypt uses three PRAGMA statements to work with encrypted database:

PRAGMA key = 'the passphrase'// passphrase

PRAGMA rekey = 'new passphrase'// change passphrase

PRAGMA lic = 'the license key'//the software key

The first PRAGMA statement is used to create/ access encrypted database. The second one will re-write database with new passphrase. The third one used to IDentify legal copy of sqliteCrypt software.

Remark: Do not userekey in mIDdle of a transaction. This method decrypt whole database using old passphrase,then encrypt using new passphrase. You can continue to use sqlite API functions,no need of closing and re-opening database. This is time-consuming operation.

Example 1: Create/ open encrypted sqlite database

sqlite3_open_v2("data.db",&db,sqlITE_OPEN_READWRITE | sqlITE_OPEN_CREATE,NulL);

sqlite3_stmt* stm;
const char*pzTail;

intres;

res = sqlite3_prepare(db,"PRAGMA key = 'ac23';",-1,&stm,&pzTail);//ac23 is database passphrase
res = sqlite3_step(stm);

res = sqlite3_prepare(db,"PRAGMA lic = '77523-009-0000007-72328';",&pzTail);//software license key
res = sqlite3_step(stm);

//Now you have all access to data.db

Example 2: Decrypt sqlite database (remove encryption,so any other sqlite application can open it)

sqlite3_open_v2("data.db",NulL);

sqlite3_stmt* stm;
const char*pzTail;

intres;

res = sqlite3_prepare(db,&pzTail);//ac23 is current passphrase
res = sqlite3_step(stm);

res = sqlite3_prepare(db,&pzTail);//software license key
res = sqlite3_step(stm);

//Now you have all access to encrypted data.db

res = sqlite3_prepare(db,"PRAGMA rekey = '';",&pzTail);// new empty passphrase
res = sqlite3_step(stm);

//Now data.db is NOT encrypted

Example 3: Change encryption key on-the-fly

sqlite3_open_v2("data.db",NulL);

sqlite3_stmt* stm;
const char* pzTail;

intres;

res = sqlite3_prepare(db,"PRAGMA rekey = 'abc123';",&pzTail);//abc123 is new passphrase
res = sqlite3_step(stm);

//Now data.db re-written using new passphrase

Example 4: Encrypt sqlite database (add encryption to regular sqlite database)

sqlite3_open_v2("data.db",&pzTail);//software license key
res = sqlite3_step(stm);

//Now you have all access to regular data.db

res = sqlite3_prepare(db,&pzTail);// encrypt database using abc123 passphrase
res = sqlite3_step(stm);

//Now data.db is encrypted

Example 5: Using sqliteCrypt command line tool

opening encrypted db without passphrase:

D:\>sqlite.exe data.db
sqlite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter sql statements terminated with a ";"
sqlite> select * from _MapPropertyA;
Error: file is encrypted or is not a database

Querry on an encrypted database

D:\>sqlite.exe data.db sqlite version 3.7.15.2 2013-01-09 11:53:05 Enter ".help" for instructions Enter sql statements terminated with a ";" sqlite> PRAGMA key = 'ac23'; sqlite> PRAGMA lic = '77523-009-0000007-72328'; sqlite> select * from _MapPropertyA; 3.0|8.0 3.0|8.0 总结

以上是内存溢出为你收集整理的SQLite加密全部内容,希望文章能够帮你解决SQLite加密所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1180367.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存