0

iam trying to decode String, which is a response from server.

byte[] input = "6KzVyH0s3RyliBBAkcIrKOmripAUrDgBrm3VZR0VKkDlBTsHdOm/ONN1st/PBhquynxOjIPvgTfXJKx3aYDaARvCBmJwm1a0kfqFUcdpho+QSqpnqlDaBelL3taPKy2XPNmbOWlYUQtircPwcTrbOEUrQeUjEKqtataHw1tIp5c=".getBytes();


            String key1="12345891456";

           // byte[] keyBytes = new byte[] { 1234567891123456 };

            byte[] keyBytes = null;
            try {
                keyBytes = key1.getBytes("UTF-16LE");               

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

            //key.init(128);

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

            System.out.println(new String(input));

            // encryption pass
            cipher.init(Cipher.ENCRYPT_MODE, key);

            byte[] cipherText = new byte[cipher.getOutputSize(input.length)];           
            int ctLength = cipher.update(input, 0, input.length, cipherText, 0);       

            // decryption pass
            cipher.init(Cipher.DECRYPT_MODE, key);

            byte[] plainText = new byte[cipher.getOutputSize(ctLength)];

            int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);

            ptLength += cipher.doFinal(plainText, ptLength);
            System.out.println(new String(plainText));
            System.out.println(ptLength);
          }

And got error, the log is

    09-14 16:24:08.348: WARN/System.err(2144): javax.crypto.BadPaddingException: pad block corrupted
09-14 16:24:08.348: WARN/System.err(2144):     at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:766)
09-14 16:24:08.358: WARN/System.err(2144):     at javax.crypto.Cipher.doFinal(Cipher.java:1064)
09-14 16:24:08.358: WARN/System.err(2144):     at com.dharani.LazyApple.cryptoED.enData(cryptoED.java:222)
09-14 16:24:08.358: WARN/System.err(2144):     at com.dharani.LazyApple.Views.LoginView.onCreate(LoginView.java:58)
09-14 16:24:08.358: WARN/System.err(2144):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-14 16:24:08.358: WARN/System.err(2144):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-14 16:24:08.358: WARN/System.err(2144):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-14 16:24:08.358: WARN/System.err(2144):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-14 16:24:08.358: WARN/System.err(2144):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-14 16:24:08.358: WARN/System.err(2144):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 16:24:08.358: WARN/System.err(2144):     at android.os.Looper.loop(Looper.java:130)
09-14 16:24:08.358: WARN/System.err(2144):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-14 16:24:08.358: WARN/System.err(2144):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 16:24:08.358: WARN/System.err(2144):     at java.lang.reflect.Method.invoke(Method.java:507)
09-14 16:24:08.358: WARN/System.err(2144):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-14 16:24:08.358: WARN/System.err(2144):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-14 16:24:08.358: WARN/System.err(2144):     at dalvik.system.NativeStart.main(Native Method)
Kishore
  • 2,051
  • 5
  • 26
  • 45
  • Check this questions; http://stackoverflow.com/questions/868776/badpaddingexception-pad-block-corrupted http://stackoverflow.com/questions/5207184/cipher-pad-block-corrupted-on-gingerbread http://stackoverflow.com/questions/4536241/aes-gingerbread – Samir Mangroliya Sep 14 '11 at 11:33
  • Make sure you did use the same cipher specification (including padding) on both sides. Also, make sure you use the right encoding for your key - UTF-16 does not look like a good idea, and your key has length 11 chars (=22 bytes). – Paŭlo Ebermann Sep 14 '11 at 16:00

0 Answers0