Android如何从.net服务器解码Aes编码的字符串?

时间:2011-09-14 11:29:34

标签: android aes

iam尝试解码String,这是来自服务器的响应。

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

得到错误,日志是

    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)

0 个答案:

没有答案