我在为我的项目加密我的SQLITE数据库时遇到了很多问题,最后我尝试使用attach数据库方法来加密我的未加密数据库。
我尝试在终端上运行attach命令,只是为了实现输出是一个未加密的数据库。所以,我应该在我的项目中运行命令,并导入我的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;
}
非常感谢帮助人员!
答案 0 :(得分:1)
BWAHAHA,我觉得因为没想到这件事而感到很傻。