Android AES加密工具類分享
1、AES加密工具類
java不支持PKCS7Padding,只支持PKCS5Padding。我們知道加密算法由算法+模式+填充組成,下一篇介紹iOS和Android通用的AES加密,本篇文章使用PKCS5Padding加密方式。
package com.example.aesdemo;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
///** AES對稱加密解密類 **/
public class AESHelper {
// /** 算法/模式/填充 **/
private static final String CipherMode = "AES/ECB/PKCS5Padding";
///** 創(chuàng)建密鑰 **/
private static SecretKeySpec createKey(String password) {
byte[] data = null;
if (password == null) {
password = "";
}
StringBuffer sb = new StringBuffer(32);
sb.append(password);
while (sb.length() < 32) {
sb.append("0");
}
if (sb.length() > 32) {
sb.setLength(32);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
// /** 加密字節(jié)數(shù)據(jù) **/
public static byte[] encrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
System.out.println(key);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
///** 加密(結(jié)果為16進(jìn)制字符串) **/
public static String encrypt(String content, String password) {
byte[] data = null;
try {
data = content.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
data = encrypt(data, password);
String result = byte2hex(data);
return result;
}
// /** 解密字節(jié)數(shù)組 **/
public static byte[] decrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
///** 解密16進(jìn)制的字符串為字符串 **/
public static String decrypt(String content, String password) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, password);
if (data == null)
return null;
String result = null;
try {
result = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
// /** 字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制字符串 **/
public static String byte2hex(byte[] b) { // 一個字節(jié)的數(shù),
StringBuffer sb = new StringBuffer(b.length * 2);
String tmp = "";
for (int n = 0; n < b.length; n++) {
// 整數(shù)轉(zhuǎn)成十六進(jìn)制表示
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase(); // 轉(zhuǎn)成大寫
}
// /** 將hex字符串轉(zhuǎn)換成字節(jié)數(shù)組 **/
private static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}
}
2、使用
新建Android工程
package com.example.aesdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.util.Log;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String masterPassword = "a";
String originalText = "于";
try {
String encryptingCode = AESHelper.encrypt(originalText,masterPassword);
// System.out.println("加密結(jié)果為 " + encryptingCode);
Log.i("加密結(jié)果為 ",encryptingCode);
String decryptingCode = AESHelper.decrypt(encryptingCode,masterPassword);
// System.out.println("解密結(jié)果為 " + decryptingCode);
Log.i("解密結(jié)果",decryptingCode);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3、打印結(jié)果
09-19 10:41:05.467: I/加密結(jié)果為(707): E55C24701F6380478E1940ADDFD08D22 09-19 10:41:05.467: I/解密結(jié)果(707): 于
- Android封裝的http請求實(shí)用工具類
- Android實(shí)用的Toast工具類封裝
- Android工具欄頂出轉(zhuǎn)場動畫的實(shí)現(xiàn)方法實(shí)例
- Android 數(shù)據(jù)存儲之 FileInputStream 工具類及FileInputStream類的使用
- android自動工具類TextUtils使用詳解
- android實(shí)用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
- android開發(fā)教程之實(shí)現(xiàn)toast工具類
- Android開發(fā)中日期工具類DateUtil完整實(shí)例
- Android動畫工具類的封裝實(shí)戰(zhàn)記錄
相關(guān)文章
安卓開發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解
這篇文章主要介紹了安卓開發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08
Android實(shí)現(xiàn)動態(tài)高斯模糊背景效果
在現(xiàn)代 Android UI 中,動態(tài)高斯模糊背景 常見于對話框或彈窗后面的模糊遮罩,相比靜態(tài)模糊圖,動態(tài)模糊可隨著內(nèi)容滾動或變化實(shí)時更新,使界面更具層次感與沉浸感,所以本文給大家介紹了Android實(shí)現(xiàn)動態(tài)高斯模糊背景效果,需要的朋友可以參考下2025-04-04
Android中選項(xiàng)菜單(OptionMenu)的創(chuàng)建方法
這篇文章主要介紹了Android中選項(xiàng)菜單(OptionMenu)的創(chuàng)建方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01
Android App開發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫廊的實(shí)例
這篇文章主要介紹了Android App開發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫廊的實(shí)例,比普通的ListView實(shí)現(xiàn)的效果更為強(qiáng)大,需要的朋友可以參考下2016-04-04
Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細(xì)介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android播放assets文件里視頻文件相關(guān)問題分析
這篇文章主要介紹了Android播放assets文件里視頻文件相關(guān)問題分析,結(jié)合Android播放assets文件出現(xiàn)錯誤的實(shí)際問題給出了原因分析與解決方法參考,需要的朋友可以參考下2016-08-08
Android SQLite數(shù)據(jù)庫基本操作方法
本篇文章主要介紹了Android SQLite數(shù)據(jù)庫基本操作方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android XmlResourceParser出錯解決辦法
這篇文章主要介紹了Android XmlResourceParser出錯解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05

