Skip to content

Commit 67948b2

Browse files
author
WeiziPlus
committed
1.0.5 新增RSA加密解密工具类
1 parent 4d97a6a commit 67948b2

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.weiziplus.common.util;
2+
3+
import lombok.Getter;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
import javax.crypto.Cipher;
7+
import java.nio.charset.StandardCharsets;
8+
import java.security.*;
9+
import java.security.interfaces.RSAPrivateKey;
10+
import java.security.interfaces.RSAPublicKey;
11+
import java.security.spec.PKCS8EncodedKeySpec;
12+
import java.security.spec.X509EncodedKeySpec;
13+
import java.util.Base64;
14+
15+
/**
16+
* RSA加密解密
17+
*
18+
* @author wanglongwei
19+
* @date 2020/06/03 18/09
20+
*/
21+
@Slf4j
22+
@Getter
23+
public class RsaUtils {
24+
25+
/**
26+
* 公钥
27+
*/
28+
private String publicKey;
29+
30+
/**
31+
* 私钥
32+
*/
33+
private String privateKey;
34+
35+
private RsaUtils(String publicKey, String privateKey) {
36+
this.publicKey = publicKey;
37+
this.privateKey = privateKey;
38+
}
39+
40+
/**
41+
* 标识RSA加密算法名字常量
42+
*/
43+
private final static String RSA = "RSA";
44+
45+
/**
46+
* 获取公钥私钥对
47+
*
48+
* @return
49+
*/
50+
public static RsaUtils getKeyPair() {
51+
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
52+
KeyPairGenerator keyPairGen = null;
53+
try {
54+
keyPairGen = KeyPairGenerator.getInstance(RSA);
55+
} catch (NoSuchAlgorithmException e) {
56+
log.warn("RSA生成公钥私钥对出错,详情:" + e);
57+
return null;
58+
}
59+
// 初始化密钥对生成器
60+
keyPairGen.initialize(1024, new SecureRandom());
61+
// 生成一个密钥对,保存在keyPair中
62+
KeyPair keyPair = keyPairGen.generateKeyPair();
63+
// 得到私钥
64+
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
65+
// 得到公钥
66+
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
67+
//得到公钥字符串
68+
String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
69+
// 得到私钥字符串
70+
String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
71+
return new RsaUtils(publicKeyString, privateKeyString);
72+
}
73+
74+
/**
75+
* 私钥解密
76+
*
77+
* @param privateKey
78+
* @param content
79+
* @return
80+
*/
81+
public static String privateDecrypt(String privateKey, String content) {
82+
try {
83+
//64位解码加密后的字符串
84+
byte[] inputByte = Base64.getDecoder().decode(content);
85+
//base64编码的私钥
86+
byte[] decoded = Base64.getDecoder().decode(privateKey);
87+
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded));
88+
//RSA解密
89+
Cipher cipher = Cipher.getInstance(RSA);
90+
cipher.init(Cipher.DECRYPT_MODE, priKey);
91+
return new String(cipher.doFinal(inputByte), StandardCharsets.UTF_8);
92+
} catch (Exception e) {
93+
log.warn("RSA私钥解密出错,详情:" + e);
94+
return null;
95+
}
96+
}
97+
98+
/**
99+
* 公钥加密
100+
*
101+
* @param publicKey
102+
* @param content
103+
* @return
104+
*/
105+
public static String publicEncrypt(String publicKey, String content) {
106+
try {
107+
//base64编码的公钥
108+
byte[] decoded = Base64.getDecoder().decode(publicKey);
109+
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
110+
//RSA加密
111+
Cipher cipher = Cipher.getInstance(RSA);
112+
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
113+
return Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
114+
} catch (Exception e) {
115+
log.warn("RSA公钥加密出错,详情:" + e);
116+
return null;
117+
}
118+
}
119+
120+
}

vue/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vue/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"core-js": "^3.6.5",
1313
"crypto-js": "^4.0.0",
1414
"element-ui": "^2.13.1",
15+
"jsencrypt": "^3.0.0-rc.1",
1516
"nprogress": "^0.2.0",
1617
"uglifyjs-webpack-plugin": "^2.2.0",
1718
"vue": "^2.6.11",

vue/src/utils/global_function.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import {Message, MessageBox, prompt} from 'element-ui';
33
/*引入AES加密*/
44
import CryptoJs from "crypto-js";
5+
/*引入jsencrypt*/
6+
import JsEncrypt from 'jsencrypt';
57

68
//某种程度上加混淆
79
let {enc, mode, pad} = CryptoJs;
@@ -426,6 +428,20 @@ function sortAscii(obj) {
426428
return str.substr(0, str.length - 1);
427429
}
428430

431+
/**
432+
* rsa公钥加密
433+
* @param key
434+
* @param obj
435+
* @returns {PromiseLike<ArrayBuffer> | *}
436+
*/
437+
function rsaPublicEncrypt(key, obj) {
438+
let encrypt = new JsEncrypt();
439+
//设置加密公钥
440+
encrypt.setPublicKey(key);
441+
//返回通过encryptLong方法加密后的结果
442+
return encrypt.encrypt(obj);
443+
}
444+
429445
/**
430446
* 将方法暴露出去
431447
*/
@@ -451,5 +467,6 @@ export default {
451467
setSessionStorage,
452468
getLocationStorage,
453469
setLocationStorage,
454-
sortAscii
470+
sortAscii,
471+
rsaPublicEncrypt
455472
};

0 commit comments

Comments
 (0)