maven包:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
Java实现:
public class AESUtil {
private static byte[] encrypt(byte[] text, byte[] key) throws Exception {
SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
return cipher.doFinal(text);
}
private static byte[] decrypt(byte[] text, byte[] key) throws Exception {
SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
return cipher.doFinal(text);
}
/**
* @param text 明文
* @param key 密钥
* @desc 加密
*/
public static String encodeAES(byte[] text, byte[] key) throws Exception {
byte[] keybBytes = DigestUtils.md5(key);
byte[] passwdBytes = text;
byte[] aesBytyes = encrypt(passwdBytes, keybBytes);
return new String(Base64.encodeBase64(aesBytyes));
}
/**
* @param password 密文
* @param key 密钥
* @desc 解密
*/
public static String deCodeAES(String password, String key) throws Exception {
byte[] keybBytes = DigestUtils.md5(key);
byte[] debase64Bytes = Base64.decodeBase64(password.getBytes());
return new String(decrypt(debase64Bytes, keybBytes));
}
}