a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android AES加密工具類分享

2020-04-11 11:42:37
字體:
來源:轉載
供稿:網友

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";  ///** 創建密鑰 **/ 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"); }  // /** 加密字節數據 **/ 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; }  ///** 加密(結果為16進制字符串) **/ 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; }  // /** 解密字節數組 **/ 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進制的字符串為字符串 **/ 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; }  // /** 字節數組轉成16進制字符串 **/ public static String byte2hex(byte[] b) { // 一個字節的數, StringBuffer sb = new StringBuffer(b.length * 2); String tmp = ""; for (int n = 0; n < b.length; n++) {  // 整數轉成十六進制表示  tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  if (tmp.length() == 1) {  sb.append("0");  }  sb.append(tmp); } return sb.toString().toUpperCase(); // 轉成大寫 }  // /** 將hex字符串轉換成字節數組 **/ 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("加密結果為 " + encryptingCode); 	  Log.i("加密結果為 ",encryptingCode); 	  String decryptingCode = AESHelper.decrypt(encryptingCode,masterPassword); //	  System.out.println("解密結果為 " + decryptingCode); 	  Log.i("解密結果",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、打印結果

09-19 10:41:05.467: I/加密結果為(707): E55C24701F6380478E1940ADDFD08D2209-19 10:41:05.467: I/解密結果(707): 于
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四虎永久在线 | 欧美日韩精品综合 | 人人草视频在线观看 | 一区毛片 | 久久久99精品免费观看 | 欧美一区二区视频 | 99re6在线视频精品免费 | 一区二区三区高清 | 成人在线看片 | 激情久久五月天 | 亚洲激情视频在线观看 | 男女小网站 | 欧美一级片免费观看 | 久久电影中文字幕 | 91视频专区 | 成人黄色91 | 国产精品99久久久久久大便 | 日韩一区二区中文字幕 | 欧美视频亚洲视频 | 婷婷色5月| 成人免费的视频 | 欧美精品一区在线 | 欧美一区二区在线免费观看 | 午夜不卡视频 | 精品国产一区二区三区性色 | 色综合99| 成人精品久久久 | 欧美三级视频在线观看 | 91黄色在线观看 | 国产精品久久久久久久久 | 亚洲视频一区 | 一区久久 | 精品欧美一区二区在线观看视频 | 久久极品| 日韩久久午夜一级啪啪 | 亚洲一区二区高清视频 | 一区二区成人网 | 四虎影院免费网址 | 免费av片网站| 九九热精品在线 | 一级久久久|