gpt4 book ai didi

java - 异常 java.security.InvalidKeyException : Invalid key length: 24 bytes using DESede

转载 作者:行者123 更新时间:2023-11-30 11:20:01 25 4
gpt4 key购买 nike

我知道这个问题经常被问到,但我已经检查了我在 Stack Overflow 中找到的所有内容,但没有找到解决我的问题的方法。
我正在使用 DESede 进行加密和解密,并采用外部 24 字节 key 输入。但出现异常。

这是我的代码:

    import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.DESedeKeySpec;
import javax.xml.bind.DatatypeConverter;

public class DESede {

private static Cipher encryptCipher;
private static Cipher decryptCipher;

public static void main(String[] args) throws InvalidKeySpecException {
try {


String desKey = "0123456789abcdef0123456789abcdef0123456789abcdef"; // value from user
byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);
System.out.println((int)keyBytes.length);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
SecretKey key = factory.generateSecret(new DESedeKeySpec(keyBytes));

encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key); //throwing Exception
byte[] encryptedData = encryptData("Confidential data");

decryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
decryptData(encryptedData);

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}

}

//加密方法

     private static byte[] encryptData(String data)
throws IllegalBlockSizeException, BadPaddingException {
System.out.println("Data Before Encryption :" + data);
byte[] dataToEncrypt = data.getBytes();
byte[] encryptedData = encryptCipher.doFinal(dataToEncrypt);
System.out.println("Encryted Data: " + encryptedData);

return encryptedData;
}

//解密方法

     private static void decryptData(byte[] data)
throws IllegalBlockSizeException, BadPaddingException {
byte[] textDecrypted = decryptCipher.doFinal(data);
System.out.println("Decryted Data: " + new String(textDecrypted));
}
}

我在以下行遇到异常:java.security.InvalidKeyException:无效 key 长度:24 字节

encryptCipher.init(Cipher.ENCRYPT_MODE, key);

有人知道为什么会这样吗?

最佳答案

你的代码有两个错误

您使用 DESede 在此行中创建 key 工厂

 SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");

但是您使用DES 来获取Cipher 对象。你必须改用 DESede

所以使用这一行

encryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

代替这一行

encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

使用相同的算法得到解密密码

另一个是使用AlgorithmParameterSpec 初始化解密密码。

byte iv[] = encryptCipher.getIV(); 
IvParameterSpec dps = new IvParameterSpec(iv);
decryptCipher.init(Cipher.DECRYPT_MODE, key, dps);

您可以使用上面的代码将AlgorithmParameterSpec应用于密码的初始化

关于java - 异常 java.security.InvalidKeyException : Invalid key length: 24 bytes using DESede,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22951606/

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