forked from opentiny/tiny-engine-backend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM2EncryptionUtil.java
More file actions
129 lines (104 loc) · 4.4 KB
/
SM2EncryptionUtil.java
File metadata and controls
129 lines (104 loc) · 4.4 KB
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
120
121
122
123
124
125
126
127
128
129
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
package com.tinyengine.it.login.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* SM2工具类
*/
public class SM2EncryptionUtil {
static {
// 注册Bouncy Castle Provider
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
}
/**
* SM2获取密钥对
*/
public static KeyPair generateSM2KeyPair() throws Exception {
// 获取ECC算法的KeyPairGenerator实例
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
// 使用国密SM2推荐的椭圆曲线参数:prime256v1 或 sm2p256v1
// 在Bouncy Castle中,通常使用 "sm2p256v1"
ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
// 初始化密钥对生成器
keyPairGenerator.initialize(sm2Spec, new SecureRandom());
// 生成并返回密钥对
return keyPairGenerator.generateKeyPair();
}
/**
* SM2加密
*/
public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
// 获取SM2加密的Cipher实例,使用 "SM2" 算法
Cipher cipher = Cipher.getInstance("SM2", BouncyCastleProvider.PROVIDER_NAME);
// 初始化为加密模式,传入公钥
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 执行加密
byte[] cipherText = cipher.doFinal(plainText.getBytes("UTF-8"));
// 将二进制密文转换为Base64字符串,便于传输和显示
return Base64.getEncoder().encodeToString(cipherText);
}
/**
* SM2解密
*/
public static String decrypt(String base64CipherText, PrivateKey privateKey) throws Exception {
// 获取SM2解密的Cipher实例
Cipher cipher = Cipher.getInstance("SM2", BouncyCastleProvider.PROVIDER_NAME);
// 初始化为解密模式,传入私钥
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 先将Base64字符串解码为二进制
byte[] cipherText = Base64.getDecoder().decode(base64CipherText);
// 执行解密
byte[] decryptedText = cipher.doFinal(cipherText);
// 将解密后的字节数组转换为字符串
return new String(decryptedText, "UTF-8");
}
/**
* base64PublicKey 解码
*/
public static PublicKey getPublicKeyFromBase64(String base64PublicKey) throws Exception {
// Base64 解码
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
// 创建密钥规范
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
// 获取密钥工厂(根据算法选择)
KeyFactory keyFactory = KeyFactory.getInstance("EC"); // 对于 SM2/ECC
// 生成公钥
return keyFactory.generatePublic(keySpec);
}
/**
* base64PrivateKey 解码
*/
public static PrivateKey getPrivateKeyFromBase64(String base64PrivateKey) throws Exception {
// Base64 解码
byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
// 创建密钥规范
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
// 获取密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance("EC"); // 对于 SM2/ECC
// 生成私钥
return keyFactory.generatePrivate(keySpec);
}
}