我有兩個類,一個是主類,另一個是AES的實現(xiàn).
但是,在我的AES類中,我有一種解密字符串的方法,但是每當(dāng)我運(yùn)行它時,它都會產(chǎn)生異常
我的加密方法工作正常,但我的解密方法無法正常工作.
編碼
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: No installed provider supports this key: (null)
更新#1:
所以我更新了以下代碼
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();
只是將字符串轉(zhuǎn)換為字節(jié)
這是正確的嗎?我還有
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
有人可以幫我解決這個問題嗎?
謝謝. 解決方法: secretKey顯然為null.
其他問題:
>您沒有使用輸入?yún)?shù)cipherText. >輸入?yún)?shù)cipherText應(yīng)該是byte [],而不是字符串:密文是二進(jìn)制的,并且String不是二進(jìn)制數(shù)據(jù)的容器. >結(jié)果字符串strDecryptedText應(yīng)該是局部變量,而不是成員變量. 來源:https://www./content-1-531851.html
|