@Component
@Slf4j
public class RSAUtils {
private static final String KEY_ALGORITHM = "RSA";
private static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
public static final String PUBLIC_KEY = "RSAPublicKey";
public static final String PRIVATE_KEY = "RSAPrivateKey";
private static final int MAX_ENCRYPT_BLOCK = 245;
private static final int MAX_DECRYPT_BLOCK = 2048;
public static Map<String, String> genKeyPair(int keySize){
KeyPairGenerator keyPairGen = null;
try {
keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(String.format("生密钥异常%s", ExceptionUtils.getStackTrace(e)));
}
final BASE64Encoder encoder = new BASE64Encoder();
keyPairGen.initialize(keySize);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, String> keyMap = new HashMap<>(2);
keyMap.put(PUBLIC_KEY, encoder.encode(publicKey.getEncoded()));
keyMap.put(PRIVATE_KEY, encoder.encode(privateKey.getEncoded()));
return keyMap;
}
public static String sign(byte[] data, String privateKey) throws Exception {
byte[] keyBytes = new BASE64Decoder().decodeBuffer(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateK);
signature.update(data);
return new BASE64Encoder().encode(signature.sign());
}
public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
byte[] keyBytes = new BASE64Decoder().decodeBuffer(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicK = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicK);
signature.update(data);
return signature.verify(new BASE64Decoder().decodeBuffer(sign));
}
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
byte[] keyBytes = new BASE64Decoder().decodeBuffer(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
return decryptByPrivateKey(encryptedData, privateK);
}
public static byte[] decryptByPrivateKey(byte[] encryptedData, Key privateK) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, privateK);
int inputLen = encryptedData.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(encryptedData, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
public static String decryptByPrivateKey(String encryptedData, String privateKey) throws Exception {
final byte[] bytes = decryptByPrivateKey(new BASE64Decoder().decodeBuffer(encryptedData), privateKey);
return new String(bytes, StandardCharsets.UTF_8);
}
public static String decryptByPublicKey(String encryptedData, String publicKey) throws Exception {
final byte[] bytes = decryptByPublicKey(new BASE64Decoder().decodeBuffer(encryptedData), publicKey);
return new String(bytes, StandardCharsets.UTF_8);
}
public static String decryptByPublicKey(String encryptedData, PublicKey publicK) throws Exception {
final byte[] bytes = decryptByPublicKey(new BASE64Decoder().decodeBuffer(encryptedData), publicK);
return new String(bytes, StandardCharsets.UTF_8);
}
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
byte[] keyBytes = new BASE64Decoder().decodeBuffer(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicK = keyFactory.generatePublic(x509KeySpec);
return decryptByPublicKey(encryptedData, publicK);
}
public static byte[] decryptByPublicKey(byte[] encryptedData, PublicKey publicK) throws Exception {
System.out.println(encryptedData.length);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, publicK);
int inputLen = encryptedData.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(encryptedData, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
final byte[] bytes = encryptByPublicKey(data.getBytes(StandardCharsets.UTF_8), publicKey);
return new BASE64Encoder().encode(bytes);
}
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
byte[] keyBytes = new BASE64Decoder().decodeBuffer(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
return encryptByPublicKey(data, publicK);
}
public static byte[] encryptByPublicKey(byte[] data, Key publicK) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publicK);
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
}
public static String encryptByPrivateKey(String data, String privateKey) throws Exception {
final byte[] bytes = encryptByPrivateKey(data.getBytes(StandardCharsets.UTF_8), privateKey);
return new BASE64Encoder().encode(bytes);
}
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
byte[] keyBytes = new BASE64Decoder().decodeBuffer(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, privateK);
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
}
}
测试
public static void main(String[] args) throws Exception {
Map<String, String> stringStringMap = genKeyPair(1024);
String pub = stringStringMap.get(PUBLIC_KEY);
System.out.println(pub);
String pri = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKpXHlS1XDb0IN+ok7rvuKMWPgvd7Vi48G4PrRkAMCD3Eyf5XdjIvm6I1geRrsxlCmhppePoVlGWxh8aqX+HWfiTsaqaUn0AA6zMEl+0z3Z2r2BfMaLfWT+RYOzGuvXQ+GVJqQmLy7803zpyyciQ6LyOz03Ahj1YIarWyBFlyrZbAgMBAAECgYAMwgajCIxWD1XQ+pLJ+v9ks5tFD0OiFAPp8mGgEDwKlAbKxSoKvQpKRBRnr7kcVOsBI/yJQ9MHPFgtZzVPuGb8EFNdMSLV72HFMzKaiV04P/uPahc3uO7IktVDfw8HjM/BppYBgcYsLHLM6sTvc6ZlrJrI3Bui2RAUyaZp0amBNQJBAOBUdXv2IMD4fmjtuA5UEgvWrMmX9t7n4Dcb3l0pYXsTsw/Tl4iWFzTt+P3JmSVlH1xO6bZlGkNmmQM0Et51avcCQQDCY2m3YWO4FVjXt/Pth28ZNWlPAJu/bpujFfhBie7iRu5XlGdpHCfMQjuB/q/zOlH25CNST7aW4sRWycRfXbK9AkAz5vajrqeXXicJKe8KyaK/QHUL03A";
System.out.println();
System.out.println(pri);
System.out.println();
String data = "123456";
final String s = encryptByPublicKey(data, pub);
final String s2 = "ISLP8qeIDR/zYXKedpGhEqD/3dW3ox50KBRaE5IFuRqpeo/tJruG5f7yV7qc5s0NQY+RHV2vZvamZeK3w9uXT8P9q7SfRMmv5fOqblKfwiMHQxcezIE";
System.out.println(s);
System.out.println();
System.out.println(decryptByPrivateKey(s2, pri));
}