android加密数据库
1.概述
SharedPreferences是Android提供用来存储一些简单配置信息的机制,其以KEY-VALUE对的方式进行存储,以便我们可以方便进行读取和存储。主要可以用来存储应用程序的欢迎语、常量参数或登录账号密码等。
2.实例
(1)创建项目SharedPreferencesDemo项目
(2)编辑主界面的布局文件main.xml如下:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SharedPreferences,是Android提供用来存储一些简单的配置信息的一种机制。"
/>
(3)创建AES加解密工具类AESEncryptor.java
其中主要提供加密encrypt、解密decrypt两个方法。(AES加解密算法具体大家可以到网上搜索相关资料)
以下为该类文件的源码:
package ni.demo.sharedpreferences;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* AES加密器
* @author Eric_Ni
*
*/
public class AESEncryptor {
/**
* AES加密
*/
public static String encrypt(String seed, String cleartext) throws
Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
/**
* AES解密
*/
public static String decrypt(String seed, String encrypted) throws
Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws
Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2),
16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
(4)编辑SharedPreferencesDemo.java
源码如下:
package ni.demo.sharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SharedPreferencesDemo extends Activity {
public static final String MY_PREFERENCES = "MY_PREFERENCES";
//Preferences文件的名称
public static final String MY_ACCOUNT = "MY_ACCOUNT"; //
public static final String MY_PASSWORD = "MY_PASSWORD";
private EditText edtAccount;
private EditText edtPassword;
private Button btnClear;
private Button btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtAccount = (EditText)findViewById(R.id.edtAccount);
edtPassword = (EditText)findViewById(R.id.edtPassword);
//获取名字为“MY_PREFERENCES”的参数文件对象,并获得MYACCOUNT、MY_PASSWORD元素的值。
SharedPreferences sp = this.getSharedPreferences(MY_PREFERENCES, 0);
String account = sp.getString(MY_ACCOUNT, "");
String password = sp.getString(MY_PASSWORD, "");
//对密码进行AES解密
try{
password = AESEncryptor.decrypt("41227677", password);
}catch(Exception ex){
Toast.makeText(this, "获取密码时产生解密错误!", Toast.LENGTH_SHORT);
password = "";
}
//将账号和密码显示在EditText控件上。
edtAccount.setText(account);
edtPassword.setText(password);
//获取"清空"按钮的对象,并为其绑定监听器,如被点击则清空账号和密码控件的值。
btnClear = (Button)findViewById(R.id.btnClear);
btnClear.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
edtAccount.setText("");
edtPassword.setText("");
}
});
//获取“退出”按钮的对象,并为其绑定监听,如被点击则退出程序。
btnExit = (Button)findViewById(R.id.btnExit);
btnExit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
SharedPreferencesDemo.this.finish();
}
});
}
@Override
protected void onStop() {
super.onStop();
//获得账号、密码控件的值,并使用AES加密算法给密码加密。
String account = edtAccount.getText().toString();
String password = edtPassword.getText().toString();
try{
password = AESEncryptor.encrypt("41227677", password);
}catch(Exception ex){
Toast.makeText(this, "给密码加密时产生错误!", Toast.LENGTH_SHORT);
password = "";
}
//获取名字为“MY_PREFERENCES”的参数文件对象。
SharedPreferences sp = this.getSharedPreferences(MY_PREFERENCES, 0);
//使用Editor接口修改SharedPreferences中的值并提交。
Editor editor = sp.edit();
editor.putString(MY_ACCOUNT, account);
editor.putString(MY_PASSWORD,password);
editor.commit();
}
}
(5)效果测试
首先,在AVD我们可以看到如下界面,在两个控件上我们分别输入abc和123456。
接着,我们打开DDMS的File
Explore可以看到在data->data->ni->shared_prefs下面产生了一个名字叫做MY_PREFERENCES.xml的文件,该文件就是用来存储我们刚才设置的账号和密码。
将其导出,并打开,可以看到如下内容:
abc这说明我们可以成功将账号和加密后的密码保存下来了。
最后,我们点击“退出”按钮将应用程序结束掉,再重新打开。我们又再次看到我们退出前的界面,账号密码已经被重新读取出来。
文章的最后,我们进入Android API手册,看看关于SharedPreferences的介绍:
Interface for accessing and modifying preference data returned by
getSharedPreferences(String, int). For any particular set of preferences, there
is a single instance of this class that all clients share. Modifications to the
preferences must go through an SharedPreferences.Editor object to ensure the
preference values remain in a consistent state and control when they are
committed to storage.
SharedPreferences是一个用来访问和修改选项数据的接口,通过getSharedPreferences(Stirng,int)来获得该接口。对于任何特别的选项集,只能有一个实例供所有客户端共享。针对选项参数的修改必须通过一个SharedPreferences.Editor对象来进行,以保证所有的选项值保持在一个始终如一的状态,并且通过该对象提交存储。
可见,SharedPreferences操作选项文件时是线程安全的。
Ⅱ android里的sqlite数据库如何加密加密后如何读取
这个md5一下就可以了吧
Ⅲ android系统里,通讯录数据库中的手机号码加密过吗
写过一些取电话号码的东西,没有出现过乱码。你看看是不是字段之类的取的问题。
下面是取电话号码的一段代码,不会出乱码。你参考一下吧
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
// 取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameFieldColumnIndex);
string += (name);
// 取得联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
// 取得电话号码(可能存在多个号码)
while (phone.moveToNext())
{
String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (":" + strPhoneNumber);
}
string += "\n";
phone.close();
}
cursor.close();
Ⅳ Android 开发,如何连接带有pragma key加密的SQLlite数据库
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用SQLCipher对数据库进行加密"
android:layout_centerHorizontal="true"
Ⅳ android sqlite数据库怎么加密
按我的理解,你可以做一个加密解密的函数,数据存进数据库之前都加密一下,取出来的时候再解密,这样就算别人看到了数据库里的数据,也完全不知道是什么意思 查看原帖>>
Ⅵ android编程中,怎样更改sqlcipher加密数据库的密码呢
貌似不是商用加密算法,应该是自己定义的
解密很麻烦的,需要大量样本,不然就得猜了
给你点提示,你这个算法密文长度是明文长度的两倍,应该是由明文ASCII码运算得出的
Ⅶ android把加密算法放在so里面 怎么办
1.比如我现在在用net.sqlcipher.database 这个加密库(网上能搜得到的,用于数据库加密)。 那么我现在就在项目用加载这个jar包(在你的项目单击右键-》属性-》Java Build Path-》Libraries-》Add Jars,选择提供给你的jar包,我这里是 sqlcipher.jar,然后在Order and Export勾选你刚刚加载的 jar包。)
2.打开你的workspace目录,在你的项目目录下创建一个文件夹libs(如果文件夹不存在的话),然后将提供给你的so库放入该目录,基本架构就算是搭建好了。
3.进行开发,这里你需要问一下提供给你jar包的厂家,基本的用法,否则的话是无法进行开发的,因为你都不知道怎么去用。 sqlcipher的基本用法是:
SQLiteDatabase.loadLibs(this); //加载 so库文件,你的厂家的方法应该也是类似。
File databaseFile = getDatabasePath(SQLite_toll.DATABASE_NAME);
databaseFile.mkdirs();
databaseFile.delete();
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, helper_SharedPreferences.get_str_sp("database_cipher",this), null);
SQLite_toll initToll = new SQLite_toll(this, avaSys);
initToll.onCreate(database);
database.close();
//因为我sqlcipher是用于数据库加密的,所以你所看到的都是数据库的一些方法,你厂家提供给你的jar包的用法,你是要去问他们的,或者他们的是否有开源代码,又或者是网上也有很多人使用,那么能搜到相关资料。
根据你补充的提问,那么就是System.loadLibrary(this); ,就可以调用了
Ⅷ 在android中如何打开加密过的sqlite数据库
在ANDROID中,应用的数据是私有的,你要得到其他应用的数据,可以通过ContentProvider来实现。
Ⅸ 加密/解密Android现有的数据库使用SQLCipher问题,怎么解决
针对sqlite数据库文件,进行加密。现有两种方案如下:
1.对数据库中的数据进行加密。
2.对数据库文件进行加密
1.uin怎么获取?
这个uin不是登录的帐号,而是属于内部的、程序界面上不可见的一个编号。
至于查看,最简单的方法就是登录web微信后,按F12打开网页调试工具,然后ctrl+F搜索“uin”,可以找到一串长长的URL,里面的uin就是当前登录的微信的uin。
还
有一种方法就是配置文件里,导出的微信目录下有几个cfg文件,这几个文件里有保存,不过是java的hashmap,怎么解析留给小伙伴们自己琢磨吧,
还有就是有朋友反应退出微信(后台运行不叫退出)或者注销微信后会清空这些配置信息,所以小伙伴们导出的时候记得在微信登陆状态下导出。博主自己鼓捣了一
个小程序来完成解析。
2.一个手机多个登录帐号怎么办(没有uin怎么办)
根
据博主那个解密的帖子,必须知道串号和uin。串号好说,配置中一般都有可以搞到,uin从配置中读取出来的时候只有当前登录的或者最后登录的,其他的几
个记录都没办法解密。网上某软件的解决方法是让用户一个一个登录后再导出。这个解决方法在某些情况下是不可能的,或者有时候根本不知道uin。
后来经过一个朋友的指点,博主终于发现了解决方法,可以从配置中秒读出来这个uin,这个方法暂时不透漏了,只是说明下这个异常情况。
3.串号和uin怎么都正确的怎么还是没办法解密
先
说说串号这个玩意,几个热心的朋友反馈了这个问题,经过博主测试发现不同的手机使用的不一定是IMEI,也可能是IMSI等等,而且串号也不一定是标准的
15位,可能是各种奇葩,比如输入*#06#出来的是一个,但是在微信程序里用的却是另一个非常奇葩的东西,这种情况多在双卡双待和山寨机中出现,经过严
格的测试,现在已经能做到精确识别,那几位热心的朋友也赠与了不同的代码表示鼓励。
4.计算出来了正确的key为什么无法打开数据库文件
微
信这个变态用的不是标准的sqlite数据库,那个帖子也提到了不是数据库加密,是文件的内容加密,其实是sqlcipher。官方上竟然还卖到
149$,不过倒是开放了源码,水平够高的可以自己尝试编译。google还能搜索到sqlcipher for
windows这个很好编译,不过博主不知是长相问题还是人品问题,编译出来的无法打开微信的数据库,后来改了这份代码才完成。
5.数据库文件内容是加密的,怎么还原
这
个是某些特殊情况下用到的,比如聊天记录删除了数据库中就没了,但是某个网友测试说数据库无法查询出来了,但是在文件中还是有残留的。这个情况我没测试
过,不过想想感觉有这个可能,就跟硬盘上删除了文件其实就是删除了文件的硬盘索引,内容还是残留在硬盘上可以还原一样,sqlite数据库删除的条目只是
抹去了索引,内容还存在这个文件中。
网上的都是直接打开读取,并没有解密还原这个文件成普通的sqlite数据库,使用sqlcipher
的导出方法也只是将可查询的内容导出。后来博主花了时间通读了内容加密的方式,做了一个小程序将加密的文件内容直接解密,不操作修改任何数据,非数据库转
换,直接数据流解密,完全还原出来了原始的未加密的数据库文件,大小不变,无内容损失,可以直接用sqlite admin等工具直接打开。
6.已经删除的聊天内容可以恢复么
通过上述第5的方式还原出原数据后,经测试可以恢复。sqlite的删除并不会从文件中彻底删掉,而是抹掉索引,所以可以通过扫描原始文件恢复。前提是没有重装过微信。。。