AES加密&解密
1、加密
java.util.Base64;
javax.crypto.Cipher;
javax.crypto.spec.SecretKeySpec;
// 入参:data(String)、seed(String)
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKeySpec;
byte[] bytes;
bytes = seed.getBytes("utf-8");
secretKeySpec = new SecretKeySpec(bytes, "AES");
cipher.init(1, secretKeySpec);
return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes("utf-8")));
2、解密
java.util.Base64;
javax.crypto.spec.SecretKeySpec;
javax.crypto.Cipher;
/*
入参:
data
seed
*/
byte[] bytes;
SecretKeySpec secretKeySpec;
bytes = seed.getBytes("utf-8");
secretKeySpec = new SecretKeySpec(bytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, secretKeySpec);
return new String(cipher.doFinal(Base64.getDecoder().decode(data)), "utf-8");
本文来自博客园,作者:幽忧一世,转载请注明原文链接:https://www.cnblogs.com/JojoMiss/p/18108216