连接SQLCIPHER的数据库 [英] Attach Database for SQLCIPHER

查看:170
本文介绍了连接SQLCIPHER的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的项目加密我的SQLITE数据库时遇到了很多问题,最后我试图使用附加数据库方法来加密我的未加密数据库。



'试过在终端上运行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];
//打开数据库。数据库是在应用程序外部准备的。
if(sqlite3_open([path UTF8String],& newDBconnection)== SQLITE_OK){
NSLog(@Database Successfully Opened :));
} else {
NSLog(@打开数据库时出错:);
}
return newDBconnection;
}
解决方案

p>经过更加深入的研究我意识到了加密的问题,我使用Mac OS X本机sqlite3。是这个链接应该帮助面临类似我的问题的任何人。



BWAHAHA,我觉得这样愚蠢这早些。


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!

解决方案

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.

这篇关于连接SQLCIPHER的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆