为 SQLCIPHER 附加数据库
我在为我的项目加密 SQLITE 数据库时遇到了很多问题,最后我尝试使用附加数据库方法来加密我未加密的数据库。
我尝试在终端上运行附加命令,却发现输出将是未加密的数据库。因此,我应该在我的项目中运行命令,并导入我的 sqlcipher 和 ssl 库,对吧?
所以我尝试了一下,该方法运行没有任何错误,但我什至没有在文档目录中创建加密数据库。我做错了什么? [下面的代码片段]
- (void)encryptDB
{
NSLog (@"Start");
sqlite3 *DB = [iPad_3AppDelegate getNewDBConnection];
sqlite3_exec(DB, "ATTACH DATABASE KeyCryptENC.db AS encrypted KEY 1234;", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.Account(A_id,C_ID,Account_Name,Username,Password,Other_Information);", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.Categories(C_ID,Category);", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.UserInfo(Password,Hint,Secret_Question,Secret_Answer);", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.Account SELECT * FROM Account;", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.Categories SELECT * FROM Categories;", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.UserInfo SELECT * FROM UserInfo;", NULL, NULL, NULL);
sqlite3_exec(DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
NSLog (@"End");
}
+ (sqlite3 *)getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"KeyCrypt.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
NSLog(@"Error in opening database :(");
}
return newDBconnection;
}
非常感谢大家的帮助!
I have had many problems getting my SQLITE database encrypted for my project and finally i'm trying to use the attach database method to encrypt my unencrypted database.
I've tried running the attach command on Terminal, only to realise the output would be a unencrypted database. So by right i'm supposed to run the commands in my project, with my sqlcipher and ssl libraries imported right?
So I tried it, the method runs without any faults, but i didn't even get the encrypted database to be created in the documents directory. What am I doing wrong? [Code Snippet Below]
- (void)encryptDB
{
NSLog (@"Start");
sqlite3 *DB = [iPad_3AppDelegate getNewDBConnection];
sqlite3_exec(DB, "ATTACH DATABASE KeyCryptENC.db AS encrypted KEY 1234;", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.Account(A_id,C_ID,Account_Name,Username,Password,Other_Information);", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.Categories(C_ID,Category);", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.UserInfo(Password,Hint,Secret_Question,Secret_Answer);", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.Account SELECT * FROM Account;", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.Categories SELECT * FROM Categories;", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.UserInfo SELECT * FROM UserInfo;", NULL, NULL, NULL);
sqlite3_exec(DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
NSLog (@"End");
}
+ (sqlite3 *)getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"KeyCrypt.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
NSLog(@"Error in opening database :(");
}
return newDBconnection;
}
Many thanks for the help guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更深入的研究,我意识到了加密的问题,ME。我使用的是 Mac OS X 原生 sqlite3。是的,这个链接应该可以帮助任何面临与我类似问题的人。
BWAHAHA,我觉得自己很傻,没有早点想到这一点。
after more intensive research I realised the problem to the encryption, ME. I was using Mac OS X native sqlite3. And yeah this link should help anyone who faces a problem similar to mine.
BWAHAHA, i feel so silly for not thinking of this earlier.