javax.crypto.IllegalBlockSizeException:解密异常中的最后一个块未完成
我正在尝试在android中解密一个字符串。我一直得到以下例外情况:javax.crypto.IllegalBlockSizeException:解密异常中的最后一个块未完成,java,android,exception,encryption,aes,Java,Android,Exception,Encryption,Aes,我正在尝试在android中解密一个字符串。我一直得到以下例外情况: 08-21 03:56:56.700: W/System.err(4208): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption 08-21 03:56:56.700: W/System.err(4208): at com.android.org.bouncycastle.jce.provider.JCEBlockCi
08-21 03:56:56.700: W/System.err(4208): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
08-21 03:56:56.700: W/System.err(4208): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:697)
08-21 03:56:56.700: W/System.err(4208): at javax.crypto.Cipher.doFinal(Cipher.java:1106)
08-21 03:56:56.700: W/System.err(4208): at com.dharani.android.legalplex.BusinessLayer.BLCommonOperations.decrypt(BLCommonOperations.java:284)
08-21 03:56:56.700: W/System.err(4208): at com.dharani.android.legalplex.BusinessLayer.BLCommonOperations.decryptAndgetFailCountFromPreferences(BLCommonOperations.java:144)
08-21 03:56:56.700: W/System.err(4208): at com.dharani.android.legalplex.PresentationLayer.TransparentActivity.onCreate(TransparentActivity.java:112)
08-21 03:56:56.700: W/System.err(4208): at android.app.Activity.performCreate(Activity.java:4465)
08-21 03:56:56.700: W/System.err(4208): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-21 03:56:56.700: W/System.err(4208): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 03:56:56.700: W/System.err(4208): at android.os.Looper.loop(Looper.java:137)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.main(ActivityThread.java:4507)
08-21 03:56:56.700: W/System.err(4208): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 03:56:56.700: W/System.err(4208): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 03:56:56.700: W/System.err(4208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-21 03:56:56.700: W/System.err(4208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-21 03:56:56.700: W/System.err(4208): at dalvik.system.NativeStart.main(Native Method)
加密和解密方法包括:
public String encrypt(String message) throws Exception
{
String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(message.getBytes());
String encrypted=Base64.encodeToString(encVal, Base64.DEFAULT);
return encrypted;
}
public String decrypt(String message) throws Exception
{
String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
Cipher c = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = Base64.decode(message.getBytes(), Base64.DEFAULT);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
salt.getBytes()
应该是salt.getBytes(UNICODE\u格式)
我得到了解决方案。下面是适合我的新代码
// Encryption
public String encrypt(String message) throws Exception
{
String message1=Base64.encodeBytes(message.getBytes(),Base64.NO_OPTIONS);
String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(message1.getBytes());
String encrypted=Base64.encodeToString(encVal, Base64.NO_OPTIONS);
return encrypted;
}
//Decryption
public String decrypt(String message) throws Exception
{
String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
Cipher c = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = Base64.decode(message.getBytes(), Base64.NO_OPTIONS);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
String decoded=new String(Base64.decode(decryptedValue,Base64.NO_OPTIONS));
return decoded;
}
当您这样调用时,是否可以提供一些导致此错误的测试数据:
decrypt(encrypt(testData))
。还包括您使用的实际加密密钥值。@很高兴听到您找到了问题的答案。你应该把它作为一个答案,并解释需要改变什么,然后你就可以接受了。这有助于其他用户在将来找到它,并让他们知道什么对你有用。你需要解释你所做的更改,而不仅仅是在没有解释的情况下发布代码。@sravanalakshmi sunkara没有Base64。没有可用的选项:-没有填充,没有包装,没有关闭Only警告:这段代码使用ECB,使用隐藏的默认字符串(“AES”)“AES/ECB/PKCS5P”适用于大多数提供商)。ECB模式不安全。