iOS Sqlite加密(FMDB/SQLCipher)
- /**
- * 对数据库加密
- *
- * @param path path description
- *
- * @return return value description
- */
- + (BOOL)encryptDatabase:(NSString *)path;
- /**
- * 对数据库解密
- *
- * @param path path description
- *
- * @return return value description
- */
- + (BOOL)unEncryptDatabase:(NSString *)path;
- /**
- * 修改数据库秘钥
- *
- * @param dbPath dbPath description
- * @param originKey originKey description
- * @param newKey newKey description
- *
- * @return return value description
- */
- + (BOOL)changeKey:(NSString *)dbPath originKey:(NSString *)originKey newKey:(NSString *)newKey;
- + (BOOL)encryptDatabase:(NSString *)path {
- NSString *sourcePath = path;
- NSString *targetPath = [NSString stringWithFormat:@"%@.tmp.db", path];
- if([self encryptDatabase:sourcePath targetPath:targetPath]) {
- NSFileManager *fm = [[NSFileManager alloc] init];
- [fm removeItemAtPath:sourcePath error:nil];
- [fm moveItemAtPath:targetPath toPath:sourcePath error:nil];
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)unEncryptDatabase:(NSString *)path {
- NSString *sourcePath = path;
- NSString *targetPath = [NSString stringWithFormat:@"%@.tmp.db", path];
- if([self unEncryptDatabase:sourcePath targetPath:targetPath]) {
- NSFileManager *fm = [[NSFileManager alloc] init];
- [fm removeItemAtPath:sourcePath error:nil];
- [fm moveItemAtPath:targetPath toPath:sourcePath error:nil];
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)encryptDatabase:(NSString *)sourcePath targetPath:(NSString *)targetPath {
- const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY '%@';", targetPath, kEncryptKey] UTF8String];
- sqlite3 *unencrypted_DB;
- if (sqlite3_open([sourcePath UTF8String], &unencrypted_DB) == SQLITE_OK) {
- // Attach empty encrypted database to unencrypted database
- sqlite3_exec(unencrypted_DB, sqlQ, NULL, NULL, NULL);
- // export database
- sqlite3_exec(unencrypted_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, NULL);
- // Detach encrypted database
- sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
- sqlite3_close(unencrypted_DB);
- return YES;
- }
- else {
- sqlite3_close(unencrypted_DB);
- NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB));
- return NO;
- }
- }
- + (BOOL)unEncryptDatabase:(NSString *)sourcePath targetPath:(NSString *)targetPath {
- const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS plaintext KEY '';", targetPath] UTF8String];
- sqlite3 *encrypted_DB;
- if (sqlite3_open([sourcePath UTF8String], &encrypted_DB) == SQLITE_OK) {
- sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA key = '%@';", kEncryptKey] UTF8String], NULL, NULL, NULL);
- // Attach empty unencrypted database to encrypted database
- sqlite3_exec(encrypted_DB, sqlQ, NULL, NULL, NULL);
- // export database
- sqlite3_exec(encrypted_DB, "SELECT sqlcipher_export('plaintext');", NULL, NULL, NULL);
- // Detach unencrypted database
- sqlite3_exec(encrypted_DB, "DETACH DATABASE plaintext;", NULL, NULL, NULL);
- sqlite3_close(encrypted_DB);
- return YES;
- }
- else {
- sqlite3_close(encrypted_DB);
- NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(encrypted_DB));
- return NO;
- }
- }
- + (BOOL)changeKey:(NSString *)dbPath originKey:(NSString *)originKey newKey:(NSString *)newKey {
- sqlite3 *encrypted_DB;
- if (sqlite3_open([dbPath UTF8String], &encrypted_DB) == SQLITE_OK) {
- sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA key = '%@';", originKey] UTF8String], NULL, NULL, NULL);
- sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA rekey = '%@';", newKey] UTF8String], NULL, NULL, NULL);
- sqlite3_close(encrypted_DB);
- return YES;
- }
- else {
- sqlite3_close(encrypted_DB);
- NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(encrypted_DB));
- return NO;
- }
- }
iOS Sqlite加密(FMDB/SQLCipher)的更多相关文章
- iOS SQLite解密之SQLCipher
开门见山,本文主要讲在Mac下使用SQLCipher编译解密数据库文件方法,iOS项目集成SQLCipher自己可以百度,网上帖子很多. 官方集成文档:https://www.zetetic.net/ ...
- iOS 使用FMDB SQLCipher给数据库加密
关于SQLite,SQLCipher和FMDB SQLite是一个轻量的.跨平台的.开源的数据库引擎,它的在读写效率.消耗总量.延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案 ...
- 【iOS】FMDB/SQLCipher数据库加解密,迁移
2016-04-19更新:本文代码可能有些问题,请移步 http://zhengbomo.github.io/2016-04-18/sqlcipher-start/ 查看 sqlite应用几乎在所有的 ...
- SQLite 加密 -- SQLCipher
SQLite3 插件 github 下载地址 插件配置步骤地址 购买地址 其他加密方式介绍 SQLCipher API 地址 前言 应用使用 SQLite 来存储数据,很多时候需要对一部分的数据进行加 ...
- Android中对sqlite加密--SQLCipher
原文:Android中对sqlite加密--SQLCipher android中有些时候会将一些隐私数据存放在sqlite数据库中,在root过的手机中通过RE就能够轻松的打开并查看数据库所有内容,所 ...
- SQLite加密方式 [转]
关于SQLite SQLite是一个轻量的.跨平台的.开源的数据库引擎,它的在读写效率.消耗总量.延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案(如iOS.Android). ...
- iOS sqlite ORM框架-LKDBHelper
LKDBHelper 一个sqlite ORM(全自动操作数据库)框架. 线程安全.不再担心递归锁死的问题 安装要求 iOS 4.3+ 仅支持 ARC FMDB 添加到你的项目 如果你使用 Cocoa ...
- iOS代码加密常用加密方式
iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...
- iOS开发 数据库FMDB
iOS开发 数据库FMDB 1.简介 需求作用: 如果需要保存大量的结构较为复杂的数据时候, 使用数据库, 例如交规考试项目 常用的数据库: (1)Microsoft SQL Server 2000 ...
随机推荐
- Bootstrap开发模板
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- HDU 5348 MZL's endless loop(DFS去奇数度点+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给你一张图,有n个点,和m条无向边,让你把m条无向边变成有向边,使得每个节点的|出度- ...
- linux c下输入密码不回显
今天做一个登录程序,需要屏蔽掉密码,于是自己就在网上找资料,找到了一种和linux终端下输入密码方式相同的方法,不显示在终端,具体代码实现如下. #include<stdio.h> #in ...
- TeX Live & TeXstudio 安装手记
数据库课上又看到了那位用 beamer 做 slides 的师兄,想到自己一拖再拖的LaTeX入门,决定赶快动手装个环境再说~在经过一番搜索和研究之后决定先在 windows 底下试用,选择 TeX ...
- SaltStack配置管理--状态间的关系(六)
一.include的引用 需求场景:用于含有多个SLS的状态,使用include可以进行多个状态的组合,将安装apache,php,mysql集合在一个sls中 [root@7mini-node1 p ...
- 尝试php命令行脚本多进程并发执行
php不支持多线程,但是我们可以把问题转换成“多进程”来解决.由于php中的pcntl_fork只有unix平台才可以使用,所以本文尝试使用popen来替代. 下面是一个例子: 被并行调用的子程序 ...
- Python并发编程-管道
管道的作用- 两个进程间传递消息 from multiprocessing import Pipe, Process def func(conn1,conn2): conn2.close() #子进程 ...
- Linux下定时备份MySQL数据库的Shell脚本
Linux下定时备份MySQL数据库的Shell脚本 对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...
- Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)
E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- JSP与Servlet传值及对比
JSP是Servlet技术的扩展,本质上是Servlet的简易方式,更强调应用的外表表达. JSP编译后是”类servlet”. Servlet和JSP最主要的不同点在于,Servlet的应用逻辑是在 ...