Android 加密和解密错误 - javax.crypto.IllegalBlockSizeException: 解密中的最后一个块不完整
Android Encryption and Decryption Error - javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
我对以下代码中的解密有疑问。我有一个加密的字符串被发送到 setData()。我正在尝试解密加密的字符串(数据)。我不断收到的错误是
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
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;
}
问题在这里:
public String getDecrypted()
这里:
decryptedOut = new String(cipher.doFinal(data));
String
不是二进制数据的容器。在将密文放入 String.
例如 base64-encoding.
之前,您需要以某种方式对其进行编码
问题是由这一行引起的:
decryptedOut = new String(cipher.doFinal(data));
这里您传递的是要解密的原始数据。但是你应该在这里传递encryptedOut
。
所以解决方案是:
decryptedOut = new String(cipher.doFinal(encryptedOut));
是的,请通过一些编码机制将 String 转换为 byteArray,反之亦然,例如 "UTF-8"
。
所以正确的行是:
decryptedOut = new String(cipher.doFinal(encryptedOut),"UTF-8");
假设您已经完成了这样的字节转换:
this.data = dataIn.getBytes("UTF-8");
我对以下代码中的解密有疑问。我有一个加密的字符串被发送到 setData()。我正在尝试解密加密的字符串(数据)。我不断收到的错误是
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
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;
}
问题在这里:
public String getDecrypted()
这里:
decryptedOut = new String(cipher.doFinal(data));
String
不是二进制数据的容器。在将密文放入 String.
例如 base64-encoding.
问题是由这一行引起的:
decryptedOut = new String(cipher.doFinal(data));
这里您传递的是要解密的原始数据。但是你应该在这里传递encryptedOut
。
所以解决方案是:
decryptedOut = new String(cipher.doFinal(encryptedOut));
是的,请通过一些编码机制将 String 转换为 byteArray,反之亦然,例如 "UTF-8"
。
所以正确的行是:
decryptedOut = new String(cipher.doFinal(encryptedOut),"UTF-8");
假设您已经完成了这样的字节转换:
this.data = dataIn.getBytes("UTF-8");