package cn.bywin.bydata.common.base.util;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @author: zhanbf
* @Date: 2023/6/19 16:54
*/
public class RSAUtils {
/**
* 类型
*/
public static final String ENCRYPT_TYPE = "RSA";
/**
* 获取公钥的key
*/
public static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq26+9uPB47gaGM9cXibc29HZFfDUadJJcXobUqexpYXuOO7EhXMsG4MvO3uqewuS/cNZdBE6arrkWKLwzyCweneqSzzJTkRKJr5WezGNNMftPoZ1QMgv/9OOlO/TinoUL4PtRKmLi/ZCJDLfgcbBJOf6OkciD4DwDjSe/7Rv+W159mGgwq9kFpS1BS7AGyPUVNdQWpRhJFrEBsr3A+MVHuGWOHUouvs2xGcdRdiX31/+PKZub9d9G4cnQz+rS1MGn/M56mknogjqykjZqKrFH9Ahamj3Kv14gIRmufhRdyenB/8HsB8ZopC6Nt+VeVbUmNeTRO1Ea/3vwMCa892v9wIDAQAB";
/**
* 获取私钥的key
*/
public static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCrbr7248HjuBoYz1xeJtzb0dkV8NRp0klxehtSp7Glhe447sSFcywbgy87e6p7C5L9w1l0ETpquuRYovDPILB6d6pLPMlOREomvlZ7MY00x+0+hnVAyC//046U79OKehQvg+1EqYuL9kIkMt+BxsEk5/o6RyIPgPAONJ7/tG/5bXn2YaDCr2QWlLUFLsAbI9RU11BalGEkWsQGyvcD4xUe4ZY4dSi6+zbEZx1F2JffX/48pm5v130bhydDP6tLUwaf8znqaSeiCOrKSNmoqsUf0CFqaPcq/XiAhGa5+FF3J6cH/wewHxmikLo235V5VtSY15NE7URr/e/AwJrz3a/3Ag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MdDi37uOBel1Rt0unTPHIlhpGX5o8eNRpvekmhhimYZymc3C/aFZdoeHSlwK7I0KlryP3y+RQ8KzlOGqWC7IwuWAfVvcN2J73TNnTFG4eTTDrCc5RTuuNSKjGuaXoGseRoN9crLnWK6s93fls230g=";
/**
* 生成秘钥对
*/
public static Map<String, String> generateKeyPair() {
try {
KeyPair pair = SecureUtil.generateKeyPair(ENCRYPT_TYPE);
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();
// 获取 公钥和私钥 的 编码格式(通过该 编码格式 可以反过来 生成公钥和私钥对象)
byte[] pubEncBytes = publicKey.getEncoded();
byte[] priEncBytes = privateKey.getEncoded();
// 把 公钥和私钥 的 编码格式 转换为 Base64文本 方便保存
String pubEncBase64 = Base64.getEncoder().encodeToString(pubEncBytes);
String priEncBase64 = Base64.getEncoder().encodeToString(priEncBytes);
Map<String, String> map = new HashMap<String, String>(2);
map.put("PUBLIC_KEY", pubEncBase64);
map.put("PRIVATE_KEY", priEncBase64);
return map;
} catch (Exception e) {
throw new RuntimeException("生成密钥对失败:"+ExceptionUtil.getExceptionMessage(e));
}
}
/**
* 公钥加密
*
* @param content 要加密的内容
* @param publicKey 公钥
*/
public static String encrypt(String content, String publicKey) {
try {
RSA rsa = new RSA(null, publicKey);
return rsa.encryptBase64(content, KeyType.PublicKey);
} catch (Exception e) {
throw new RuntimeException("加密失败:"+ExceptionUtil.getExceptionMessage(e));
}
}
/**
* 私钥解密
*
* @param content 要解密的内容
* @param privateKey 私钥
*/
public static String decrypt(String content, String privateKey) {
try {
RSA rsa = new RSA(privateKey, null);
return rsa.decryptStr(content, KeyType.PrivateKey);
} catch (Exception e) {
throw new RuntimeException("解密失败:"+ExceptionUtil.getExceptionMessage(e));
}
}
public static void main(String[] args) {
// Map<String, String> keyMap = generateKeyPair();
// String PUBLIC_KEY = keyMap.get("PUBLIC_KEY");
// String PRIVATE_KEY = keyMap.get("PRIVATE_KEY");
System.out.println("公钥:"+PUBLIC_KEY);
System.out.println("私钥:"+PRIVATE_KEY);
String content = "Bywin@123";
System.out.println("原始数据:"+content);
String encrypt = RSAUtils.encrypt(content, PUBLIC_KEY);
System.out.println("加密:"+encrypt);
String decrypt = RSAUtils.decrypt(encrypt, PRIVATE_KEY);
System.out.println("解密:"+decrypt);
}
}