package com.lms.jdk8.rsa;
import sun.security.rsa.RSAKeyPairGenerator;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* @Author: 李孟帅
* @Date: 2021-12-09 10:17
* @Description:
*/
public class RSAEncryptor {
private String privateKey;
private String publicKey;
public RSAEncryptor() {
generateKeyPair();
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:26
* @Description TODO 生成密钥对
*/
private void generateKeyPair() {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
// KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
RSAKeyPairGenerator keyPairGenerator = new RSAKeyPairGenerator();
// 初始化密钥对生成器,密钥大小为1024位
keyPairGenerator.initialize(2034, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
// 得到公钥字符串
String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
// 得到私钥字符串
String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
// 将公钥和私钥保存到Map
this.publicKey = publicKeyString;
this.privateKey = privateKeyString;
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:32
* @Description TODO 使用公钥解密
*/
public String encrypt(String content) throws Exception {
return encrypt(content, this.publicKey);
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:55
* @Description TODO 使用公钥加密
*/
public static String encrypt(String content, String publicKey) throws Exception {
//base64编码的公钥
byte[] publicKeyString = Base64.getDecoder().decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyString));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:55
* @Description TODO 使用私钥解密
*/
public String decrypt(String content) throws Exception {
return decrypt(content, this.privateKey);
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:55
* @Description TODO 使用私钥解密
*/
public static String decrypt(String content, String privateKey) throws Exception {
//base64编码的私钥
byte[] privateKeyString = Base64.getDecoder().decode(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyString));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8))));
}
/**
* @Author 李孟帅
* @Date 2021-12-09 11:04
* @Description TODO 使用私钥签名
*/
public String sign(String content) throws Exception {
return sign(content, privateKey);
}
/**
* @Author 李孟帅
* @Date 2021-12-09 11:04
* @Description TODO 使用私钥签名
*/
public static String sign(String content, String privateKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
Signature signature = Signature.getInstance("MD5WithRSA");
signature.initSign(priKey);
signature.update(content.getBytes(StandardCharsets.UTF_8));
byte[] signed = signature.sign();
return Base64.getEncoder().encodeToString(signed);
}
/**
* @Author 李孟帅
* @Date 2021-12-09 11:14
* @Description TODO 验签
*/
public static boolean verifySign(String content, String sign, String publicKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey)));
Signature signature = Signature.getInstance("MD5WithRSA");
signature.initVerify(pubKey);
signature.update(content.getBytes(StandardCharsets.UTF_8));
return signature.verify(Base64.getDecoder().decode(sign));
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:56
* @Description TODO 获取公钥
*/
public String getPublicKey() {
return publicKey;
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:56
* @Description TODO 获取私钥
*/
public String getPrivateKey() {
return privateKey;
}
/**
* @Author 李孟帅
* @Date 2021-12-09 10:56
* @Description TODO 测试
*/
public static void main(String[] args) throws Exception {
RSAEncryptor rsa = new RSAEncryptor();
String publicKey = rsa.getPublicKey();
String privateKey = rsa.getPrivateKey();
System.out.println("公钥:" + publicKey);
System.out.println("私钥:" + privateKey);
System.out.println("数据加密测试 >========================");
System.out.println();
// 加密
String encrypt = rsa.encrypt("中文123");
System.out.println(encrypt);
// 解密
String decrypt = rsa.decrypt(encrypt);
System.out.println(decrypt);
System.out.println("模拟数据传输====》");
System.out.println();
String content = "2021-12-11";
String encrypt1 = RSAEncryptor.encrypt(content, publicKey);
System.out.println("服务端发送加密后的数据:" + encrypt1);
String sign = RSAEncryptor.sign(encrypt1, privateKey);
System.out.println("签名:" + sign);
boolean b = RSAEncryptor.verifySign(encrypt1 , sign, publicKey);
if (b) {
System.out.println("客户端验证签名 成功");
// 一般有两个公钥私钥对,谁发送数据就用对方的公钥加密,这里模拟的的同一个公钥私钥对
content = RSAEncryptor.decrypt(encrypt1, privateKey);
System.out.println("客户端解析数据:" + content);
} else {
System.out.println("客户端验证签名 失败");
}
}
}