在Android上保护数据,对sqlite 数据库加密(SQLCipher)
Android.mk 代码
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := samuel
LOCAL_SRC_FILES := samuel.c
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI :=armeabi-v7a
APP_PLATFORM:=android-8
APP_STL:=gnustl_static
APP_CFLAGS += -Wno-error=format-security
samuel.c
#include<string.h>
#include<jni.h>
#include<stdio.h>
#include<stdlib.h>
#include<dlfcn.h>
void *handle;
const char *IKey="[数据库密码]";
int IndexOf(char *strSource, char *strFind)
{
char *p = strSource;
int i = 0;
p = strstr(strSource, strFind);
if (p == NULL)
return -1;
else {
while (strSource != p)
{
strSource++;
i++;
}
}
return i;
}
char*subString(char *someString, int n)
{
char *new = malloc(sizeof(char)*n + 1);
strncpy(new, someString, n);
new[n] = '\0';
return new;
}
char* joinstr(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);
if (result == NULL) exit (1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
//参数,src 字符串源,sub想要替换的字符串,dst,用来替换的字符串
char*replaceFirst(char*src, char*sub, char*dst)
{
//记录当前指针位置
int pos = 0;
//记录偏移
int offset = 0;
//字符串长度
int srcLen, subLen, dstLen;
//返回内容
char*pRet = NULL;
//求得各字符串长度
srcLen = strlen(src);
subLen = strlen(sub);
dstLen = strlen(dst);
//申请替换后的字符串缓冲区。用dst替换sub,所以应该是srclen-sublen+dstlen,+1流出'\0'位置
pRet = (char*)malloc(srcLen + dstLen - subLen + 1);//(外部是否该空间)if (NULL != pRet)
{
//strstr查找sub字符串出现的指针。该指针减去src地址。得到相对位置
pos = strstr(src, sub) - src;
//拷贝src字符串,从首地址开始,pos个字符。
memcpy(pRet, src, pos);
//增加偏移位置到pos
offset += pos;
//拷贝dst到返回内容中。
memcpy(pRet + offset, dst, dstLen);
//重新定位偏移
offset += dstLen;
//拷贝src中,sub字符串后面的字符串到pRet中
memcpy(pRet + offset, src + pos + subLen, srcLen - pos - subLen);
//重新定位偏移
offset += srcLen - pos - subLen;
//最后添加字符串结尾标记'\0'
*(pRet + offset) = '\0';
}
//返回新构造的字符串
return pRet;
}
/*
** Open a new database handle.
*/
int sqlite3_open(const char *zFilename, void **ppDb){
int (*fun)(const char *zFilename1, void **ppDb1);
int s=0;
handle=dlopen("libsqlcipher_android.so",RTLD_NOW);
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_open");
s=fun(zFilename,ppDb);
return s;
}
int sqlite3_close(void *db){
int (*fun)(void *db1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_close");
s=fun(db);
dlclose(handle);
return s;
}
int sqlite3_key(void *db, const char *pKey, int nKey){
int IKeyLen=12;
int (*fun)(void *db1, const char *pKey1, int nKey1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_key");
s=fun(db,IKey,IKeyLen);
return s;
}
int sqlite3_prepare_v2(void *db,const char *zSql,int nBytes, void **ppStmt,const char **pzTail){
int (*fun)(void *db1,const char *zSql1,int nBytes1, void **ppStmt1,const char **pzTail1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_prepare_v2");
char *find = "ATTACH ";
int pos = IndexOf(zSql, find);
if(pos==0){
find = " KEY ";
pos = IndexOf(zSql, find);
char *temp=subString(zSql, pos);
char*newSql = joinstr(temp, find);
char*newPwd = joinstr(find," '");
newPwd = joinstr(newPwd, IKey);
newPwd = joinstr(newPwd, "'");
newSql = replaceFirst(newSql, find, newPwd);
s=fun(db,newSql,strlen(newSql),ppStmt,pzTail);
}
else{
s=fun(db,zSql,nBytes,ppStmt,pzTail);
}
return s;
}
int sqlite3_step(void *pStmt){
int (*fun)(void *pStmt1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_step");
s=fun(pStmt);
return s;
}
int sqlite3_finalize(void *pStmt){
int (*fun)(void *pStmt1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_finalize");
s=fun(pStmt);
return s;
}
const char *sqlite3_errmsg(void *db){
const char *(*fun)(void *db1);
const char *s;
if(NULL == handle)
return (char*)(-1);
*(void **)(&fun)=dlsym(handle,"sqlite3_errmsg");
s=fun(db);
return s;
}
int sqlite3_column_count(void *pStmt){
int (*fun)(void *pStmt1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_column_count");
s=fun(pStmt);
return s;
}
const char *sqlite3_column_name(void *pStmt, int N){
const char *(*fun)(void *pStmt1,int N1);
const char *s;
if(NULL == handle)
return "";
*(void **)(&fun)=dlsym(handle,"sqlite3_column_name");
s=fun(pStmt,N);
return s;
}
int sqlite3_column_type(void *pStmt, int i){
int (*fun)(void *pStmt1, int i1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_column_type");
s=fun(pStmt,i);
return s;
}
int sqlite3_column_int(void *pStmt, int i){
int (*fun)(void *pStmt1, int i1);
int s=0;
if(NULL == handle)
return -1;
*(void **)(&fun)=dlsym(handle,"sqlite3_column_int");
s=fun(pStmt,i);
return s;
}
char *sqlite3_column_text(void *pStmt, int i){
char *(*fun)(void *pStmt1, int i1);
char *s;
if(NULL == handle)
return "";
*(void **)(&fun)=dlsym(handle,"sqlite3_column_text");
s=fun(pStmt,i);
return s;
}
double sqlite3_column_double(void *pStmt, int i){
double (*fun)(void *pStmt1, int i1);
double s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_column_double");
s=fun(pStmt,i);
return s;
}
void *sqlite3_column_blob(void *pStmt, int i){
void *(*fun)(void *pStmt1, int i1);
void *s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_column_blob");
s=fun(pStmt,i);
return s;
}
int sqlite3_column_bytes(void *pStmt, int i){
int (*fun)(void *pStmt1, int i1);
int s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_column_bytes");
s=fun(pStmt,i);
return s;
}
int sqlite3_bind_double(void *pStmt, int i, double rValue){
int (*fun)(void *pStmt1, int i1, double rValue1);
int s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_bind_double");
s=fun(pStmt,i,rValue);
return s;
}
int sqlite3_bind_int(void *p, int i, int iValue){
int (*fun)(void *pStmt1, int i1, int rValue1);
int s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_bind_int");
s=fun(p,i,iValue);
return s;
}
int sqlite3_bind_text( void *pStmt, int i, const char *zData, int nData, void *xDel){
int (*fun)(void *pStmt1, int i1, const char *zData1, int nData1, void *xDel1);
int s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_bind_text");
s=fun(pStmt,i,zData,nData,xDel);
return s;
}
int sqlite3_bind_blob( void *pStmt, int i, const void *zData, int nData, void *xDel){
int (*fun)(void *pStmt1, int i1, const char *zData1, int nData1, void *xDel1);
int s;
if(NULL == handle)
return 0;
*(void **)(&fun)=dlsym(handle,"sqlite3_bind_blob");
s=fun(pStmt,i,zData,nData,xDel);
return s;
}