RSA之基于hutool的快速实现

RSA之基于hutool的快速实现

刚刚临时帮同事写个rsa加密的工具包,基于hutool的快速实现,随手给需要的小朋友,
非依赖hutool的请看我另外的文章,RSA加密算法Java应用解析-目前地球上最有影响力的加密算法

RSA工具包

2020年08月02日21:06:42 关于RSA就不多说了,直接上干货

一、首先获取公私钥

1
2
3
4
5
6
7
8

// 第一次使用时获取,获取后保存公私钥,不要重复获取

Map<String, String> stringStringMap = RsaUtils.generateKeyPair();

String RSAPublicKey = stringStringMap.get(PUBLIC_KEY);

String RSAPrivateKey = stringStringMap.get(PRIVATE_KEY);

二、公钥加密

1
2
3
4
5
6

//直接调用encrypt方法

String content = "拳打南山敬老院,脚踢北海幼儿园";

String encrypt = RsaUtils.encrypt(content, RSAPublicKey);

三、私钥解密

1
String decrypt = RsaUtils.decrypt(encrypt, RSAPrivateKey);

附代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import sun.misc.BASE64Encoder;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

public class RsaUtils {

/**
* 类型
*/
public static final String ENCRYPT_TYPE = "RSA";

/**
* 获取公钥的key
*/
private static final String PUBLIC_KEY = "RSAPublicKey";

/**
* 获取私钥的key
*/
private static final String PRIVATE_KEY = "RSAPrivateKey";

/**
* 公钥加密
* @param content 要加密的内容
* @param publicKey 公钥
*/
public static String encrypt(String content, PublicKey publicKey) {
try{
RSA rsa = new RSA(null,publicKey);
return rsa.encryptBase64(content, KeyType.PublicKey);
}catch (Exception e){
e.printStackTrace();
}
return null;
}

/**
* 公钥加密
* @param content 要加密的内容
* @param publicKey 公钥
*/
public static String encrypt(String content, String publicKey) {
try{
RSA rsa = new RSA(null,publicKey);
return rsa.encryptBase64(content, KeyType.PublicKey);
}catch (Exception e){
e.printStackTrace();
}
return null;
}


/**
* 私钥解密
* @param content 要解密的内容
* @param privateKey 私钥
*/
public static String decrypt(String content, PrivateKey privateKey) {
try {
RSA rsa = new RSA(privateKey,null);
return rsa.decryptStr(content, KeyType.PrivateKey);
} catch (Exception e){
e.printStackTrace();
}
return null;
}

/**
* 私钥解密
* @param content 要解密的内容
* @param privateKey 私钥
*/
public static String decrypt(String content, String privateKey) {
try {
RSA rsa = new RSA(privateKey,null);
return rsa.decryptStr(content, KeyType.PrivateKey);
} catch (Exception e){
e.printStackTrace();
}
return null;
}

/**
* 获取公私钥-请获取一次后保存公私钥使用
* @return
*/
public static Map<String,String> generateKeyPair() {
try {
KeyPair pair = SecureUtil.generateKeyPair(ENCRYPT_TYPE);
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();
// 获取 公钥和私钥 的 编码格式(通过该 编码格式 可以反过来 生成公钥和私钥对象)
byte[] pubEncBytes = publicKey.getEncoded();
byte[] priEncBytes = privateKey.getEncoded();

// 把 公钥和私钥 的 编码格式 转换为 Base64文本 方便保存
String pubEncBase64 = new BASE64Encoder().encode(pubEncBytes);
String priEncBase64 = new BASE64Encoder().encode(priEncBytes);

Map<String, String> map = new HashMap<String, String>(2);
map.put(PUBLIC_KEY,pubEncBase64);
map.put(PRIVATE_KEY,priEncBase64);

return map;
} catch (Exception e){
e.printStackTrace();
}
return null;
}


}

关注Github:1/2极客

关注博客:御前提笔小书童

关注网站:HuMingfeng

关注公众号:开发者的花花世界


本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。

本文链接:https://royalscholar.cn/2020/08/02/RSA之基于hutool的快速实现/

# JAVA, RSA

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×