Android AES加密工具類,androidaes工具類

來源:互聯網
上載者:User

Android AES加密工具類,androidaes工具類

1、AES加密工具類

java不支援PKCS7Padding,只支援PKCS5Padding。我們知道密碼編譯演算法由演算法+模式+填充組成,,本篇文章使用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; }}

使用~~~~~~~~~~~~~~~~··

     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();      }      }

結果

加密結果為(707): E55C24701F6380478E1940ADDFD08D22

解密結果( 707 ): 於

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.