InvalidKeyException java.security.InvalidKeyException:没有安装的提供程序支持这个键:(空)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26517944/
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
InvalidKeyException java.security.InvalidKeyException: No installed provider supports this key: (null)
提问by Deyaa
I have two classes, one is main class and another is the implementation of AES.
我有两个类,一个是主类,另一个是 AES 的实现。
However, in my AES class i have a method to decrypt a string, but whenever i run it, it gives an exception
但是,在我的 AES 类中,我有一种解密字符串的方法,但是每当我运行它时,它都会出现异常
My encryption method works just fine but my decryption method doesn't work as expected.
我的加密方法工作正常,但我的解密方法没有按预期工作。
The code
代码
private Cipher aesCipherForDecryption;
String strDecryptedText = new String();
public String decryptAES(final String ciphertext) {
try {
aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS5PADDING");
aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iV));
byte[] byteDecryptedText = aesCipherForDecryption.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);
} catch (IllegalBlockSizeException e) {
System.out.print("IllegalBlockSizeException " +e);
} catch (BadPaddingException e) {
System.out.print("BadPaddingException "+e);
} catch (NoSuchAlgorithmException e) {
System.out.print("NoSuchAlgorithmException "+ e);
} catch (NoSuchPaddingException e) {
System.out.print("NoSuchPaddingException "+e);
} catch (InvalidKeyException e) {
System.out.print("InvalidKeyException "+e);
} catch (InvalidAlgorithmParameterException e) {
System.out.print("InvalidAlgorithmParameterException "+e);
}
System.out.println("\nDecrypted Text message is " + strDecryptedText);
return strDecryptedText;
}
The error this method outputs is
此方法输出的错误是
InvalidKeyException java.security.InvalidKeyException: No installed provider supports this key: (null)
InvalidKeyException java.security.InvalidKeyException:没有安装的提供程序支持这个键:(空)
UPDATE #1:
更新#1:
So i have updated the code as the following
所以我更新了代码如下
public String decryptAES(byte[] ciphertext) {
String strDecryptedText = new String();
try {
byte[] byteDecryptedText = aesCipherForDecryption.doFinal(ciphertext);
strDecryptedText = new String(byteDecryptedText);
} catch (IllegalBlockSizeException e) {
System.out.print("IllegalBlockSizeException "+e);
e.printStackTrace();
} catch (BadPaddingException e) {
System.out.print("BadPaddingException "+e);
e.printStackTrace();
}
System.out.println("\nDecrypted Text message is " + strDecryptedText);
return strDecryptedText;
}
and in the main class i have this line
在主班我有这条线
byte [] byteciphertext = ciphertext.getBytes();
just to convert the string to bytes
只是将字符串转换为字节
is it correct? i'm still having
这是正确的吗?我还有
IllegalBlockSizeException javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipherjavax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
Could someone help me fix this issue?
有人可以帮我解决这个问题吗?
Thank you.
谢谢你。
回答by user207421
secretKey
is clearly null.
secretKey
很明显 null.
Other problems:
其他问题:
- You aren't using the input parameter
cipherText
. - The input parameter
cipherText
should be abyte[],
not aString
: cipher text is binary, andString
is not a container for binary data. - The result string
strDecryptedText
should be a local variable, not a member variable.
- 您没有使用输入参数
cipherText
。 - 输入参数
cipherText
应该是一个byte[],
not aString
:密文是二进制的,String
不是二进制数据的容器。 - 结果字符串
strDecryptedText
应该是局部变量,而不是成员变量。