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對(duì)稱加密解密類 **/ 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) { // 一個(gè)字節(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請(qǐng)求實(shí)用工具類
- Android實(shí)用的Toast工具類封裝
- Android工具欄頂出轉(zhuǎn)場動(dòng)畫的實(shí)現(xiàn)方法實(shí)例
- Android 數(shù)據(jù)存儲(chǔ)之 FileInputStream 工具類及FileInputStream類的使用
- android自動(dòng)工具類TextUtils使用詳解
- android實(shí)用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
- android開發(fā)教程之實(shí)現(xiàn)toast工具類
- Android開發(fā)中日期工具類DateUtil完整實(shí)例
- Android動(dòng)畫工具類的封裝實(shí)戰(zhàn)記錄
相關(guān)文章
完美解決客戶端webview持有的頁面緩存,不會(huì)立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會(huì)立即釋放的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12快速調(diào)試Android應(yīng)用系統(tǒng)修改ro.debuggable屬性的兩種方式
這篇文章主要為大家介紹了快速調(diào)試Android應(yīng)用系統(tǒng)修改ro.debuggable屬性的兩種方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10建造者模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
建造者實(shí)現(xiàn)抽象類的所有未實(shí)現(xiàn)的方法,具體來說一般是兩項(xiàng)任務(wù),組建產(chǎn)品;返回組建好的產(chǎn)品2017-08-08Android利用SoundPool實(shí)現(xiàn)音樂池
這篇文章主要為大家詳細(xì)介紹了Android利用SoundPool實(shí)現(xiàn)音樂池,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android開發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果,結(jié)合完整實(shí)例形式分析了Android獲取通訊錄列表及采用自定義view排列顯示的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07android實(shí)現(xiàn)圖片上傳功能(springMvc)
這篇文章主要為大家詳細(xì)介紹了android結(jié)合springMvc實(shí)現(xiàn)圖片上傳的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android關(guān)于BottomNavigationView使用指南
本文主要介紹了Android關(guān)于BottomNavigationView使用指南,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01