久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

BadPaddingException 在 Android 中解密加密數(shù)據(jù)

BadPaddingException decrypting the encrypted data in Android(BadPaddingException 在 Android 中解密加密數(shù)據(jù))
本文介紹了BadPaddingException 在 Android 中解密加密數(shù)據(jù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我是 Android 安全概念的新手.

I'm new to Android security concepts.

我一直在閱讀一些博客以了解我們可以使用公鑰加密數(shù)據(jù)并使用相應(yīng)的私鑰對其進(jìn)行解密.加密似乎沒有任何問題,但是當(dāng)我嘗試解密它時,它會拋出:

I have been reading some blogs to 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.

javax.crypto.BadPaddingException: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02.

我的代碼如下:

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

我不確定哪里出錯了.

推薦答案

當(dāng)填充(填充太小的加密塊的字節(jié))不匹配指定格式(例如例如 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 模式進(jìn)行加密和解密.
  2. 你從加密中得到的數(shù)據(jù)(字節(jié)[])與你傳遞給解密的數(shù)據(jù)不同.
  3. (您使用的密鑰對不正確.)

由于您使用 getInstance("RSA") 初始化 RSA 進(jìn)行加密,使用 getInstance("RSA/ECB/PKCS1Padding") 進(jìn)行解密,因此可能ECB/PKCS1Padding 不是 Android 上的默認(rèn)設(shè)置(即使它應(yīng)該在 Desktop-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() 中試試這個:

So try this in RSAEncrypt():

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

如果這不起作用,請確保將加密中從 cipher.doFinal() 獲得的完全相同的 byte[] 傳遞給 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.

(順便說一句,你的代碼在我的 Desktop Java7 上運行.)

這篇關(guān)于BadPaddingException 在 Android 中解密加密數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 亚洲精品乱码 | 中文字幕一区二区三区在线观看 | 亚洲精品www | 第四色影音先锋 | 在线观看视频一区二区三区 | 中文字幕视频在线观看 | 91在线视频免费观看 | 欧美在线观看一区 | 国产草草视频 | 国产精品视频免费观看 | 亚州一区二区三区 | 亚洲视频一区在线观看 | 天天操操操操操 | 欧美日韩精品一区二区三区视频 | 五月天国产视频 | 欧美中文一区 | 欧美一区二区三区久久精品 | 操操日| 精品国产乱码久久久久久丨区2区 | 在线一区二区观看 | 最新中文字幕久久 | 亚洲精彩视频 | 亚洲成人精品 | 国产亚洲一区二区三区 | 国产精品视频一区二区三区四蜜臂 | 精品久久久网站 | 久久黄网 | av一二三区 | 日韩亚洲视频 | av中文字幕在线 | 男女免费网站 | 免费观看的av毛片的网站 | 国产精品欧美一区喷水 | 国产精品免费一区二区三区四区 | 午夜激情在线视频 | 亚洲欧美精 | 亚洲a毛片 | 中文字幕一区二区视频 | 午夜精品导航 | 国产精品福利视频 | 中文字幕视频一区二区 |