package com.kmqakj.supervise.service.impl.utils;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
/**
* @Author
@Date 2024/3/12 10:54
*/
public class RSAUtils {
/**
* 常量字符串
*/
private static final String RSA = "RSA";
/**
* 私钥
*/
private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALI9HxbLHLbl6HPLuIUPhPzs5vqTq8QFN5fT37ht3gWO1shSKNTpHzslf96Kp0+wBMdN7FqHm7SGGTmSSramgcgDtD17wKRCZ1t4V6KCrtW9QOUxIlRN7c8NOsHqCXaQGQDdT3+/6YKcvtL5+iGLGBKps1j36EO+eoSj31uqxH37AgMBAAECgYAJ3sBH0FlCOEAOQtlF0Q1caOKvke7gl34JHoFUjWJOl3wwXVBdM0hN9lkljIhlrXR49Ek4MtXorMZdKEs9NcJDpo5/froCqpFeOvm93FiA1YMtdnOy/qCNySQ3zoZylsgfJGPyvZlc+Q9TELp+aJT0MQJEHTwIUP0xit70JrQniQJBAPGU3ANAOaOcq/5ppEOO4eOIAojrjKP5ADI8uzZ5Bzk0QK56Kf9dsUC72M64oGc7qZMeQb9AepzpJ8B8cl1RRukCQQC84HGqzsgjdmqnBKjgMCb3kkdI0j3CaxjrhZVcxVHfeokCI98f6475rpRaYkiw+THkwsqsjauw6lWnQ2z0IBdDAkEAkB5nFoVlHWck8DSjqy39Mlj8ciRblaLBbDbw1LFlDspOYAg/L7n8OYsW71QTVSKxqjOds3PK8friHSHgtCz9qQJAWMDvKOB6U4+1lj4uTQrThMqMDMAenVlOIH5pLWrbijQ+ZW8NziYWzWUn+UDt/5tZwrF2I5d+8mdrUGnyrNTXmQJBAKzO2QAQtFQY2Ic5j+62bIiI35YzAqJwd2F8kcTFHAilTZSLC80PU+60LY7zrnOwXju17+oKJWwoCfKX/ibOOnE=";
/**
* 公钥
*/
private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyPR8Wyxy25ehzy7iFD4T87Ob6k6vEBTeX09+4bd4FjtbIUijU6R87JX/eiqdPsATHTexah5u0hhk5kkq2poHIA7Q9e8CkQmdbeFeigq7VvUDlMSJUTe3PDTrB6gl2kBkA3U9/v+mCnL7S+fohixgSqbNY9+hDvnqEo99bqsR9+wIDAQAB";
/**
* 获取密钥对象
*
* @param keySize RSA算法模长(个人理解应该是模长越大加密安全性越高,但加密过程可能也越长)
* @return List
*/
public static List<Key> getRsaObject(int keySize) throws NoSuchAlgorithmException {
//创建list用来接收公钥对象和私钥对象
List<Key> keyList = new ArrayList<>();
//创建RSA密钥生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA);
//设置密钥大小,RSA算法的模长=最大加密数据的大小
keyPairGenerator.initialize(keySize);
//调用函数生成公钥私钥对象(以对生成密钥)
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//获取公钥放入list
keyList.add(keyPair.getPublic());
//获取私钥放入list
keyList.add(keyPair.getPrivate());
//返回list
return keyList;
}
/**
* 生成公钥私钥的字符串
*
* @param keySize 模长
* @return List
*/
public static List<String> getRsaKeyString(int keySize) throws NoSuchAlgorithmException {
//创建list用来接收公钥对象和私钥对象
List<String> keyList = new ArrayList<>();
//创建RSA密钥生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA);
//设置密钥大小,RSA算法的模长=最大加密数据的大小
keyPairGenerator.initialize(keySize);
//调用函数生成公钥私钥对象(以对生成密钥)
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//将公钥对象转换为字符串通过base64加密
String publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
//将私钥对象转换为字符串通过base64加密
String privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
//获取公钥放入list
keyList.add(publicKey);
//获取私钥放入list
keyList.add(privateKey);
//返回list
return keyList;
}
/**
* 通过公钥字符串生成公钥对象(RSAPublicKey类型)
* X509EncodeKeySpec方式(字符串公钥转为RSAPublicKey公钥)
*
* @param publicKeyStr 公钥字符串
* @return 返回RSAPublicKey类型的公钥对象
*/
public static RSAPublicKey getRSAPublicKeyByX509(String publicKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
//密钥工厂创建
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
//公钥字符解密为bytes数组
byte[] keyBytes = Base64.getDecoder().decode(publicKeyStr);
//公钥字符串转x509
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
//x509转RSAPublicKey
return (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
}
/**
* 通过私钥字符串生成私钥对象(RSAPrivateKey类型)
* PKCS8EncodedKeySpec方式(字符串私钥转为RSAPrivateKey公钥)
*
* @param privateKey 私钥字符串
* @return 返回RSAPrivateKey类型的私钥对象
*/
public static RSAPrivateKey getRSAPrivateKeyByPKCS8(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
//密钥工厂创建
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
//私钥字符串解密为bytes数组
byte[] keyBytes = Base64.getDecoder().decode(privateKey);
//私钥字符串转pkcs8
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
//pkcs8转RSAPrivateKey
return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
}
/**
* 公钥加密
*
* @param message 需要加密的信息
* @param rsaPublicKey rsa公钥对象
* @return 返回信息被加密后的字符串
*/
public static String encryptByPublicKey(String message, RSAPublicKey rsaPublicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException {
//RSA加密实例
Cipher cipher = Cipher.getInstance(RSA);
//初始化公钥
cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
//模长转为字节数
int modulusSize = rsaPublicKey.getModulus().bitLength() / 8;
//PKCS PADDING长度为11字节,解密数据是除去这11byte
int maxSingleSize = modulusSize - 11;
//切分字节数,每段不大于maxSingleSize
byte[][] dataArray = splitArray(message.getBytes(), maxSingleSize);
//字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//分组加密,加密后内容写入输出字节流
for (byte[] s : dataArray) {
byteArrayOutputStream.write(cipher.doFinal(s));
}
//使用base64将字节数组转为string类型
return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
}
/**
* 公钥加密
*
* @param message 需要加密的信息
* @param publicKey rsa公钥字符串
* @return 返回信息被加密后的字符串
*/
public static String encryptByPublicKey(String message, String publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeySpecException {
RSAPublicKey rsaPublicKeyByX509 = getRSAPublicKeyByX509(publicKey);
return encryptByPublicKey(message, rsaPublicKeyByX509);
}
/**
* 私钥解密密
*
* @param encryptedMessage 信息加密后的字符串
* @param rsaPrivateKey rsa私钥对象
* @return 返回解密后的字符串
*/
public static String decryptByPrivateKey(String encryptedMessage, RSAPrivateKey rsaPrivateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException {
//RSA加密实例
Cipher cipher = Cipher.getInstance(RSA);
//初始化公钥
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
//加密算法模长
int modulusSize = rsaPrivateKey.getModulus().bitLength() / 8;
byte[] dataBytes = encryptedMessage.getBytes();
//加密做了转码,这里也要用base64转回来
byte[] decodeData = Base64.getDecoder().decode(dataBytes);
//切分字节数,每段不大于maxSingleSize
byte[][] dataArray = splitArray(decodeData, modulusSize);
//字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//分组解密,解密后内容写入输出字节流
for (byte[] s : dataArray) {
byteArrayOutputStream.write(cipher.doFinal(s));
}
//使用base64将字节数组转为string类型
return byteArrayOutputStream.toString();
}
/**
* 私钥解密密
*
* @param encryptedMessage 信息加密后的字符串
* @param privateKey rsa私钥字符串
* @return 返回解密后的字符串
*/
public static String decryptByPrivateKey(String encryptedMessage, String privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeySpecException {
RSAPrivateKey rsaPublicKeyByX509 = getRSAPrivateKeyByPKCS8(privateKey);
return decryptByPrivateKey(encryptedMessage, rsaPublicKeyByX509);
}
/**
* 私钥解密密
*
* @param encryptedMessage 信息加密后的字符串
* @return 返回解密后的字符串
*/
public static String decryptByPrivateKey(String encryptedMessage) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeySpecException {
return decryptByPrivateKey(encryptedMessage, PRIVATE_KEY);
}
/**
* 按指定长度切分数组
*
* @param byteArrayInfo 需要切分的byte数组
* @param maxSize 单个字节数组长度
* @return byte[][]
*/
private static byte[][] splitArray(byte[] byteArrayInfo, int maxSize) {
int dataLen = byteArrayInfo.length;
if (dataLen <= maxSize) {
return new byte[][]{byteArrayInfo};
}
byte[][] result = new byte[(dataLen - 1) / maxSize + 1][];
int resultLen = result.length;
for (int i = 0; i < resultLen; i++) {
if (i == resultLen - 1) {
int sLen = dataLen - maxSize * i;
byte[] single = new byte[sLen];
System.arraycopy(byteArrayInfo, maxSize * i, single, 0, sLen);
result[i] = single;
break;
}
byte[] single = new byte[maxSize];
System.arraycopy(byteArrayInfo, maxSize * i, single, 0, maxSize);
result[i] = single;
}
return result;
}
/**
* 返回公钥
*
* @return String
*/
public static String getPublicKey() {
return PUBLIC_KEY;
}
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeyException, InvalidKeySpecException {
String message = "aaaaaaaaaaaa";
System.out.println("加密前的信息内容:" + message);
System.out.println("-----------------------RSA密钥对象加密解密--------------------------------");
// //生成密钥对象
// List<Key> keyList = RSAUtils.getRsaObject(1024);
// //RSA类型的公钥对象
// RSAPublicKey rsaPublicKey = (RSAPublicKey) keyList.get(0);
// //RSA类型的私钥对象
// RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyList.get(1);
//通过RSA类型的公钥对象加密信息
String encryptedMsg = RSAUtils.encryptByPublicKey(message, PUBLIC_KEY);
System.out.println("加密后的字符串encryptedMsg:\r\n" + encryptedMsg);
//通过RSA类型的私钥对象解密
String decryptedMsg = RSAUtils.decryptByPrivateKey(encryptedMsg, PRIVATE_KEY);
System.out.println("------");
System.out.println("解密后的字符串decryptedMsg:\r\n" + decryptedMsg);
// System.out.println();
// System.out.println("-----------------------字符串类型的密钥对象转为RSA密钥对象后的加密解密--------------------------");
// //生成密钥(字符串类型的密钥)
// List<String> keyStringList = RSAUtils.getRsaKeyString(1024);
// //字符串类型的公钥
// String publicKeyString = keyStringList.get(0);
// System.out.println("生成的公钥,可以把 PUBLIC_KEY 改为此值:\r\n" + publicKeyString);
// //字符串类型的私钥
// String privateKeyString = keyStringList.get(1);
// System.out.println("生成的私钥,可以把 PRIVATE_KEY 改为此值:\r\n" + privateKeyString);
// //将字符串类型的公钥私钥转为RSA类型
// RSAPublicKey rsaPublicKey1 = RSAUtils.getRSAPublicKeyByX509(publicKeyString);
// RSAPrivateKey rsaPrivateKey1 = RSAUtils.getRSAPrivateKeyByPKCS8(privateKeyString);
// //通过字符串公钥转为RSA类型的公钥 加密
// String encryptedMsg1 = RSAUtils.encryptByPublicKey(message, rsaPublicKey1);
// System.out.println("使用字符串转的rsa公钥对象加密后的字符串encryptedMsg1:\r\n" + encryptedMsg1);
// //通过字符串私钥转为RSA类型的私钥 解密
// String decryptedMsg1 = RSAUtils.decryptByPrivateKey(encryptedMsg1, rsaPrivateKey1);
// System.out.println("------");
// System.out.println("使用字符串转的私钥对象解密后的字符串decryptedMsg1:\r\n" + decryptedMsg1);
}
}