InvalidKeyException java.security.InvalidKeyException:没有安装的提供程序支持此密钥:(空) [英] InvalidKeyException java.security.InvalidKeyException: No installed provider supports this key: (null)
问题描述
我有两个类,一个是主类,另一个是AES的实现。
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
我的加密方法可以正常工作,但是我的解密方法无法按预期工作。
My encryption method works just fine but my decryption method doesn't work as expected.
代码
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;
}
此方法输出的错误是
InvalidKeyException java.security.InvalidKeyException:没有安装的提供程序支持此密钥:(空)
InvalidKeyException java.security.InvalidKeyException: No installed provider supports this key: (null)
UPDATE#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;
}
在主类中,我有这行
byte [] byteciphertext = ciphertext.getBytes();
只是将字符串转换为字节
just to convert the string to bytes
对吗?我仍然有
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
解密时,输入长度必须为16的倍数
Could someone help me fix this issue?
谢谢。
推荐答案
secretKey
显然是 null。
其他问题:
- 您没有使用输入参数
cipherText
。 - 输入参数
cipherText
应该是byte [],
而不是String
:密文为二进制,字符串
不是二进制数据的容器。 - 结果字符串
strDecryptedText
应该是局部变量,而不是成员变量。
- 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.
这篇关于InvalidKeyException java.security.InvalidKeyException:没有安装的提供程序支持此密钥:(空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!