异常 java.security.InvalidKeyException:无效的密钥长度:使用 DESede 的 24 字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22951606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception java.security.InvalidKeyException: Invalid key length: 24 bytes using DESede
提问by nitinkt
I know that this question is often asked but I have checked everything I found in Stack Overflow and did not find the solution to my problem.
i am using DESede for encryption and decryption and taking external 24 byte key input. but getting exception.
我知道这个问题经常被问到,但我已经检查了我在 Stack Overflow 中找到的所有内容,但没有找到我的问题的解决方案。
我正在使用 DESede 进行加密和解密,并采用外部 24 字节密钥输入。但得到例外。
here is my code:
这是我的代码:
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();
}
}
//method for encryption
//加密方法
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;
}
//method for decryption
//解密方法
private static void decryptData(byte[] data)
throws IllegalBlockSizeException, BadPaddingException {
byte[] textDecrypted = decryptCipher.doFinal(data);
System.out.println("Decryted Data: " + new String(textDecrypted));
}
}
i am getting exception at the line: java.security.InvalidKeyException: Invalid key length: 24 bytes
我在该行遇到异常:java.security.InvalidKeyException:无效的密钥长度:24 字节
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
any one have any idea why this is happening?
有人知道为什么会这样吗?
采纳答案by lakshman
There is two mistakes you have done in your code
您在代码中犯了两个错误
you use DESede
to create the secret key factory in this line
您用于DESede
在此行中创建密钥工厂
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
but you use the DES
to get the Cipher
object. you have to use DESede
instead
但是您使用DES
来获取Cipher
对象。你必须DESede
改用
so use this line
所以使用这条线
encryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
encryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
instead of this line
而不是这条线
encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
use the same algorithm to get decripting cipher
使用相同的算法来获得解密密码
and another one is use a AlgorithmParameterSpec
to init the decrypting cipher.
另一个是使用 aAlgorithmParameterSpec
来初始化解密密码。
byte iv[] = encryptCipher.getIV();
IvParameterSpec dps = new IvParameterSpec(iv);
decryptCipher.init(Cipher.DECRYPT_MODE, key, dps);
you can use above code to apply the AlgorithmParameterSpec
to the initialization of cipher
您可以使用上面的代码将 应用于AlgorithmParameterSpec
密码的初始化