JojoMiss

疯魔jojo的后花园

导航

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");

 

posted on 2024-04-01 13:19  幽忧一世  阅读(4)  评论(0编辑  收藏  举报

⭐️CSDN 我的文章列表⭐️
⭐WEIBO 微博首页⭐️
⭐园子 我的院子⭐️