import org.apache.commons.codec.binary.Base64;
import sun.security.rsa.RSAPrivateCrtKeyImpl;
import org.bouncycastle.util.encoders.Hex;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class CountRsaUtil {
/**
* 私钥解密
* @param encryptedData 加密数据
* @param privateKey 私钥
* @return 解密结果
*/
public static byte[] decryptByPrivateKey(String encryptedData, String privateKey) {
try {
byte[] encryptedDataByte = hexStr2Bytes(encryptedData);
byte[] keyBytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateK);
int inputLen = encryptedDataByte.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > 128) {
cache = cipher.doFinal(encryptedDataByte, offSet, 128);
} else {
cache = cipher.doFinal(encryptedDataByte, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * 128;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
} catch (Exception e) {
}
return null;
}
/**
* 公钥解密
*
* @param encryptedData 已加密数据
* @param privateKey 私钥(BASE64编码)
* @return 解密结果
*/
public static byte[] decryptByPublicKey(String encryptedData, String privateKey) {
try {
byte[] keyBytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateCrtKeyImpl privateK = (RSAPrivateCrtKeyImpl) keyFactory.generatePrivate(pkcs8KeySpec);
RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(privateK.getModulus(), privateK.getPublicExponent());
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(rsaPubKeySpec);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
// 初始化解密
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] encryptedDataByte = hexStr2Bytes(encryptedData);
int inputLen = encryptedDataByte.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > 128) {
cache = cipher.doFinal(encryptedDataByte, offSet, 128);
} else {
cache = cipher.doFinal(encryptedDataByte, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * 128;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
} catch (Exception e) {
}
return null;
}
public static byte[] hexStr2Bytes(String var0) {
return Hex.decode(var0);
}
标签:公钥,私钥,java,cache,解密,offSet,import,byte From: https://www.cnblogs.com/lcxz/p/17705631.html
}