package com.simple.tool.signcheck.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class AESUtil {
private static final String DATA_ENCODING = "UTF-8";
private static final int KEY_SIZE = 128;
//AES DES
private static final String ALGORITHM = "AES";
/**
* AES/ECB/PKCS5Padding DES/CBC/PKCS5Padding
*/
private static final String ALGORITHM_PATTERN = "AES/ECB/PKCS5Padding";
public static String encrypt(String data, String key) throws Exception {
return encrypt(data, key.getBytes("UTF-8"));
}
public static String decrypt(String data, String key) throws Exception {
return decrypt(data, key.getBytes("UTF-8"));
}
/**
* base64 encode
* @param key
* @return
* @throws Exception
*/
public static String encodeBase64String(String key) throws Exception {
return Base64Util.encode(key.getBytes(DATA_ENCODING));
}
public static String generateAESKeyStr() throws Exception {
byte[] key = generateAESKey(0);
return Base64Util.encode(key);
}
public static void main(String[] args) throws Exception {
String str = "123456";
String key = "1234567890121234";
String result = AESUtil.encrypt(str, key);
System.out.println(result);
String rr = AESUtil.decrypt("j6ziIPht1g3yrTw9gDGCAg==", key);
System.out.println(rr);
}
private static byte[] generateAESKey(int keySize) throws Exception {
byte[] key = (byte[]) null;
KeyGenerator kgen = null;
if (keySize != KEY_SIZE) {
keySize = KEY_SIZE;
}
try {
kgen = KeyGenerator.getInstance(ALGORITHM);
kgen.init(keySize);
SecretKey skey = kgen.generateKey();
key = skey.getEncoded();
} catch (NoSuchAlgorithmException e) {
throw new Exception("ִAES算法异常", e);
}
return key;
}
public static String generateAESKey(String keyStr) throws Exception {
byte[] key = generateAESKey(keyStr, KEY_SIZE);
return Base64Util.encode(key);
}
private static byte[] generateAESKey(String keyStr, int keySize) throws Exception {
byte[] key = (byte[]) null;
KeyGenerator kgen = null;
if (keySize != KEY_SIZE) {
keySize = KEY_SIZE;
}
try {
kgen = KeyGenerator.getInstance(ALGORITHM);
kgen.init(keySize, new SecureRandom(keyStr.getBytes(DATA_ENCODING)));
SecretKey skey = kgen.generateKey();
key = skey.getEncoded();
} catch (NoSuchAlgorithmException e) {
throw new Exception("ִAES算法异常", e);
}
return key;
}
private static String encrypt(String data, byte[] key) throws Exception {
byte[] byteSrc = data.getBytes(DATA_ENCODING);
byte[] byteRect = encrypt(byteSrc, key);
return Base64Util.encode(byteRect);
}
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
if (key == null) {
throw new Exception("key为空");
}
SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_PATTERN);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(data);
return encrypted;
}
private static String decrypt(String data, byte[] key) throws Exception {
byte[] byteSrc = Base64Util.decode(data);
byte[] byteRect = decrypt(byteSrc, key);
return new String(byteRect, DATA_ENCODING);
}
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
if (key == null) {
throw new Exception("key为空");
}
SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_PATTERN);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(data);
return original;
}
}
如有问题,请私信。
tL+8vMr1vbvB96Osx+vO8NPD09q3x7eo08PNvqGjDQoNCsTmz/LP4LnYv86zzKGiSlOyubu3vrO/zrPMoaJKQVZBz+C52L/Os8zI59Do0qrSsr/J0tTBqs+1UVGhow0KDQrX99XfIFFRIDQwNDU0MDIyOQ==
这是一个使用JAVA代码实现AES ECB加密的工具类,提供学习和生产使用都没有问题。