之前一篇帖子(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编码
以下为公钥加密、私钥解密的完整示例代码,文章来源:https://www.toymoban.com/news/detail-856017.html
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不同即可,文章来源地址https://www.toymoban.com/news/detail-856017.html
// 使用私钥加密,使用公钥解密
encryptedString = encrypt(text, getPrivateKey(pubKeyFilePathInClasspath));
decryptedString = decrypt(encryptedString, getPublicKey(priKeyFilePathInClasspath));
到了这里,关于Java RSA加解密-非对称加密-公钥私钥加解密(使用hutool工具)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!