aes/cbc/pkcs5padding加密代码
时间: 2024-10-26 09:03:33 浏览: 25
AES/CBC/PKCS5PADDING/128加密解密
5星 · 资源好评率100%
以下是Java实现AES/CBC/PKCS5Padding加密的代码实例:
```
import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESEncrypter {
private static final String IV_PARAMETER = "1234567890123456"; // 16 bytes IV
private static final String ENCODING = "UTF-8";
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String SECRET_KEY = "0123456789123456"; // 16 bytes secret key
public static String encrypt(String plainText) {
try {
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(ENCODING));
SecretKeySpec skeySpec = new SecretKeySpec(SECRET_KEY.getBytes(ENCODING), "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return new String(Base64.encodeBase64(encrypted), ENCODING);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
```
请注意,这只是一个简单的例子,仅供参考。如果要在生产环境中使用,需要更多的安全措施和更好的错误处理。
阅读全文