[英]Can't overcome the javax.crypto.BadPaddingException
我正在寫一個隱寫系統,該系統對圖像中的文本進行加密-我決定在將其嵌入圖片之前也要對文本進行加密。 隱寫算法與String輸入/輸出一起使用。
由於我的想法,我一直在嘗試使用DES和AES算法,但遇到了上面的Exception。 我將舉例說明AES算法的加密/解密方法:
public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
return byteCipherText;}
public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}
這是調用(加密端):
//sending the text to encrypt using AES algorithm
byte[] cipherText = new AES_Algorithm().encrypt(messageDesc.getText(), key);
//converting into a String to encrypt using Steganography
newStringtoEncrypt = new String (cipherText);
這是調用方(解密方) -EXCEPTION :
//AES encrypted String after the Steganography's decryption
String decMessage = dec.decode(path, name);
//converting the String to byte []
byte [] byteArray = decMessage.getBytes();
try{
//HERE IS WHERE I GET THE EXCEPTION
//sending the byte array to decryption
String original = AES_Algorithm().decrypt(byteArray, key);
問題出在哪兒?
鍵和字節數組也很相似(我通過打印序列對其進行了檢查)-但是有人告訴我,當我打算使用AES/DES
時,我不能使用getBytes()
從String轉換為byteArray
。解密算法。
加密的輸出是二進制的,您不能像在此代碼中那樣將其轉換為String
:
byte[] cipherText = new AES_Algorithm().encrypt(messageDesc.getText(), key);
//converting into a String to encrypt using Steganography
newStringtoEncrypt = new String (cipherText);
如果需要將加密的數據作為String
,則需要以某種方式對其進行編碼,例如通過base64encoding或hexEncoding
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.