Java RSA加解密-非对称加密-公钥私钥加解密(使用hutool工具)

本文介绍了如何在Java项目中利用Hutool库处理RSA加密和解密,包括在Linux环境下生成公私钥、导入项目、配置pom依赖以及提供HutoolAPI的详细示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前一篇帖子(https://blog.csdn.net/u014137486/article/details/136413532)展示了使用原生Java进行RSA加解密,本文介绍下使用当下流行的Hutool工具进行RSA加解密的用法。

目录

一、在Linux环境下生成公钥、私钥文件

二、将生成的公私钥文件导入项目中并移除pem文件的前后公私钥标记

三、pom文件中引入hutool依赖

四、使用hutool API编码


一、在Linux环境下生成公钥、私钥文件

生成公私钥文件此处不再描述,请参考笔者的另一篇帖子(https://blog.csdn.net/u014137486/article/details/136413532

二、将生成的公私钥文件导入项目中并移除pem文件的前后公私钥标记

比如导入private_key.pem, public_key.pem到项目classpath的keys目录中,

并移除pem文件中的以下内容,只保留密钥内容即可,

-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----

三、pom文件中引入hutool依赖

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


四、使用hutool API编码

以下为公钥加密、私钥解密的完整示例代码,

package com.frank.project.test.rsa;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

public class RSAUtilUseHutool {

	public static void main(String[] args) throws Exception {
		// 在classpath下的公私钥文件
        String pubKeyFilePathInClasspath = "keys/public_key.pem";
        String priKeyFilePathInClasspath = "keys/private_key.pem";
        // 要加密的文本
        String text = "和发票是否激活皮肤设计大";
        
        long startTime = System.currentTimeMillis();
        // 获取公钥并进行rsa加密
        String encryptedString = encrypt(text, getPublicKey(pubKeyFilePathInClasspath));
        // 获取私钥并进行rsa解密
        String decryptedString = decrypt(encryptedString, getPrivateKey(priKeyFilePathInClasspath));
        
        System.out.println("hutool rsa cost:" + (System.currentTimeMillis() - startTime) + "ms");
        
        System.out.println("Encrypted String - " + encryptedString);
        System.out.println("Decrypted String - " + decryptedString);
	}
	
	/**
	 * 使用公钥加密
	 * @param text 待使用rsa加密的明文
	 * @param publicKey 文本格式的公钥内容(不包含-----BEGIN PUBLIC KEY-----,-----END PUBLIC KEY-----)
	 * @return
	 */
	private static String encrypt(String text, String publicKey){
		return new RSA(null, publicKey).encryptHex(text, KeyType.PublicKey);
	}
	
	/**
	 * 使用私钥解密
	 * @param encryptedString 使用rsa加密的密文串
	 * @param privateKey 文本格式的私钥内容(不包含-----BEGIN PRIVATE KEY-----,-----END PRIVATE KEY-----)
	 * @return
	 */
	private static String decrypt(String encryptedString, String privateKey){
		return new RSA(privateKey, null).decryptStr(encryptedString, KeyType.PrivateKey);
	}
	
	/**
	 * 从公钥文件中读取公钥数据
	 * 
	 * @param pubKeyFilePathInClasspath classpath下的公钥文件路径
	 * @return
	 * @throws Exception
	 */
	private static String getPublicKey(String pubKeyFilePathInClasspath)
			throws Exception {
		return new FileReader(getAbsolutePath(pubKeyFilePathInClasspath))
				.readString();

	}

	/**
	 * 从私钥文件中读取私钥数据
	 * 
	 * @param priKeyFilePathInClasspath classpath下的私钥文件路径
	 * @return
	 * @throws Exception
	 */
	private static String getPrivateKey(String priKeyFilePathInClasspath)
			throws Exception {
		return new FileReader(getAbsolutePath(priKeyFilePathInClasspath))
				.readString();
	}

	/**
	 * 获取classpath下指定文件的绝对路径
	 * @param filePathInClasspath classpath下文件路径
	 * @return
	 */
	private static String getAbsolutePath(String filePathInClasspath) {
		return RSAUtilUseHutool.class.getClassLoader()
				.getResource(filePathInClasspath).getPath();
	}

}

可原生Java API一样,hutool同样支持使用私钥加密、公钥解密,只需要传入的key不同即可,

// 使用私钥加密,使用公钥解密
encryptedString = encrypt(text, getPrivateKey(pubKeyFilePathInClasspath));
decryptedString = decrypt(encryptedString, getPublicKey(priKeyFilePathInClasspath));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

笑看人生三百年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值