I have an issue with the decryption in the following code. I have a encrypted string being sent to setData(). I am trying to decrypt the encrypted string(data). The error I keep getting is
我在以下代码中遇到了解密问题。我有一个加密的字符串发送到setData()。我试图解密加密的字符串(数据)。我一直得到的错误是
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整
byte[] data;
String key = "tkg96827pco74510";
byte[] encryptedOut;
String decryptedOut;
Key aesKey;
Cipher cipher;
public void setData(String dataIn){
this.data = dataIn.getBytes();
try {
aesKey = new SecretKeySpec(key.getBytes(), "AES");
cipher = Cipher.getInstance("AES");
}catch(Exception e){
System.out.println("SET DATA ERROR - " + e);
}
}
public void encrypt() {
try{
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
encryptedOut = cipher.doFinal(data);
}catch(Exception e){
System.out.println(e);
}
}
public void decrypt(){
try {
cipher.init(Cipher.DECRYPT_MODE, aesKey);
decryptedOut = new String(cipher.doFinal(data));
}catch(Exception e){
System.out.println("Decrypt Error: " + e);
}
}
public byte[] getEncrypted() {
return encryptedOut;
}
public String getDecrypted(){
return decryptedOut;
}
2 个解决方案
#1
The problem is caused by this line :
问题是由这条线引起的:
decryptedOut = new String(cipher.doFinal(data));
Here you are passing original data to decrypt. But you should pass encryptedOut
here.
在这里,您将传递原始数据进行解密。但是你应该在这里传递encryptedOut。
So the solution would be :
所以解决方案是:
decryptedOut = new String(cipher.doFinal(encryptedOut));
and yes, Please pass some encoding mechanism to convert String to byteArray and vice-versa like "UTF-8"
.
是的,请传递一些编码机制将String转换为byteArray,反之亦然,如“UTF-8”。
So the correct line would be :
所以正确的路线是:
decryptedOut = new String(cipher.doFinal(encryptedOut),"UTF-8");
Given you have done conversion to bytes like this :
鉴于您已完成转换为字节,如下所示:
this.data = dataIn.getBytes("UTF-8");
#2
The problem is here:
问题出在这里:
public String getDecrypted()
and here:
decryptedOut = new String(cipher.doFinal(data));
String
is not a container for binary data. You need to encode the cipher text somehow before you put it into a String.
For example, base64-encoding.
String不是二进制数据的容器。在将密文放入String之前,需要以某种方式对密文进行编码。例如,base64编码。
#1
The problem is caused by this line :
问题是由这条线引起的:
decryptedOut = new String(cipher.doFinal(data));
Here you are passing original data to decrypt. But you should pass encryptedOut
here.
在这里,您将传递原始数据进行解密。但是你应该在这里传递encryptedOut。
So the solution would be :
所以解决方案是:
decryptedOut = new String(cipher.doFinal(encryptedOut));
and yes, Please pass some encoding mechanism to convert String to byteArray and vice-versa like "UTF-8"
.
是的,请传递一些编码机制将String转换为byteArray,反之亦然,如“UTF-8”。
So the correct line would be :
所以正确的路线是:
decryptedOut = new String(cipher.doFinal(encryptedOut),"UTF-8");
Given you have done conversion to bytes like this :
鉴于您已完成转换为字节,如下所示:
this.data = dataIn.getBytes("UTF-8");
#2
The problem is here:
问题出在这里:
public String getDecrypted()
and here:
decryptedOut = new String(cipher.doFinal(data));
String
is not a container for binary data. You need to encode the cipher text somehow before you put it into a String.
For example, base64-encoding.
String不是二进制数据的容器。在将密文放入String之前,需要以某种方式对密文进行编码。例如,base64编码。