无法使用 SQLCipher 加密

发布于 2024-10-05 18:24:52 字数 1390 浏览 3 评论 0原文

我目前正在为 iPhone 开发一个简单的帐户管理应用程序。我正在使用 sqlcipher 来加密和解密数据库。

目前,我的应用程序包中有一个未加密的数据库,我想将其复制到 iPhone 文档目录然后对其进行加密,或者在将其复制到文档目录之前对其进行加密。

我面临的问题是,无论我做什么,我似乎都会得到一个未加密的数据库,无论我尝试使用哪种加密方法,无论是“ATTACH”数据库方法还是“key()/rekey” ()“ 方法。

我尝试在终端中使用“ATTACH”数据库方法,但结果是一个未加密的数据库。我尝试以编程方式使用“key()/rekey()”方法,如下所示:

 sqlite3 *db;
 NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"KeyCrypt.sqlite"];
 if (sqlite3_open([defaultDBPath UTF8String],&db)==SQLITE_OK) {
  NSLog (@"Running keying.");
  sqlite3_key(db, "1234", 4);
  if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
   // password is correct, or, database has been initialized
   NSLog (@"This has occured correctly?");
  } 
  else 
  {
   // incorrect password!
   NSLog (@"This has occured incorrectly?");
  }
 }

我在某处做错了什么吗? 我已经尝试在线研究一整天,但无法找到解决方案来解释为什么我的数据库在运行之前或运行期间没有加密:(

如果您需要任何额外的信息,我愿意提供给您,请帮助学生

编辑:

用于键入我的数据库的方法摘录。

 //Initializing the sqlite3_key function.
        int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
        sqlite3_key(db, "1234", 4);

显然我没有初始化 sqlite3_key -_-”。 此外,即使文件已加密,检查仍然表明发生了错误并且数据库未成功打开。

关于打开数据库,我打开数据库的每个实例都必须运行 sqlite3_key,对吧?在这种情况下,我可以正常访问数据库,对吗?

感谢您的所有帮助。

I'm currently working on a simple account management app for the iPhone. I'm using sqlcipher to encrypt and decrypt the database.

Currently i have an unencrypted database in the application bundle, which i want to either copy to the iPhone document directory and then get it encrypted, or get it encrypted before i copy it over to the document directory.

The problem that i'm facing is that no matter what i do, i seem to get an unencrypted database, no matter what method of encryption i try to use, be it the "ATTACH" database method or the "key()/rekey()" method.

I tried using the "ATTACH" database method in terminal but the result was an unencrypted database. I tried using the "key()/rekey()" method programmatically as seen here:

 sqlite3 *db;
 NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"KeyCrypt.sqlite"];
 if (sqlite3_open([defaultDBPath UTF8String],&db)==SQLITE_OK) {
  NSLog (@"Running keying.");
  sqlite3_key(db, "1234", 4);
  if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
   // password is correct, or, database has been initialized
   NSLog (@"This has occured correctly?");
  } 
  else 
  {
   // incorrect password!
   NSLog (@"This has occured incorrectly?");
  }
 }

Am i doing something wrong, somewhere?
I've tried researching online for one whole day and couldn't come close to finding a solution for why my database isn't encrypted before or during runtime :(

If you require any extra information i am willing to provide it to you, please help a student out!

THANK YOU!

EDIT:

Excerpt of method used to key my db.

 //Initializing the sqlite3_key function.
        int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
        sqlite3_key(db, "1234", 4);

Apparently i didn't initialize the sqlite3_key -_-".
Also eventhough the file is encrypted the check still says something occurred incorrectly and database wasn't successfully opened.

Regarding the opening of databases, every instance i open my database i have to run the sqlite3_key right? And during that instance I can access the database as per normal right?

Thank you for all the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魂归处 2024-10-12 18:24:52

SQLCipher 不再支持使用重新生成密钥将未加密的数据库转换为加密的数据库,因为更改页面大小和每页初始化向量所需的保留字节涉及多种复杂性。因此,您提供的代码将无法在任何最新版本的 SQLCipher 上运行。根据您想要执行的操作,有两种选择:

  1. 我们当前向数据库添加加密的指南是使用 ATTACH。简而言之,您将加密的数据库附加到标准 SQLite 数据库,然后在两者之间复制数据。这是描述该方法的帖子: 如何加密纯文本 SQLite 数据库以使用 SQLCipher
  2. 无需在应用程序包中分发未加密的数据库,只需在使用已知密钥的应用程序中包含数据库的加密副本即可。然后,在启动时,将数据库复制到位,使用已知的默认密钥将其打开,然后使用新的每用户密钥数据运行 rekey。这将重写数据库以根据需要使用新密钥。

Using rekey to convert an unencrypted database to an encrypted database is no longer supported by SQLCipher due to several complexities involved with changing the page size and reserved bytes required for per-page initialization vectors. Thus, the code you provided will not operate on any recent version of SQLCipher. Based on what you want to do, there are two options:

  1. Our current guidance for adding encryption to a database is to use ATTACH. In short, you attach an encrypted database to your standard SQLite database, and then copy data between the two. Here is a post describing the approach: How to encrypt a plaintext SQLite database to use SQLCipher
  2. Instead of distributing an unencrypted database in the application bundle, just include an encrypted copy of the the database in the that is using a known key. Then, on startup, copy the database into place, open it with the known default key, and and run rekey with the new per-user key data. This would rewrite the database to use the new key as you wanted.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文