1.簡(jiǎn)單快速集成,飲用hutool工具包實(shí)現(xiàn)
1)引入hutool包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>
2) 制作好自己的rsa公私鑰文件,通過openssl實(shí)現(xiàn)即可,直接放到resources下面?
3)讀取rsa文件,進(jìn)行加解密實(shí)現(xiàn),公鑰加密,私鑰解密,注意點(diǎn)都在里面注釋了。
package com.shebao.controller.wechat;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class HutoolRSATest {
public static void main(String[] args) throws IOException {
String en = testEncrypt();
testDecrypt(en);
}
/**
* 私鑰解密
* @param content 加密了的內(nèi)容
* @throws IOException
*/
public static void testDecrypt(String content) throws IOException {
Resource resource = new ClassPathResource("rsa_private_key.pem");
System.out.println(resource.getFile().getPath());
BufferedReader br = new BufferedReader(new FileReader(resource.getFile().getPath()));
/**
* 這個(gè)巨重要,會(huì)篩選掉密鑰內(nèi)容中的首行標(biāo)識(shí)字段
*/
String s = br.readLine();
String str = "";
s = br.readLine();
while (s.charAt(0) != '-') {
str += s + "\r";
s = br.readLine();
}
RSA rsa = new RSA(str,null);
String s1 = rsa.decryptStr(content, KeyType.PrivateKey);
System.out.println(s1);
}
/**
* 公鑰加密
* @return
* @throws IOException
*/
public static String testEncrypt() throws IOException {
String content = "這是糊涂工具包hutool";
Resource resource = new ClassPathResource("rsa_public_key.pem");
System.out.println(resource.getFile().getPath());
BufferedReader br = new BufferedReader(new FileReader(resource.getFile().getPath()));
String s = br.readLine();
String str = "";
s = br.readLine();
while (s.charAt(0) != '-') {
str += s + "\r";
s = br.readLine();
}
RSA rsa = new RSA(null,str);
String result = rsa.encryptBase64(content, KeyType.PublicKey);
System.out.println(result);
return result;
}
}
2.不借助工具,直接引用java?security包實(shí)現(xiàn)功能
1)公私鑰依然放在resources下
2)公鑰加密,私鑰解密
3)java.security.Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider() );這句話很重要,不過別亂用,容易導(dǎo)致內(nèi)存泄漏:可以參看如下文章
每次New一個(gè)BouncyCastleProvider導(dǎo)致的內(nèi)存泄漏_shenhaiwen的博客-CSDN博客
我的實(shí)例改良了這里文章來源:http://www.zghlxwxcb.cn/news/detail-694905.html
4)要注意base64的解碼和編碼文章來源地址http://www.zghlxwxcb.cn/news/detail-694905.html
package com.shebao.controller.wechat;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.crypto.Cipher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class TestRSA {
private static String content = "你好呀RSA";
public static void main(String[] args) throws Exception {
PublicKey publicKey = getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content.getBytes());
byte[] bytes1 = Base64.encodeBase64(bytes);
String result = new String(bytes1,"utf-8");
System.out.println(result);
test(result);
}
public static void test(String en) throws Exception {
PrivateKey privateKey = getPrivate();
KeyFactory keyFactory1 = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance(keyFactory1.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes2 = cipher.doFinal(Base64.decodeBase64(en));
System.out.println(new String(bytes2, StandardCharsets.UTF_8));
}
public static PrivateKey getPrivate() throws Exception {
Resource resource = new ClassPathResource("rsa_private_key.pem");
System.out.println(resource.getFile().getPath());
BufferedReader br = new BufferedReader(new FileReader(resource.getFile().getPath()));
/**
* 這個(gè)巨重要,會(huì)篩選掉密鑰內(nèi)容中的首行標(biāo)識(shí)字段
*/
String s = br.readLine();
/**
* 這里是讀取的私鑰文件
*/
String str = "";
s = br.readLine();
while (s.charAt(0) != '-') {
str += s + "\r";
s = br.readLine();
}
Base64 base64 = new Base64();
byte[] b = base64.decode(str);
/**
* 這個(gè)也是巨重要的,不信可以去了試試,也有另一種方式去解決這個(gè)問題,沒試
*/
BouncyCastleProvider instance = getInstance();
if (Security.getProvider(instance.getName()) == null) {
java.security.Security.addProvider(
instance
);
}
// 生成私匙
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(b);
PrivateKey privateKey = kf.generatePrivate(keySpec);
return privateKey;
}
public static PublicKey getPublicKey() throws Exception {
Resource resource = new ClassPathResource("rsa_public_key.pem");
System.out.println(resource.getFile().getPath());
BufferedReader br = new BufferedReader(new FileReader(resource.getFile().getPath()));
String s = br.readLine();
/**
* 這里是讀取的公鑰文件
*/
String public_key = "";
s = br.readLine();
while (s.charAt(0) != '-') {
public_key += s + "\r";
s = br.readLine();
}
Base64 base64 = new Base64();
byte[] decode = base64.decode(public_key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decode);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
private static org.bouncycastle.jce.provider.BouncyCastleProvider bouncyCastleProvider = null;
public static synchronized org.bouncycastle.jce.provider.BouncyCastleProvider getInstance() {
if (bouncyCastleProvider == null) {
bouncyCastleProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
}
return bouncyCastleProvider;
}
}
到了這里,關(guān)于RSA 公私鑰加解密Java實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!