使用hutool工具生成非对称加密公私密钥以及使用案例

1.导入hutool依赖

  <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.8.18</version>
            </dependency>

2.直接复制代码

package com.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.security.KeyPair;
import java.util.LinkedList;

/**
 * 非对称加密工具类
 *
 * @author edimen
 */
public class AsymmetricAlgorithmUtil {


    /**
     * 公钥加密(解密就要用到对应的私钥)
     *
     * @param msg    明文信息
     * @param pubKey 公钥,用来加密明文
     * @return
     */
    public static String encryptByPublic(String msg, String pubKey) {
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), null, pubKey);
        return rsa.encryptBase64(msg, KeyType.PublicKey);
    }

    /**
     * 私钥解密
     *
     * @param encryptMsg 公钥加密的密文
     * @param priKey     私钥,用来解密密文
     * @return
     */
    public static String decryptByPrivate(String encryptMsg, String priKey) {
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), priKey, null);
        return rsa.decryptStr(encryptMsg, KeyType.PrivateKey);
    }

    /**
     * 私钥加密(解密就要用到对应的公钥)
     *
     * @param msg    明文信息
     * @param priKey 私钥,用来加密明文
     * @return
     */
    public static String encryptByPrivate(String msg, String priKey) {
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), priKey, null);
        return rsa.encryptBase64(msg, KeyType.PrivateKey);
    }


    /**
     * 公钥解密
     *
     * @param encryptMsg 密文
     * @param pubKey     公钥,用来解密
     * @return
     */
    public static String decryptByPublic(String encryptMsg, String pubKey) {
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), null, pubKey);
        return rsa.decryptStr(encryptMsg, KeyType.PublicKey);
    }

    /**
     * 获取公私钥集合
     *
     * @return
     */
    public static LinkedList<String> getPriKeyAndPubKey() {
        KeyPair pair = SecureUtil.generateKeyPair("RSA");
        String privateKey = Base64.encode(pair.getPrivate().getEncoded());
        String publicKey = Base64.encode(pair.getPublic().getEncoded());
        LinkedList<String> keys = new LinkedList<>();
        keys.add(privateKey);
        keys.add(publicKey);
        return keys;
    }
}

3.测试案例

    public static void main(String[] args) {

        LinkedList<String> priKeyAndPubKey = AsymmetricAlgorithmUtil.getPriKeyAndPubKey();
        String privateKey = priKeyAndPubKey.get(0);
        String publicKey = priKeyAndPubKey.get(1);
        String text = "HelloWorld";

        String encryptByPublic = AsymmetricAlgorithmUtil.encryptByPublic(text, publicKey);
        System.out.println(encryptByPublic);
        String s = AsymmetricAlgorithmUtil.decryptByPrivate(encryptByPublic, privateKey);
        System.out.println("公钥加密私钥解密:"+s);

        String encryptByPrivate = AsymmetricAlgorithmUtil.encryptByPrivate(text, privateKey);
        System.out.println(encryptByPrivate);
        String s1 = AsymmetricAlgorithmUtil.decryptByPublic(encryptByPrivate,publicKey);
        System.out.println("私钥加密公钥解密:"+s1);
    }

4.运行结果图:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞机飞上天空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值