當前位置:首頁 » 密碼管理 » android加密資料庫

android加密資料庫

發布時間: 2022-04-23 08:46:47

Ⅰ 怎麼測試給資料庫加密的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的刪除並不會從文件中徹底刪掉,而是抹掉索引,所以可以通過掃描原始文件恢復。前提是沒有重裝過微信。。。

熱點內容
微信忘記密碼從哪裡看 發布:2024-05-19 16:06:37 瀏覽:32
寶馬x4貸款買哪個配置好 發布:2024-05-19 15:56:03 瀏覽:22
微控pid演算法 發布:2024-05-19 15:46:31 瀏覽:135
雲盤視頻解壓密碼 發布:2024-05-19 15:23:17 瀏覽:848
和平精英怎麼改地區位置安卓 發布:2024-05-19 15:19:05 瀏覽:286
酒店的路由器如何配置 發布:2024-05-19 15:10:44 瀏覽:500
rpgmaker腳本 發布:2024-05-19 14:48:58 瀏覽:407
hds存儲虛擬化 發布:2024-05-19 14:47:09 瀏覽:21
mysql資料庫分片 發布:2024-05-19 14:42:30 瀏覽:342
2021款魏派vv6買哪個配置 發布:2024-05-19 14:31:11 瀏覽:633