package com.example.xxx.utls;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
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.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: deng
* @Date: 2024/2/6 10:11
*/
public class EncryptUtil {
public static Map<String, String> keyMap = new HashMap<>();
public static void main(String[] args) throws Exception {
generatorKeys();
String content = "fasf3432#@(";
String encrypt = encrypt(content, keyMap.get("pubk"));
decrypt(encrypt, keyMap.get("prik"));
}
/**
* 生成密钥对
*/
public static void generatorKeys() throws NoSuchAlgorithmException {
KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
// 初始化长度
rsa.initialize(2048);
// 生成密钥对
KeyPair keyPair = rsa.generateKeyPair();
// 获取公钥
PublicKey publicKey = keyPair.getPublic();
// 获取私钥
PrivateKey privateKey = keyPair.getPrivate();
// 获取公钥,私钥的编码
byte[] publicKeyEncoded = publicKey.getEncoded();
byte[] privateKeyEncoded = privateKey.getEncoded();
// 生成密钥对字符串
Base64.Encoder encoder = Base64.getEncoder();
String pulicKeyString = encoder.encodeToString(publicKeyEncoded);
String privateKeyString = encoder.encodeToString(privateKeyEncoded);
System.out.println("pubk:"+pulicKeyString);
System.out.println("prik:"+privateKeyString);
keyMap.put("pubk", pulicKeyString);
keyMap.put("prik", privateKeyString);
}
/**
* 加密
* @param content
* @param pubk
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String encrypt(String content, String pubk) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// 对公钥字符串base64解码
byte[] pubkDecode = Base64.getDecoder().decode(pubk);
RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubkDecode));
Cipher cipher = Cipher.getInstance("RSA");
// 初始化加密模式
cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
byte[] doFinal = cipher.doFinal(content.getBytes());
// 生成加密后的base64编码字符串
String encryptString = Base64.getEncoder().encodeToString(doFinal);
System.out.println("encryptString: "+encryptString);
return encryptString;
}
/**
* 解密
* @param content
* @param prik
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static void decrypt(String content, String prik) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// base64解码
Base64.Decoder decoder = Base64.getDecoder();
byte[] contentDecode = decoder.decode(content);
byte[] prikDecode = decoder.decode(prik);
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(prikDecode));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
byte[] doFinal = cipher.doFinal(contentDecode);
// String decryptStr = new String(doFinal, 0, 0, doFinal.length);
String decryptStr = new String(doFinal);
System.out.println("decryptStr: "+decryptStr);
}
}
运行结果: