vue使用sm2国密加密和后端hutool解密配置
vue使用sm2国密加密和后端hutool解密配置
后端pom文件引入相关依赖1
2
3
4
5
6<!-- sm2国密 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.69</version>
</dependency>
结合hutool工具类,获取公钥,在service层1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/**
* 获取公钥q
*/
private String privatekey;
public String getSM2() {
SM2 sm2 = SmUtil.sm2(); // 创建密钥
// sm2的加解密时有两种方式即 C1C2C3前端的就是0、 C1C3C2前端的就是1,
sm2.setMode(SM2Engine.Mode.C1C3C2);
// 生成私钥
this.privatekey = HexUtil.encodeHexStr(sm2.getPrivateKey().getEncoded());
// 生成公钥
String publickey = HexUtil.encodeHexStr(sm2.getPublicKey().getEncoded());
return publickey;
}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/**
* 获取公钥q
*/
public Result getSM2() {
String publickey = "PUBLICKEY";
String privatekey = "PRIVATEKEY";
String prik = stringRedisTemplate.opsForValue().get(privatekey);
String pubk = stringRedisTemplate.opsForValue().get(publickey);
if (StrUtil.isBlank(prik) || StrUtil.isBlank(pubk)) {
stringRedisTemplate.delete(publickey);
stringRedisTemplate.delete(privatekey);
SM2 sm2 = SmUtil.sm2(); // 创建密钥
// sm2的加解密时有两种方式即 C1C2C3前端的就是0、 C1C3C2前端的就是1,
sm2.setMode(SM2Engine.Mode.C1C3C2);
// 生成私钥
String privakey = HexUtil.encodeHexStr(sm2.getPrivateKey().getEncoded());
// 生成公钥
String publkey = HexUtil.encodeHexStr(sm2.getPublicKey().getEncoded());
stringRedisTemplate.opsForValue().set(publickey, publkey);
stringRedisTemplate.opsForValue().set(privatekey, privakey);
prik = stringRedisTemplate.opsForValue().get(privatekey);
pubk = stringRedisTemplate.opsForValue().get(publickey);
}
return Result.success(pubk);
}
前端vue下载sm-crypto1
npm install sm-crypto --save
代码1
2
3
4request.get("/user/sm").then((res) => {
this.pubk = res.data
console.log(this.pubk)
})1
2
3
4
5this.user.email = this.sm2Encrypt(this.user.email,this.pubk)
this.user.password = this.sm2Encrypt(this.user.password,this.pubk)
console.log(this.user.email)
console.log(this.user.password)
return1
2
3
4
5
6
7sm2Encrypt(data) {
const sm2 = require('sm-crypto').sm2;
console.log(data)
console.log(this.pubk)
const cipherMode = 1; // 1 - C1C3C2,0 - C1C2C3,默认为1
return sm2.doEncrypt(data, this.pubk, cipherMode)
},