RSA加密工具类,也是抄来的(看了好几个大佬的),我自己跑过了
package com.lxb.manage.common.utils;
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;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName RsaUtil
* @Description RSA加密工具类
* @Author
* @Date 2020/11/15 21:15
*/
public class RsaUtil {
/**
* 生成密钥对
* @param keySize 密钥的长度 512 1024 2048
* @return 返回的密钥对
* @throws NoSuchAlgorithmException 异常
*/
public static Map<String, String> genKeyPair(int keySize) throws NoSuchAlgorithmException {
Map<String, String> keyMap = new HashMap<>(2);
//KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//初始化密钥对生成器
keyPairGen.initialize(keySize, new SecureRandom());
//生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.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
//公钥
keyMap.put("pubKey", publicKeyString);
//私钥
keyMap.put("priKey", privateKeyString);
return keyMap;
}
/**
* RSA公钥加密
*
* @param str 加密字符串
* @param publicKey 公钥
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt(String str, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.getDecoder().decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
}
/**
* RSA私钥解密
*
* @param str 加密字符串
* @param privateKey 私钥
* @return 明文
* @throws Exception 解密过程中的异常信息
*/
public static String decrypt(String str, String privateKey) throws Exception {
//64位解码加密后的字符串
byte[] inputByte = Base64.getDecoder().decode(str);
//base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
public static void main(String[] args) throws Exception {
//生成公钥和私钥
Map<String, String> keyMap = RsaUtil.genKeyPair(1024);
String pubKey = keyMap.get("pubKey");
String priKey = keyMap.get("priKey");
//加密字符串
System.out.println("公钥:" + pubKey);
System.out.println("私钥:" + priKey);
String message = "test";
//通过原文和公钥加密
String messageEn = encrypt(message, pubKey);
System.out.println("密文:" + messageEn);
//通过密文和私钥解密
String messageDe = decrypt(messageEn, priKey);
System.out.println("解密:" + messageDe);
}
}
该博客介绍了一个RSA加密工具类的Java实现,包括生成密钥对、公钥加密和私钥解密的方法。示例展示了如何使用这些方法进行加解密操作,适用于数据安全场景。
604

被折叠的 条评论
为什么被折叠?



