gpt4 book ai didi

使用 32 字节 key 的 Java AES 加密 - key 大小无效

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:03 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
InvalidKeyException Illegal key size

public static byte[] encryptBytes(byte[] bytes, byte[] key)
{
Cipher cipher = null;

try
{
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);

return Base64.encodeBase64(cipher.doFinal(bytes));
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}

public static byte[] decrpytBytes(byte[] encryptedData, String key)
{
byte[] keyBytes = convertToByteArray(key);
Cipher cipher = null;

try
{
cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);

return cipher.doFinal(Base64.decodeBase64(encryptedData));
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}
//Simply takes every other two characters an terms them into a byte value
//then stuffs them into a byteArray
public static byte[] convertToByteArray(String key)
{
byte[] b = new byte[key.length()/2];

for(int i=0, bStepper=0; i<key.length()+2; i+=2)
if(i !=0)
b[bStepper++]=((byte) Integer.parseInt((key.charAt(i-2)+""+key.charAt(i-1)), 16));

return b;
}

public static void main(String[] args) throws Exception
{
//This string has 64 characters. When sent to convertToByteArray it returns a byte array or 32 bytes
String key = "00112233445566778899AABBCCDDEEFF0123456789ABCDEF0123456789ABCDEF";

//Test it out
byte f[] = {2,4,7};
byte[] encrypted = encryptBytes(f, convertToByteArray(key));
byte[] unencrypted = decrpytBytes(encrypted, key);

System.out.print(unencrypted[0]);
}

错误:

非法 key 大小或默认参数

我不知道为什么我得到一个无效的 key 大小。它应该能够采用 256 位加密的 32 字节 key

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