1. 程式人生 > >Aes加密(Android與Java後臺可以正常加解密 )

Aes加密(Android與Java後臺可以正常加解密 )

本文采用的加密模式是AES-128-CBC,這種加密模式可以自己定義“金鑰”和“偏移量“。

程式碼如下,Android端及Java後臺都可以直接使用

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * AES 是一種可逆加密演算法,對使用者的敏感資訊加密處理 對原始資料進行AES加密後,在進行Base64編碼轉化;
 */
public class AesUtil { /* * 加密用的Key 可以用26個字母和數字組成 此處使用AES-128-CBC加密模式,key需要為16位。 */ private String sKey = "1234567890123456";//key,加密的key private String ivParameter = "1201230125462244";//偏移量,4*4矩陣 private static AesUtil instance = null; private AesUtil() { } public
static AesUtil getInstance() { if (instance == null) instance = new AesUtil(); return instance; } /** * 加密 * @param encData 要加密的內容 * @param secretKey 加密的祕鑰 * @param vector 偏移量 * @return 加密後的字串 * @throws Exception */ public static
String encrypt(String encData ,String secretKey,String vector) throws Exception { if(secretKey == null) { return null; } if(secretKey.length() != 16) { return null; } Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] raw = secretKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8")); return new BASE64Encoder().encode(encrypted).replaceAll("\r|\n", "");// 此處使用BASE64做轉碼。 } /** * 加密 * @param sSrc 要加密的內容 * @return 加密後的字串 * @throws Exception */ public String encrypt(String sSrc) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] raw = sKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return new BASE64Encoder().encode(encrypted).replaceAll("\r|\n", "");// 此處使用BASE64做轉碼。 } /** * 解密 * @param sSrc 要解密的內容 * @return 解密後的字串 * @throws Exception */ public String decrypt(String sSrc) throws Exception { try { byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密 byte[] original = cipher.doFinal(encrypted1); return new String(original, "utf-8"); } catch (Exception ex) { return null; } } /** * 解密 * @param sSrc 要解密的內容 * @param key 解密要祕鑰 * @param ivs 偏移量 * @return 解密後的字串 * @throws Exception */ public String decrypt(String sSrc,String key,String ivs) throws Exception { try { byte[] raw = key.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(ivs.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密 byte[] original = cipher.doFinal(encrypted1); return new String(original, "utf-8"); } catch (Exception ex) { return null; } } }

其中Android端sun.misc.BASE64Encoder,和sun.misc.BASE64Decoder兩個包是沒有的,需要額外匯入lib包,Java後臺則不需要。