设为首页 收藏本站
查看: 724|回复: 0

[经验分享] SQLCipher加解密Android sqlite

[复制链接]

尚未签到

发表于 2016-11-30 07:12:01 | 显示全部楼层 |阅读模式
具体的操作:http://sqlcipher.net/sqlcipher-for-android/  

如果sql语句执行过程中SQLException: SQL logic error or missing database ,那就是你的zip包的版本太低了,请升级到最新的版本(用我附件中的版本也就没问题)



SQLCipher for Android Application Integration
This tutorial will cover integrating the binaries of SQLCipher for Android into an Android application. This tutorial assumes the Android SDK is already installed on the local development machine.

Create sample Android application
To create a sample Android application, issue the following commands:

% mkdir demo-app
% cd demo-app
% android create project \
  --target android-10 \
  --name demoapp \
  --path . \
  --activity HelloSQLCipherActivity \
  --package com.demo.sqlcipher
Obtaining binaries
The source code for SQLCipher for Android is publicly maintained on Github.com, the current binary release can be found here. For information on building the source of SQLCipher for Android, please see the instructions below. We will manually download and extract the contents of the compressed tar file into your application root directory with the commands below:

% curl -L -o SQLCipherforAndroid2.0.5.zip https://github.com/downloads/sqlcipher/android-database-sqlcipher/SQLCipher%20for%20Android%202.0.5.zip
% unzip SQLCipherforAndroid2.0.5.zip
We need to copy the various library and asset files over into the root directory of our application. Execute the following commands:

% cp -R SQLCipher\ for\ Android\ 2.0.5/libs/* libs
% cp -R SQLCipher\ for\ Android\ 2.0.5/assets .
The files for your demo application should look similar to this structure:



Integration
Launch Eclipse and choose File -> New -> Android Project from the menu. Give the project a name and choose Create project from existing source pointing to your application root directory. It should look similar to this:



Next we need to reference the 3 jar files in our libs directory. Right click on the project node in the Package Explorer and select Build Path -> "Configure Build Path...". Select the Libraries tab and press the "Add JARs..." button. Select commons-codec.jar, guava-r09.jar and sqlcipher.jar. After selecting these three jars, the screen should appear like this:



Next we will modify the source of the default activity to properly initialize the native libraries for SQLCipher and then create our database file inserting a record. In particular, note the import of net.sqlcipher.database.SQLiteDatabase instead of android.database.sqlite.SQLiteDatabase as well as the call to SQLiteDatabase.loadLibs(this). The call to SQLiteDatabase.loadLibs(this) must occur before any other database operation.

package com.demo.sqlcipher;

import java.io.File;
import net.sqlcipher.database.SQLiteDatabase;
import android.app.Activity;
import android.os.Bundle;

public class HelloSQLCipherActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InitializeSQLCipher();
    }

    private void InitializeSQLCipher() {
        SQLiteDatabase.loadLibs(this);
        File databaseFile = getDatabasePath("demo.db");
        databaseFile.mkdirs();
        databaseFile.delete();
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null);
        database.execSQL("create table t1(a, b)");
        database.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"one for the money",
                                                                        "two for the show"});
    }
}
The sample application should now be able to run on either an emulator or device.


SQLCipher for Android Build Tutorial
This tutorial will cover building the source code for SQLCipher for Android from scratch. This tutorial targets revision 7 of the Android NDK and is intended for complication under Linux or OSX environments.

System environment requrements
In order to build the source of SQLCipher for Android several tools are required. Several development kits including the Android SDK, Android NDK and the JDK need to be present on the build machine. We will need the ANDROID_NDK_ROOT environment variable available on the PATH. You will also need Git to acquire the source tree. To verify your PATH is properly configured, execute the following command:

% which ndk-build
/Users/nparker/bin/android-ndk/ndk-build
If you receive ndk-build not found you will need to add the following to your .bashrc or corresponding shell configuration file. Note the ~/bin/android-ndk path below represents the location on my machine for the root directory of the Android NDK, adjust accordingly:

export ANDROID_NDK_ROOT=~/bin/android-ndk
export PATH=$ANDROID_NDK_ROOT:$PATH
We will use Make as our base build tool for interacting with the NDK toolchain.

Get the source
The source code for SQLCipher for Android is maintained publicly on Github.com. We will start by cloning the repository locally:

% cd ~/code
% git clone git://github.com/sqlcipher/android-database-sqlcipher.git
Building
The process of building the code is split into two different phases. The first time this is performed we need to initialize various git submodules that SQLCipher for Android depends on. This is all automated through Make.

% cd android-database-sqlcipher
% make init
Once git has finished pulling down the various submodules required for compilation, we can begin the build process:

% make
This process will take some time to complete. Once it has completed, you should have the following files available for integration into your application's libs directory:

% tree libs
libs
├── armeabi
│   ├── libdatabase_sqlcipher.so
│   ├── libsqlcipher_android.so
│   └── libstlport_shared.so
├── commons-codec.jar
├── guava-r09.jar
└── sqlcipher.jar
Localization dependencies
SQLCipher for Android depends on localization data from the ICU project. SQLCipher for Android will attempt to use a system provided ICU localization data file called icudt46l.dat located in the /system/usr/icu directory if available. If that is not found, SQLCipher for Android will attempt to unzip the icudt46l.zip file located within the applications asset directory. It is recommended that the icudt46.zip file be included with your application for best platform compatibility. If you need to adjust the size of the localization data for your application, a ICU data library customizer is available here.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307323-1-1.html 上篇帖子: Android初级教程十一——SQLite 下篇帖子: Sqlite无脑使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表