-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSM4Utils.java
More file actions
186 lines (164 loc) · 4.09 KB
/
SM4Utils.java
File metadata and controls
186 lines (164 loc) · 4.09 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.lzk.gdut.sm;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SM4Utils
{
public String secretKey = "";
public String iv = "";
public boolean hexString = false;
public SM4Utils()
{
}
public String encryptData_ECB(String plainText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_ENCRYPT;
byte[] keyBytes;
if (hexString)
{
keyBytes = Util.hexStringToBytes(secretKey);
}
else
{
keyBytes = secretKey.getBytes();
}
SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("GBK"));
String cipherText = new BASE64Encoder().encode(encrypted);
if (cipherText != null && cipherText.trim().length() > 0)
{
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(cipherText);
cipherText = m.replaceAll("");
}
return cipherText;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public String decryptData_ECB(String cipherText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_DECRYPT;
byte[] keyBytes;
if (hexString)
{
keyBytes = Util.hexStringToBytes(secretKey);
}
else
{
keyBytes = secretKey.getBytes();
}
SM4 sm4 = new SM4();
sm4.sm4_setkey_dec(ctx, keyBytes);
byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
return new String(decrypted, "GBK");
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public String encryptData_CBC(String plainText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_ENCRYPT;
byte[] keyBytes;
byte[] ivBytes;
if (hexString)
{
keyBytes = Util.hexStringToBytes(secretKey);
ivBytes = Util.hexStringToBytes(iv);
}
else
{
keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes();
}
SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("GBK"));
String cipherText = new BASE64Encoder().encode(encrypted);
if (cipherText != null && cipherText.trim().length() > 0)
{
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(cipherText);
cipherText = m.replaceAll("");
}
return cipherText;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public String decryptData_CBC(String cipherText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true; //定义的全局变量
ctx.mode = SM4.SM4_DECRYPT;
byte[] keyBytes;
byte[] ivBytes;
if (hexString)
{
keyBytes = Util.hexStringToBytes(secretKey);
ivBytes = Util.hexStringToBytes(iv);
}
else
{
keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes();
}
SM4 sm4 = new SM4();
sm4.sm4_setkey_dec(ctx, keyBytes);
byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
return new String(decrypted, "GBK");
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws IOException
{
String plainText = "erer";
SM4Utils sm4 = new SM4Utils();
sm4.secretKey = "JeF8U9wHFOMfss28";
sm4.hexString = false;
System.out.println("ECB模式"); //ECB模式也即是分组加密技术。
String cipherText = sm4.encryptData_ECB(plainText);
System.out.println("密文: " + cipherText);
System.out.println("");
plainText = sm4.decryptData_ECB(cipherText);
System.out.println("明文: " + plainText);
System.out.println("");
System.out.println("CBC模式"); //把分组之后的生成的密文,作为下一分组的秘钥
sm4.iv = "UISwD9fW6cFh9SNS";
cipherText = sm4.encryptData_CBC(plainText);
System.out.println("密文: " + cipherText);
System.out.println("");
plainText = sm4.decryptData_CBC(cipherText);
System.out.println("明文: " + plainText);
}
}