gpt4 book ai didi

java - AES加密抛出 "Illegal Key Size error"

转载 作者:行者123 更新时间:2023-12-02 05:45:11 25 4
gpt4 key购买 nike

我正在尝试使用 AES 加密。下面是我的代码片段:

private final static String ENCRYPTION_ALGORITHM = "AES";
private final static String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
private final static String ENCODING = "UTF-8";
private final static String KEY_STRING = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";

public static String encryptData(String plainText) {

String keyString = KEY_STRING;
String encryptedValue = null;

byte[] keyValue = DatatypeConverter.parseHexBinary(keyString);
Key key = new SecretKeySpec(keyValue, ENCRYPTION_ALGORITHM);
Cipher cipher;
byte[] encVal;

try {
cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(new byte[64]));
encVal = cipher.doFinal(plainText.getBytes());
encryptedValue = (new BASE64Encoder()).encode(encVal);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
System.out.println("Error in EncryptData.encryptData(): " + e.getMessage());
encryptedValue = null;
}


return encryptedValue;
}

下面是我在 eclipse 中运行时遇到的错误:

Error in EncryptData.encryptData(): Illegal key size

有什么建议或者我做错了什么吗?

最佳答案

您是否已更新策略以允许 256 位加密?检查来自 http://www.oracle.com/technetwork/java/javase/downloads/index.html 的“Java 加密扩展 (JCE) 无限强度管辖权策略文件 6” .

关于java - AES加密抛出 "Illegal Key Size error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24138402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com