AES解密:javax.crypto.BadPaddingException:垫块损坏的Andr​​oid [英] AES Decryption: javax.crypto.BadPaddingException: pad block corrupted in Android

查看:297
本文介绍了AES解密:javax.crypto.BadPaddingException:垫块损坏的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持我的Andr​​oid应用程序与AES解密的问题。我寻觅了很多,但无法得到解决。

I am stuck with a problem with AES Decryption in my android Application. I have searched a lot but unable to get the solution.

下面是具体步骤,我在做什么。

Here are the steps, what i am doing.


  • 加密与我的钥匙信用卡号码发送到Web服务器。

  • Web服务器解密的信用卡号,并保存。

  • 当我们从获取Web服务的信用卡号码。

  • 然后Web服务器加密使用相同的密钥信用卡号码,发送给我们。

  • 现在,当我们解密这个数字,它会抛出一些信用卡号码信息坏填充例外。

也进行了加密信息是不一样的来自服务器,我们在加密格式有致。
而同样的事情在iPhone应用程序做的,iPhone是能够成功地解密信息。

Also encrypted information is not same coming from the server, what we have send in encrypted format. While the same thing is done in iPhone app, and iPhone is able to decrypt the information successfully.

我用下面的code用于加密和解密。

I am using the following code for encryption and decryption.

public class AES256Cipher {

    public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    public static String AES_Encode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

        byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
             SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
             Cipher cipher = null;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);

        return Base64.encodeToString(cipher.doFinal(textBytes), 0);
    }

    public static String AES_Decode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

        byte[] textBytes =Base64.decode(str,0);
        //byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return new String(cipher.doFinal(textBytes), "UTF-8");
    }

请建议。

编辑:
我一件事,它正在为< 16位数的信息。当我们把16位信息,那么就抛出异常解密。

I one more thing, it is working for < 16 digits information. When we put the 16 digit information, then it is throwing the Exception in decryption.

推荐答案

如果服务器遇到未知的编码不映射到特定字符那么关键就不能正常转移,并在一段时间后失效,从而导致不​​正确的密钥。密文是EN codeD使用Base64所以这可能是好的,但你的钥匙可能不会这么幸运了。

If the server encounters unknown encodings that don't map to specific characters then the key will not transfer properly and fail once in a while, resulting in an incorrect key. The ciphertext is encoded using base64 so that's probably OK, but your key may not be so lucky.

请注意,在键或密文的最后阻止任何变化的可能的导致一个 BadPaddingException

Note that any change in the key or the last blocks of the ciphertext is likely to result in a BadPaddingException.

这篇关于AES解密:javax.crypto.BadPaddingException:垫块损坏的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆