gpt4 book ai didi

java - AES 解密/加密为 256 位 key 抛出 'Illegal key size'

转载 作者:行者123 更新时间:2023-11-29 04:40:21 25 4
gpt4 key购买 nike

<分区>

我最近开始尝试使用 AES 加密/解密。我的第一个实验来自 here并稍作修改。

然后我尝试生成一个加密字符串,然后再次解密,使用 16 位 IV 和 256 位密码(示例中使用的原始密码是 128 位,这对我有用)。我还比较了 this post并没有真正看到区别

如果我尝试运行下面提供的简单代码,我会不断收到此异常:

java.security.InvalidKeyException: Illegal key size

我的代码:

public static void main(String[] args) throws Exception {
// byte [] key = "Bar12345Bar12345".getBytes("UTF-8"); // 128 bit key
byte[] key = Hex.decode("8ec8f262e96e3d80ef52b530a5bc7b7baaf6e4357a363119b0a636b2034e298e");
byte[] iv = Hex.decode("a5e8d2e9c1721ae0e84ad660c472c1f3");
System.out.print(Arrays.toString(key));
System.out.println(key.length);
System.out.print(Arrays.toString(iv));
System.out.println(iv.length);

System.out.println(decrypt(key, iv, encrypt(key, iv, "Hello World")));

}

public static String encrypt(byte[] key, byte[] initVector, String value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");/// CBC/PKCS5PADDING
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

byte[] encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}

return null;
}

public static String decrypt(byte[] key, byte[] initVector, String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}

return null;
}

完整输出:

[-114, -56, -14, 98, -23, 110, 61, -128, -17, 82, -75, 48, -91, -68, 123, 123, -86, -10, -28, 53, 122, 54, 49, 25, -80, -90, 54, -78, 3, 78, 41, -114]32
[-91, -24, -46, -23, -63, 114, 26, -32, -24, 74, -42, 96, -60, 114, -63, -13]16
java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1034)
at javax.crypto.Cipher.implInit(Cipher.java:800)
at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
at javax.crypto.Cipher.init(Cipher.java:1370)
at javax.crypto.Cipher.init(Cipher.java:1301)
at TestMain.encrypt(TestMain.java:76)
at TestMain.main(TestMain.java:66)
java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1034)
at javax.crypto.Cipher.implInit(Cipher.java:800)
at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
at javax.crypto.Cipher.init(Cipher.java:1370)
at javax.crypto.Cipher.init(Cipher.java:1301)
at TestMain.decrypt(TestMain.java:95)
at TestMain.main(TestMain.java:66)
null

请注意,输出中最后的“null”是在 main 的最后一行打印到 System.out 的操作结果。

另请注意,如果我将 key 变量与注释行交换,(示例中使用的原始 128 位 key )此代码工作正常。

对于抛出此异常的原因以及如何解决这个问题有什么想法吗?

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