之前一篇帖子(https://blog.csdn.net/u014137486/article/details/136413532)展示了使用原生Java進行RSA加解密,本文介紹下使用當(dāng)下流行的Hutool工具進行RSA加解密的用法。
目錄
一、在Linux環(huán)境下生成公鑰、私鑰文件
二、將生成的公私鑰文件導(dǎo)入項目中并移除pem文件的前后公私鑰標(biāo)記
三、pom文件中引入hutool依賴
四、使用hutool API編碼
一、在Linux環(huán)境下生成公鑰、私鑰文件
生成公私鑰文件此處不再描述,請參考筆者的另一篇帖子(https://blog.csdn.net/u014137486/article/details/136413532)
二、將生成的公私鑰文件導(dǎo)入項目中并移除pem文件的前后公私鑰標(biāo)記
比如導(dǎo)入private_key.pem, public_key.pem到項目classpath的keys目錄中,
并移除pem文件中的以下內(nèi)容,只保留密鑰內(nèi)容即可,
-----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編碼
以下為公鑰加密、私鑰解密的完整示例代碼,文章來源:http://www.zghlxwxcb.cn/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 = "和發(fā)票是否激活皮膚設(shè)計大";
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 文本格式的公鑰內(nèi)容(不包含-----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 文本格式的私鑰內(nèi)容(不包含-----BEGIN PRIVATE KEY-----,-----END PRIVATE KEY-----)
* @return
*/
private static String decrypt(String encryptedString, String privateKey){
return new RSA(privateKey, null).decryptStr(encryptedString, KeyType.PrivateKey);
}
/**
* 從公鑰文件中讀取公鑰數(shù)據(jù)
*
* @param pubKeyFilePathInClasspath classpath下的公鑰文件路徑
* @return
* @throws Exception
*/
private static String getPublicKey(String pubKeyFilePathInClasspath)
throws Exception {
return new FileReader(getAbsolutePath(pubKeyFilePathInClasspath))
.readString();
}
/**
* 從私鑰文件中讀取私鑰數(shù)據(jù)
*
* @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不同即可,文章來源地址http://www.zghlxwxcb.cn/news/detail-856017.html
// 使用私鑰加密,使用公鑰解密
encryptedString = encrypt(text, getPrivateKey(pubKeyFilePathInClasspath));
decryptedString = decrypt(encryptedString, getPublicKey(priKeyFilePathInClasspath));
到了這里,關(guān)于Java RSA加解密-非對稱加密-公鑰私鑰加解密(使用hutool工具)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!