encrypt sqlite with sqlcipher

##Get the sqlcipher SourceCode

1
2
#使用2.1的版本比较稳定些
https://github.com/welsonla/sqlcipher/archive/v2.1.1.tar.gz

##Complite the source code
动态链接的编译方法(Compiling with dynamic linking)[推荐]:

1
2
3
./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
make
ln -s /Users/wanyc/sqlcipher/sqlite3 /usr/bin/sqlcipher

静态库的编译方法: (replace /path/to with the path to libcrypto.a)

1
2
#./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="/path/to/libcrypto.a"
#make

##How to encrypt a sqlite(In Shell)

#加密数据库几种方式

####1.shell方式加密一个sqlite

1
2
sqlcipher test.db
> PRAGMA key='test'; //必须在打开数据库第一步来执行

####2.(In shell)

1
2
3
4
5
sqlciper test.db
PRAGMA KEY='helloworld';#第一步必须执行
ATTACH DATABASE 'encrypted.db' as encrypted KEY 'SomePassword'; //encrypted.db是要导出的新的数据库
SELECT sqlcipher_export('encrypted');
DETACH DATABASE encrypted;

#####3..Object-c代码实现对一个非加密库导入到加密库的方法(已经验证)(Xcode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *attachPath = [documentPath stringByAppendingPathComponent:@"new.db"];

if (sqlite3_open([path_u UTF8String], &convert_DB) == SQLITE_OK) {
NSString *sql = [NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY '1234';",attachPath];

//执行Attach操作
sqlite3_exec(convert_DB, [sql UTF8String] , NULL, NULL, NULL);

// 导出数据库
sqlite3_exec(convert_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, NULL);

// 执行分离
sqlite3_exec(convert_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);

NSLog (@"End database copying at %@",[NSDate date]);
sqlite3_close(convert_DB);
}
else {
sqlite3_close(convert_DB);
NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(convert_DB));
}

#为加密数据库解密

####1.先导出现有库的数据(In shell)

1
2
3
sqlcipher plaintext.db
.output dsa.sql
.dump

####将新的数据库导出,并加密,之后导入非加密的库里面的数据

1
2
3
sqlcipher another.db
PRAGMA key='aaa';
.read dsa.sql

####2. 为加密后的sqlite执行解密(其实步骤与加密一样,只要把key设置为空就实现了不加密)

1
2
3
ATTACH DATABASE 'encrypted.db' as encrypted KEY ''; //encrypted.db是要导出的新的数据库
SELECT sqlcipher_export('encrypted');
DETACH DATABASE encrypted;

####3.(In shell)

1
2
3
4
5
sqlciper test.db
PRAGMA KEY='helloworld';#第一步必须执行
ATTACH DATABASE 'encrypted.db' as encrypted KEY '';
SELECT sqlcipher_export('encrypted');
DETACH DATABASE encrypted;

##参考