AES 对称加密 (JDK)
程序员文章站
2022-05-15 16:59:49
...
/**
* AES 加密 解密
*/
//key生成
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
//长度有 64 112 128 192 256
keyGenerator.init(256);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encoded = secretKey.getEncoded();
//key转换
SecretKeySpec keySpec = new SecretKeySpec(encoded, "AES");
//加密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] bytes = cipher.doFinal("加密字符串".getBytes());
System.out.println(Base64.encodeBase64String(bytes));
//解密
cipher.init(Cipher.DECRYPT_MODE, keySpec);
bytes = cipher.doFinal(bytes);
System.out.println(new String(bytes));
上一篇: PHP 非对称加密实现