import org.apache.commons.codec.binary.Hex;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author jacky
* AES128 算法
* CBC 模式
* NoPadding 无填充模式
* CBC模式需要添加一个参数iv
*
* 介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别
* 要实现在java端用PKCS7Padding填充,需要用到bouncycastle组件来实现
*/
public class AesEncoder {
/*
* 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
*/
private byte[] ivParameter = {0x68, 0x7c, 0x39, 0x30, 0x2a, 0x71, 0x51, 0x5e, 0x0f, 0x13, 0x2a, 0x76, 0x4d, 0x76, 0x61, 0x7b};
public static byte[] cSrc = {0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
private static AesEncoder instance = null;
private AesEncoder() {
}
public static AesEncoder getInstance() {
if (instance == null)
instance = new AesEncoder();
return instance;
}
// 加密
public byte[] encrypt(byte[] sSrc, String sKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
return cipher.doFinal(sSrc);
}
// 解密
public String decrypt(byte[] sSrc, String sKey) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
IvParameterSpec iv = new IvParameterSpec(ivParameter);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(sSrc);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static void main(String[] args) throws Exception {
// 需要加密的字串
byte[] cSrc = {0x12,0x34,0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
//String cSrc = "123456789";
/* System.out.print("加密前的字串是:");
for (byte b : cSrc) {
System.out.print(b);
}*/
System.out.println();
// 加密
byte[] enString = AesEncoder.getInstance().encrypt(cSrc,"12345678901234561234567890123456");
System.out.println("加密后的字串长度是:" + enString.length);
for (byte b : enString) {
System.out.printf("%02x ",b);
}
byte[] bSrc = {0x12,0x34,0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
System.out.println("数组是否相等:"+Arrays.equals(enString,bSrc));
String desc = AesEncoder.getInstance().decrypt(enString,"12345678901234561234567890123456");
System.out.println("解密后的字串是:" + desc);
}
}
注:由128改成256只要把密钥长度由16改成32就好