为 SQLCIPHER 附加数据库

发布于 2024-10-06 08:32:04 字数 1774 浏览 5 评论 0原文

我在为我的项目加密 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-10-13 08:32:04

经过更深入的研究,我意识到了加密的问题,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文