RSA加密工具类

该博客介绍了一个RSA加密工具类的Java实现,包括生成密钥对、公钥加密和私钥解密的方法。示例展示了如何使用这些方法进行加解密操作,适用于数据安全场景。

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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值