2025-02-24 20:05:20 +08:00

146 lines
5.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.syzb.common.util;
import cn.hutool.core.codec.Base64;
import com.syzb.common.util.logger.LoggerUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
public class RsaUtil {
/**
* RSA最大加密大小
*/
private final static int MAX_ENCRYPT_BLOCK = 117;
/**
* **
* RSA最大解密大小
*/
private final static int MAX_DECRYPT_BLOCK = 128;
/**
* 公钥加密
*
* @param pubKey 公钥字符串
* @param str 需要加密操作的字符串
* @return 结果再Base64加密后的字符串
* @throws Exception
*/
public static String pubKeyEncryption(String pubKey, String str) {
try {
// String 转 公钥
PublicKey rsaPublicKey = getPublicKey(pubKey);
//公钥加密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] inputArray = str.getBytes();
int inputLength = inputArray.length;
// 分段加密
int offSet = 0;
byte[] resultBytes = {};
byte[] cache;
while (inputLength - offSet > 0) {
if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
offSet += MAX_ENCRYPT_BLOCK;
} else {
cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
offSet = inputLength;
}
resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
}
return Base64.encode(resultBytes);
} catch (Exception exception) {
LoggerUtil.info("RsaUtil.pubKeyEncryption异常" + ExceptionUtils.getStackTrace(exception));
return null;
}
}
/**
* 用私钥 解密
*
* @param priKey 私钥字符串
* @param encryptionBase64Str RSA加密后又base64编码后的字符串
* @return 解密结果
* @throws Exception
*/
public static String priKeyDecryption(String priKey, String encryptionBase64Str) {
try {
// base64 解码
byte[] base64 = Base64.decode(encryptionBase64Str);
// String 转 私钥
PrivateKey rsaPrivateKey = getPrivateKey(priKey);
//私钥解密
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 返回UTF-8编码的解密信息
int inputLen = base64.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(base64, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(base64, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
out.close();
return out.toString(StandardCharsets.UTF_8);
} catch (Exception exception) {
LoggerUtil.info("RsaUtil.priKeyDecryption异常" + ExceptionUtils.getStackTrace(exception));
return null;
}
}
/**
* String转公钥PublicKey
*
* @param key
* @return
* @throws Exception
*/
private static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes = Base64.decode(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
/**
* String转私钥PrivateKey
*
* @param key
* @return
* @throws Exception
*/
private static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes = Base64.decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
}