抛出BadPaddingException当我尝试解密Android上的加密数据 [英] Throws BadPaddingException when I try to decrypt the encrypted data in android

查看:691
本文介绍了抛出BadPaddingException当我尝试解密Android上的加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的Andr​​oid的安全概念,
阅读获得一些博客来了解我们可以使用公共密钥加密的数据,可以使用相应的私钥解密它,加密似乎没有任何问题,但是当我尝试解密它,它抛出 javax.crypto.BadPaddingException:错误:0407106B:RSA套路:RSA_padding_check_PKCS1_type_2:块类型不是02 请能有人帮我解决这个问题,我的code是如下,

Hi I'm new to android security concepts, Reading some blogs in get to know about we can encrypt data using Public key and can decrypt it using respective Private key, Encryption seems to be doesn't have any problem, but when I try to decrypt it, it throws javax.crypto.BadPaddingException: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02. Could please some one help me out in solving this problem, My code is as follows,

public String RSAEncrypt(final String plain, PublicKey publicKey ) throws NoSuchAlgorithmException, NoSuchPaddingException,
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte [] encryptedBytes = cipher.doFinal(plain.getBytes());
    String encrypted = bytesToString(encryptedBytes);
    System.out.println("EEncrypted?????" + encrypted );
    return encrypted;
}

public String RSADecrypt(String encryptedBytes,PrivateKey privateKey ) throws NoSuchAlgorithmException, NoSuchPaddingException,
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
    Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher1.init(Cipher.DECRYPT_MODE, privateKey);
    byte [] decryptedBytes = cipher1.doFinal(stringToBytes(encryptedBytes));

    String decrypted = new String(decryptedBytes);
    System.out.println("DDecrypted?????" + decrypted);
    return decrypted;
    }

public  String bytesToString(byte[] b) {
    byte[] b2 = new byte[b.length + 1];
    b2[0] = 1;
    System.arraycopy(b, 0, b2, 1, b.length);
    return new BigInteger(b2).toString(36);
}

public  byte[] stringToBytes(String s) {
    byte[] b2 = new BigInteger(s, 36).toByteArray();
    return Arrays.copyOfRange(b2, 1, b2.length);
}

堆栈跟踪如下:

07-28 11:27:35.119: I/System.out(22933): KEYSTORE : String to encrypt = > Hello
07-28 11:27:35.119: I/System.out(22933): KEYSTORE : [B@41bbf4d0
07-28 11:27:38.422: I/System.out(22933): KEYSTORE : String to Decrypt = > UJGAchuDhu3mqH5YPjmYqKBapJYMjJRk9g6HIy8bANooWorzwqgiEo+dOse6Nfq7i0yzw/Wt7TSdTNiYROxehkZvEx/mW5+Niw1CgZ2y9b/ijTeNTF+7aGPrqfDXJ38hUFdTPc6oNl2FVOIafncGOSK9po1JOAYeK0JiA2KrACfPLPjsLQSRzseThyYGxttRM7qbx/N0VTmlTeuNpLFld8Gtw3fHR8UoLGkH/OTFYPLZBVNE8t/oCCy8FpcCu9SGXxF8vh1R4rq15bfyyh9sBU9RuVtoLM0wDSbKixHhNOwwx2Z/A+SHDaQD9C+x3p1AnS9FYZm0Y07E+VYQWqzOpw
07-28 11:27:38.562: W/System.err(22933): javax.crypto.BadPaddingException: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02
07-28 11:27:41.515: D/WifiNative-wlan0(773): doString: SIGNAL_POLL
07-28 11:27:41.515: W/WifiHW(773): QCOM Debug wifi_send_command "IFNAME=wlan0 SIGNAL_POLL"
07-28 11:27:41.525: D/wpa_supplicant(16189): nl80211: survey data missing!
07-2
07-28 11:27:56.612: W/WifiHW(773): QCOM Debug wifi_send_command "IFNAME=wlan0 SIGNAL_POLL"
07-28 11:27:56.612: D/wpa_supplicant(16189): nl80211: survey data missing!
07-28 11:27:56.622: I/wpa_supplicant(16189): environment dirty rate=0 [0][0][0]
07-28 11:27:56.622: D/WifiStateMachine(773): fetchRssiAndLinkSpeedNative RSSI = -62 abnormalRssiCnt = 0 newLinkSpeed = 58
07-28 11:27:56.622: D/WifiStateMachine(773): fetchRssiAndLinkSpeedNative mLinkspeedCount = 2, mLinkspeedSum: 116

我不知道我要去哪里错了,请帮助我。
谢谢你。

I'm not sure where am going wrong, please help me out. Thanks .

推荐答案


A BadPaddingException 的发生时,填充(字节填充一个太小加密块)不符合规定的格式(例如PKCS1,OAEP,...)。这能有几个原因:

A BadPaddingException occurs when the padding (bytes to fill up a too small encryption block) doesn't match a specified format (for example PKCS1, OAEP, ...). This can have a few causes:


  1. 您正在使用RSA对恩和解密不同的模式。

  2. 数据从加密得到(字节[])是不一样的,你传递给解密之一。

  3. (你正在使用不正确的密钥对。)

由于您正在使用初始化RSA的getInstance(RSA)用于加密和的getInstance(RSA / ECB / PKCS1Padding) 进行解密,它可能是可能的 ECB / PKCS1Padding 的不是Android上(即使它应该是桌面Java的)。

Since you are initializing RSA with getInstance("RSA") for encryption and getInstance("RSA/ECB/PKCS1Padding") for decryption, it could be possible that ECB/PKCS1Padding is not the default on Android (even though it should be on Desktop-Java).

因此​​,尝试这 RSAEncrypt()

cipher.getInstance("RSA/ECB/PKCS1Padding");

如果这不起作用,请确保您通过相同的字节[]从获得cipher.doFinal()加密技术,以 cipher.doFinal()解密

If this does not work, make sure that you pass the exact same byte[] you get from cipher.doFinal() in encryption to cipher.doFinal() in decryption.

(您code不工作在我的台式java7目录BTW。)

这篇关于抛出BadPaddingException当我尝试解密Android上的加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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