RSA解密 @param data 待解密数据 @return
(String data)
| 100 | * @return |
| 101 | */ |
| 102 | public static String decrypt(String data) throws Exception { |
| 103 | PrivateKey privateKey = getPrivateKey(thprik); |
| 104 | Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding"); |
| 105 | cipher.init(Cipher.DECRYPT_MODE, privateKey); |
| 106 | byte[] dataBytes = Base64.decode(data,Base64.NO_WRAP); |
| 107 | int inputLen = dataBytes.length; |
| 108 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 109 | int offset = 0; |
| 110 | byte[] cache; |
| 111 | int i = 0; |
| 112 | // 对数据分段解密 |
| 113 | while (inputLen - offset > 0) { |
| 114 | if (inputLen - offset > MAX_DECRYPT_BLOCK) { |
| 115 | cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK); |
| 116 | } else { |
| 117 | cache = cipher.doFinal(dataBytes, offset, inputLen - offset); |
| 118 | } |
| 119 | out.write(cache, 0, cache.length); |
| 120 | i++; |
| 121 | offset = i * MAX_DECRYPT_BLOCK; |
| 122 | } |
| 123 | byte[] decryptedData = out.toByteArray(); |
| 124 | out.close(); |
| 125 | // 解密后的内容 |
| 126 | return new String(decryptedData, "UTF-8"); |
| 127 | } |
| 128 | |
| 129 | |
| 130 |
nothing calls this directly
no test coverage detected