rsa 加解密
*/
public final class CipherRsaUtil {private static final Logger log = LoggerFactory.getLogger("CipherUtil");public static final String KEY_RSA = "RSA";public static final String SIG_ALG_SHA256_RSA = "SHA256withRSA";public static final int KEY_SIZE = 2048;
public static final int KEY_SIZE_MAX = 4096;
private CipherRsaUtil() {
}public static String encryptRsa(String publicKeyStr, String context) {
if (publicKeyStr == null) {
log.error("KeyUtil.encrypt--context:{}, publicKey is null", context);
return context;
}
try {
byte[] decodeArr = Base64.getDecoder().decode(publicKeyStr);
X509EncodedKeySpec encPubKeySpec = new X509EncodedKeySpec(decodeArr);
PublicKey publicKey = KeyFactory.getInstance(KEY_RSA).generatePublic(encPubKeySpec);
Cipher cipher = Cipher.getInstance(KEY_RSA);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = context.getBytes(StandardCharsets.UTF_8);
byte[] output = null;
for (int i = 0; i < bytes.length; i += 64) {
byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(bytes, i, i + 64));
output = ArrayUtils.addAll(output, doFinal);
}
return Base64.getEncoder().encodeToString(output);
} catch (Exception e) {
log.error("KeyUtil.encrypt Exception:", e);
throw new RuntimeException("Encrypt Failed.", e);
}
}public static String decryptRsa(String privateKeyStr, String context) {
if (privateKeyStr == null) {
log.error("KeyUtil.decrypt--context:{}, privateKey is null", context);
return context;
}
try {
byte[] decodeArr = Base64.getDecoder().decode(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodeArr);
PrivateKey privateKey = KeyFactory.getInstance(KEY_RSA).generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance(KEY_RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = Base64.getDecoder().decode(context);
byte[] output = new byte[0]; // 初始化为空数组
for (int i = 0; i < bytes.length; i += 512) { // 4096位密钥的块大小为512字节
byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(bytes, i, i + 512));
output = ArrayUtils.addAll(output, doFinal);
}
return new String(output, StandardCharsets.UTF_8);
} catch (Exception e) {
log.error("KeyUtil.decrypt Exception:", e);
throw new RuntimeException("Decrypt Failed.", e);
}
}public static PrivateKey rsaPrivateKey(String privateKeyString) throws Exception {
byte[] priKeyArr = Base64.getDecoder().decode(privateKeyString);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(priKeyArr);
KeyFactory kf = KeyFactory.getInstance(KEY_RSA);
PrivateKey priKey = kf.generatePrivate(spec);
return priKey;
}public static String sign(String data, String privateKeyStr, String signAlg) throws Exception {
PrivateKey privateKey = rsaPrivateKey(privateKeyStr);
Signature signature = Signature.getInstance(signAlg);
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(signature.sign());
}public static boolean verify(String srcData, String publicKey, String sign) {
try {
byte[] keyBytes = Base64.getDecoder().decode(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA);
PublicKey key = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIG_ALG_SHA256_RSA);
signature.initVerify(key);
signature.update(srcData.getBytes(StandardCharsets.UTF_8));
byte[] arr = Base64.getDecoder().decode(sign.getBytes(StandardCharsets.UTF_8));
return signature.verify(arr);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}