我試圖開發一個簡單的加密/解密程序。我遇到的問題是,當我嘗試解密加密的消息時,出現一條錯誤消息,指出使用密碼解密時,輸入長度必須是16的倍數。我在某處讀取加密的消息在將其轉換爲字符串之前可能需要進行編碼。我不知道如何做到這一點?或者如果有另一種方式可以幫助我一個人嗎?JAVA解密錯誤:需要輸入爲16的倍數
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
public class Cryption {
public static void cryption(String[] args, String message) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
byte[] encodedKey = "ADBSJHJS12547896".getBytes();
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
Key aesKey = keyGen.generateKey();
System.out.println("CheckType: "+ Global.checkType);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] input = Global.message.getBytes();
// Check if clicked Encrypted
if(Global.checkType==true) {
// Encrypt
byte[] messageEncrypted = cipher.doFinal(input);
System.out.println("Encrypted Text: " + messageEncrypted);
Global.encValue = messageEncrypted.toString();
}
// Check if clicked Decrypted
if(Global.checkType==false) {
//String mes = message;
System.out.println(Global.message);
System.out.println("Char lenght " + Global.message.length());
byte[] mesByte = Global.message.getBytes();
// Decrypt
cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] messageDecrypted = cipher.doFinal(mesByte);
System.out.println("Text Decrypted: " + new String(messageDecrypted));
}
}
}
它比這更糟糕,它根本不會發送密文,請參閱EJP – 2012-03-01 16:57:26