javax.crypto.IllegalBlockSizeException:解密异常中的最后一个块不完整

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18350459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:47:37  来源:igfitidea点击:

javax.crypto.IllegalBlockSizeException: last block incomplete in decryption exception

javaandroidexceptionencryptionaes

提问by sravanalakshmi.sunkara

I am trying to decrypt a string in android. I keep getting the following exception:

我正在尝试在 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.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.access0(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)

The encrypt and decrypt methods are:

加密和解密的方法是:

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;
}

采纳答案by sravanalakshmi.sunkara

I got the solution.Following is new code that worked for me

我得到了解决方案。以下是对我有用的新代码

// 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;
}

回答by votiethuy

salt.getBytes()should be salt.getBytes(UNICODE_FORMAT)

salt.getBytes()应该 salt.getBytes(UNICODE_FORMAT)