【Android】AES加密算法的Android实现


1
import android.text.TextUtils; 2 import android.util.Base64; 3 import android.util.Log; 4 5 import javax.crypto.Cipher; 6 import javax.crypto.spec.IvParameterSpec; 7 import javax.crypto.spec.SecretKeySpec; 8 9 public class AESCBCUtils { 10 private static final String TAG = "AESCBCUtils"; 11 12 // CBC(Cipher Block Chaining, 加密快链)模式,PKCS5Padding补码方式 13 // AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式 14 /** 15 * 加解密算法/工作模式/填充方式 16 */ 17 private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding"; 18 // AES 加密 19 private static final String AES = "AES"; 20 21 // 密钥偏移量 22 //private static final String mstrIvParameter = "1234567890123456"; 23 /* key必须为16位,可更改为自己的key */ 24 //String mstrTestKey = "1234567890123456"; 25 26 // 加密 27 /** 28 * AES 加密 29 * 30 * @param strKey 加密密钥 31 * @param strClearText 待加密内容 32 * @param mstrIvParameter 密钥偏移量 33 * @return 返回Base64转码后的加密数据 34 */ 35 public static String encrypt_AES(String strKey, String strClearText, String mstrIvParameter){ 36 37 try { 38 byte[] raw = strKey.getBytes(); 39 // 创建AES密钥 40 SecretKeySpec skeySpec = new SecretKeySpec(raw, AES); 41 // 创建密码器 42 Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING); 43 // 创建偏移量 44 IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes()); 45 // 初始化加密器 46 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); 47 // 执行加密操作 48 byte[] cipherText = cipher.doFinal(strClearText.getBytes()); 49 //Log.d(TAG, "encrypt result(not BASE64): " + cipherText.toString()); 50 String strBase64Content = Base64.encodeToString(cipherText, Base64.DEFAULT); // encode it by BASE64 again 51 //Log.d(TAG, "encrypt result(BASE64): " + strBase64Content); 52 strBase64Content = strBase64Content.replaceAll(System.getProperty("line.separator"), ""); 53 54 return strBase64Content; 55 } catch (Exception e) { 56 e.printStackTrace(); 57 } 58 59 return null; 60 } 61 62 // 解密 63 /** 64 * AES 解密 65 * 66 * @param strKey 解密密钥 67 * @param strCipherText 待解密内容 68 * @param mstrIvParameter 偏移量 69 * @return 返回Base64转码后的加密数据 70 */ 71 public static String decrypt(String strKey, String strCipherText, String mstrIvParameter) throws Exception { 72 73 try { 74 byte[] raw = strKey.getBytes("ASCII"); 75 // 创建AES秘钥 76 SecretKeySpec skeySpec = new SecretKeySpec(raw, AES); 77 // 创建密码器 78 Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING); 79 // 创建偏移量 80 IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes()); 81 // 初始化解密器 82 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); 83 // 执行解密操作 84 byte[] cipherText = Base64.decode(strCipherText, Base64.DEFAULT); // decode by BASE64 first 85 //Log.d(TAG, "BASE64 decode result(): " + cipherText.toString()); 86 byte[] clearText = cipher.doFinal(cipherText); 87 String strClearText = new String(clearText); 88 //Log.d(TAG, "decrypt result: " + strClearText); 89 90 return strClearText; 91 } catch (Exception e) { 92 e.printStackTrace(); 93 } 94 95 return null; 96 } 97 }

 

【Android】AES加密算法的Android实现

上一篇:Java程序设计案例教程


下一篇:Android安卓实现mqtt客户端