public static String decrypt(byte[] text, PublicKey key) {
byte[] dectyptedText = null;
try {
//秘钥初始化时加入制定默认的算法库
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//使用第三方算法加密库 BC
final Cipher cipher = Cipher.getInstance("RSA/NONE/NoPadding", "BC");
cipher.init(Cipher.DECRYPT_MODE, key);
//分段
int inputLen = text.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > 128) {
cache = cipher.doFinal(text, offSet, 128);
} else {
cache = cipher.doFinal(text, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * 128;
}
dectyptedText = out.toByteArray();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(dectyptedText);
}