|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+using Org.BouncyCastle.Crypto;
|
|
|
2
|
+using Org.BouncyCastle.Crypto.Digests;
|
|
|
3
|
+using Org.BouncyCastle.Crypto.Generators;
|
|
|
4
|
+using Org.BouncyCastle.Crypto.Parameters;
|
|
|
5
|
+using Org.BouncyCastle.Math;
|
|
|
6
|
+using Org.BouncyCastle.Math.EC;
|
|
|
7
|
+using Org.BouncyCastle.Security;
|
|
|
8
|
+using Org.BouncyCastle.Utilities.Encoders;
|
|
|
9
|
+using System;
|
|
|
10
|
+using System.Collections.Generic;
|
|
|
11
|
+using System.IO;
|
|
|
12
|
+using System.Linq;
|
|
|
13
|
+using System.Text;
|
|
|
14
|
+
|
|
|
15
|
+namespace CallCenter.Utility
|
|
|
16
|
+{
|
|
|
17
|
+ /// <summary>
|
|
|
18
|
+ /// SM2工具类
|
|
|
19
|
+ /// </summary>
|
|
|
20
|
+ public class SM2CryptoUtil
|
|
|
21
|
+ {
|
|
|
22
|
+
|
|
|
23
|
+ #region 获取公钥私钥
|
|
|
24
|
+ /// <summary>
|
|
|
25
|
+ /// 获取公钥私钥
|
|
|
26
|
+ /// </summary>
|
|
|
27
|
+ /// <returns></returns>
|
|
|
28
|
+ public static SM2Model GetKey()
|
|
|
29
|
+ {
|
|
|
30
|
+ SM2 sm2 = SM2.Instance;
|
|
|
31
|
+ AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.GenerateKeyPair();
|
|
|
32
|
+ ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;
|
|
|
33
|
+ ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;
|
|
|
34
|
+ BigInteger privateKey = ecpriv.D;
|
|
|
35
|
+ ECPoint publicKey = ecpub.Q;
|
|
|
36
|
+ SM2Model sM2Model = new SM2Model();
|
|
|
37
|
+ sM2Model.PrivateKey = Encoding.UTF8.GetString(Hex.Encode(privateKey.ToByteArray())).ToUpper();
|
|
|
38
|
+ sM2Model.PublicKey = Encoding.UTF8.GetString(Hex.Encode(publicKey.GetEncoded())).ToUpper();
|
|
|
39
|
+ return sM2Model;
|
|
|
40
|
+ }
|
|
|
41
|
+ #endregion
|
|
|
42
|
+
|
|
|
43
|
+ #region 加密
|
|
|
44
|
+ /// <summary>
|
|
|
45
|
+ /// 加密
|
|
|
46
|
+ /// </summary>
|
|
|
47
|
+ /// <param name="publickey">公钥</param>
|
|
|
48
|
+ /// <param name="sourceData">需要加密的值</param>
|
|
|
49
|
+ /// <returns>加密结果</returns>
|
|
|
50
|
+ public static string Encrypt(string publickey, string sourceData)
|
|
|
51
|
+ {
|
|
|
52
|
+ var data = Encrypt(Hex.Decode(publickey), Encoding.UTF8.GetBytes(sourceData));
|
|
|
53
|
+ return data;
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ /// <summary>
|
|
|
57
|
+ /// 加密
|
|
|
58
|
+ /// </summary>
|
|
|
59
|
+ /// <param name="publicKey">公钥</param>
|
|
|
60
|
+ /// <param name="data">需要加密的值</param>
|
|
|
61
|
+ /// <returns></returns>
|
|
|
62
|
+ public static String Encrypt(byte[] publicKey, byte[] data)
|
|
|
63
|
+ {
|
|
|
64
|
+ if (null == publicKey || publicKey.Length == 0)
|
|
|
65
|
+ {
|
|
|
66
|
+ return null;
|
|
|
67
|
+ }
|
|
|
68
|
+ if (data == null || data.Length == 0)
|
|
|
69
|
+ {
|
|
|
70
|
+ return null;
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ byte[] source = new byte[data.Length];
|
|
|
74
|
+ Array.Copy(data, 0, source, 0, data.Length);
|
|
|
75
|
+
|
|
|
76
|
+ Cipher cipher = new Cipher();
|
|
|
77
|
+ SM2 sm2 = SM2.Instance;
|
|
|
78
|
+
|
|
|
79
|
+ ECPoint userKey = sm2.ecc_curve.DecodePoint(publicKey);
|
|
|
80
|
+
|
|
|
81
|
+ ECPoint c1 = cipher.Init_enc(sm2, userKey);
|
|
|
82
|
+ cipher.Encrypt(source);
|
|
|
83
|
+
|
|
|
84
|
+ byte[] c3 = new byte[32];
|
|
|
85
|
+ cipher.Dofinal(c3);
|
|
|
86
|
+
|
|
|
87
|
+ String sc1 = Encoding.UTF8.GetString(Hex.Encode(c1.GetEncoded()));
|
|
|
88
|
+ String sc2 = Encoding.UTF8.GetString(Hex.Encode(source));
|
|
|
89
|
+ String sc3 = Encoding.UTF8.GetString(Hex.Encode(c3));
|
|
|
90
|
+
|
|
|
91
|
+ return (sc1 + sc2 + sc3).ToUpper();
|
|
|
92
|
+ }
|
|
|
93
|
+ #endregion
|
|
|
94
|
+
|
|
|
95
|
+ #region 解密
|
|
|
96
|
+
|
|
|
97
|
+ /// <summary>
|
|
|
98
|
+ ///
|
|
|
99
|
+ /// </summary>
|
|
|
100
|
+ /// <param name="privateKey"></param>
|
|
|
101
|
+ /// <param name="encryptedData"></param>
|
|
|
102
|
+ /// <returns></returns>
|
|
|
103
|
+ public static string Decrypt(string privateKey, string encryptedData)
|
|
|
104
|
+ {
|
|
|
105
|
+ var data = Encoding.UTF8.GetString(Decrypt(Hex.Decode(privateKey), Hex.Decode(encryptedData)));
|
|
|
106
|
+ return data;
|
|
|
107
|
+ }
|
|
|
108
|
+ /// <summary>
|
|
|
109
|
+ /// 解密
|
|
|
110
|
+ /// </summary>
|
|
|
111
|
+ /// <param name="privateKey"></param>
|
|
|
112
|
+ /// <param name="encryptedData"></param>
|
|
|
113
|
+ /// <returns></returns>
|
|
|
114
|
+ public static byte[] Decrypt(byte[] privateKey, byte[] encryptedData)
|
|
|
115
|
+ {
|
|
|
116
|
+ if (null == privateKey || privateKey.Length == 0)
|
|
|
117
|
+ {
|
|
|
118
|
+ return null;
|
|
|
119
|
+ }
|
|
|
120
|
+ if (encryptedData == null || encryptedData.Length == 0)
|
|
|
121
|
+ {
|
|
|
122
|
+ return null;
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
|
125
|
+ String data = Encoding.UTF8.GetString(Hex.Encode(encryptedData));
|
|
|
126
|
+
|
|
|
127
|
+ byte[] c1Bytes = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(0, 130)));
|
|
|
128
|
+ int c2Len = encryptedData.Length - 97;
|
|
|
129
|
+ byte[] c2 = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(130, 2 * c2Len)));
|
|
|
130
|
+ byte[] c3 = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(130 + 2 * c2Len, 64)));
|
|
|
131
|
+
|
|
|
132
|
+ SM2 sm2 = SM2.Instance;
|
|
|
133
|
+ BigInteger userD = new BigInteger(1, privateKey);
|
|
|
134
|
+
|
|
|
135
|
+ //ECPoint c1 = sm2.ecc_curve.DecodePoint(c1Bytes);
|
|
|
136
|
+
|
|
|
137
|
+ ECPoint c1 = sm2.ecc_curve.DecodePoint(c1Bytes);
|
|
|
138
|
+ Cipher cipher = new Cipher();
|
|
|
139
|
+ cipher.Init_dec(userD, c1);
|
|
|
140
|
+ cipher.Decrypt(c2);
|
|
|
141
|
+ cipher.Dofinal(c3);
|
|
|
142
|
+
|
|
|
143
|
+ return c2;
|
|
|
144
|
+ }
|
|
|
145
|
+ #endregion
|
|
|
146
|
+
|
|
|
147
|
+ private class Cipher
|
|
|
148
|
+ {
|
|
|
149
|
+ private int ct;
|
|
|
150
|
+ private ECPoint p2;
|
|
|
151
|
+ private SM3Digest sm3keybase;
|
|
|
152
|
+ private SM3Digest sm3c3;
|
|
|
153
|
+ private byte[] key;
|
|
|
154
|
+ private byte keyOff;
|
|
|
155
|
+
|
|
|
156
|
+ public Cipher()
|
|
|
157
|
+ {
|
|
|
158
|
+ this.ct = 1;
|
|
|
159
|
+ this.key = new byte[32];
|
|
|
160
|
+ this.keyOff = 0;
|
|
|
161
|
+ }
|
|
|
162
|
+
|
|
|
163
|
+ public static byte[] byteConvert32Bytes(BigInteger n)
|
|
|
164
|
+ {
|
|
|
165
|
+ byte[] tmpd = null;
|
|
|
166
|
+ if (n == null)
|
|
|
167
|
+ {
|
|
|
168
|
+ return null;
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ if (n.ToByteArray().Length == 33)
|
|
|
172
|
+ {
|
|
|
173
|
+ tmpd = new byte[32];
|
|
|
174
|
+ Array.Copy(n.ToByteArray(), 1, tmpd, 0, 32);
|
|
|
175
|
+ }
|
|
|
176
|
+ else if (n.ToByteArray().Length == 32)
|
|
|
177
|
+ {
|
|
|
178
|
+ tmpd = n.ToByteArray();
|
|
|
179
|
+ }
|
|
|
180
|
+ else
|
|
|
181
|
+ {
|
|
|
182
|
+ tmpd = new byte[32];
|
|
|
183
|
+ for (int i = 0; i < 32 - n.ToByteArray().Length; i++)
|
|
|
184
|
+ {
|
|
|
185
|
+ tmpd[i] = 0;
|
|
|
186
|
+ }
|
|
|
187
|
+ Array.Copy(n.ToByteArray(), 0, tmpd, 32 - n.ToByteArray().Length, n.ToByteArray().Length);
|
|
|
188
|
+ }
|
|
|
189
|
+ return tmpd;
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ private void Reset()
|
|
|
193
|
+ {
|
|
|
194
|
+ this.sm3keybase = new SM3Digest();
|
|
|
195
|
+ this.sm3c3 = new SM3Digest();
|
|
|
196
|
+
|
|
|
197
|
+ byte[] p = byteConvert32Bytes(p2.Normalize().XCoord.ToBigInteger());
|
|
|
198
|
+ this.sm3keybase.BlockUpdate(p, 0, p.Length);
|
|
|
199
|
+ this.sm3c3.BlockUpdate(p, 0, p.Length);
|
|
|
200
|
+
|
|
|
201
|
+ p = byteConvert32Bytes(p2.Normalize().YCoord.ToBigInteger());
|
|
|
202
|
+ this.sm3keybase.BlockUpdate(p, 0, p.Length);
|
|
|
203
|
+ this.ct = 1;
|
|
|
204
|
+ NextKey();
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+ private void NextKey()
|
|
|
208
|
+ {
|
|
|
209
|
+ SM3Digest sm3keycur = new SM3Digest(this.sm3keybase);
|
|
|
210
|
+ sm3keycur.Update((byte)(ct >> 24 & 0xff));
|
|
|
211
|
+ sm3keycur.Update((byte)(ct >> 16 & 0xff));
|
|
|
212
|
+ sm3keycur.Update((byte)(ct >> 8 & 0xff));
|
|
|
213
|
+ sm3keycur.Update((byte)(ct & 0xff));
|
|
|
214
|
+ sm3keycur.DoFinal(key, 0);
|
|
|
215
|
+ this.keyOff = 0;
|
|
|
216
|
+ this.ct++;
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
|
219
|
+ public ECPoint Init_enc(SM2 sm2, ECPoint userKey)
|
|
|
220
|
+ {
|
|
|
221
|
+ AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.GenerateKeyPair();
|
|
|
222
|
+ ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;
|
|
|
223
|
+ ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;
|
|
|
224
|
+ BigInteger k = ecpriv.D;
|
|
|
225
|
+ ECPoint c1 = ecpub.Q;
|
|
|
226
|
+ this.p2 = userKey.Multiply(k);
|
|
|
227
|
+ Reset();
|
|
|
228
|
+ return c1;
|
|
|
229
|
+ }
|
|
|
230
|
+
|
|
|
231
|
+ public void Encrypt(byte[] data)
|
|
|
232
|
+ {
|
|
|
233
|
+ this.sm3c3.BlockUpdate(data, 0, data.Length);
|
|
|
234
|
+ for (int i = 0; i < data.Length; i++)
|
|
|
235
|
+ {
|
|
|
236
|
+ if (keyOff == key.Length)
|
|
|
237
|
+ {
|
|
|
238
|
+ NextKey();
|
|
|
239
|
+ }
|
|
|
240
|
+ data[i] ^= key[keyOff++];
|
|
|
241
|
+ }
|
|
|
242
|
+ }
|
|
|
243
|
+
|
|
|
244
|
+ public void Init_dec(BigInteger userD, ECPoint c1)
|
|
|
245
|
+ {
|
|
|
246
|
+ this.p2 = c1.Multiply(userD);
|
|
|
247
|
+ Reset();
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ public void Decrypt(byte[] data)
|
|
|
251
|
+ {
|
|
|
252
|
+ for (int i = 0; i < data.Length; i++)
|
|
|
253
|
+ {
|
|
|
254
|
+ if (keyOff == key.Length)
|
|
|
255
|
+ {
|
|
|
256
|
+ NextKey();
|
|
|
257
|
+ }
|
|
|
258
|
+ data[i] ^= key[keyOff++];
|
|
|
259
|
+ }
|
|
|
260
|
+
|
|
|
261
|
+ this.sm3c3.BlockUpdate(data, 0, data.Length);
|
|
|
262
|
+ }
|
|
|
263
|
+
|
|
|
264
|
+ public void Dofinal(byte[] c3)
|
|
|
265
|
+ {
|
|
|
266
|
+ byte[] p = byteConvert32Bytes(p2.Normalize().YCoord.ToBigInteger());
|
|
|
267
|
+ this.sm3c3.BlockUpdate(p, 0, p.Length);
|
|
|
268
|
+ this.sm3c3.DoFinal(c3, 0);
|
|
|
269
|
+ Reset();
|
|
|
270
|
+ }
|
|
|
271
|
+ }
|
|
|
272
|
+ private class SM2
|
|
|
273
|
+ {
|
|
|
274
|
+ public static SM2 Instance
|
|
|
275
|
+ {
|
|
|
276
|
+ get
|
|
|
277
|
+ {
|
|
|
278
|
+ return new SM2();
|
|
|
279
|
+ }
|
|
|
280
|
+
|
|
|
281
|
+ }
|
|
|
282
|
+ public static SM2 InstanceTest
|
|
|
283
|
+ {
|
|
|
284
|
+ get
|
|
|
285
|
+ {
|
|
|
286
|
+ return new SM2();
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ }
|
|
|
290
|
+
|
|
|
291
|
+ public static readonly string[] sm2_param = {
|
|
|
292
|
+ "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",// p,0
|
|
|
293
|
+ "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",// a,1
|
|
|
294
|
+ "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",// b,2
|
|
|
295
|
+ "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",// n,3
|
|
|
296
|
+ "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",// gx,4
|
|
|
297
|
+ "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0" // gy,5
|
|
|
298
|
+ };
|
|
|
299
|
+
|
|
|
300
|
+ public string[] ecc_param = sm2_param;
|
|
|
301
|
+
|
|
|
302
|
+ public readonly BigInteger ecc_p;
|
|
|
303
|
+ public readonly BigInteger ecc_a;
|
|
|
304
|
+ public readonly BigInteger ecc_b;
|
|
|
305
|
+ public readonly BigInteger ecc_n;
|
|
|
306
|
+ public readonly BigInteger ecc_gx;
|
|
|
307
|
+ public readonly BigInteger ecc_gy;
|
|
|
308
|
+
|
|
|
309
|
+ public readonly ECCurve ecc_curve;
|
|
|
310
|
+ public readonly ECPoint ecc_point_g;
|
|
|
311
|
+
|
|
|
312
|
+ public readonly ECDomainParameters ecc_bc_spec;
|
|
|
313
|
+
|
|
|
314
|
+ public readonly ECKeyPairGenerator ecc_key_pair_generator;
|
|
|
315
|
+
|
|
|
316
|
+ private SM2()
|
|
|
317
|
+ {
|
|
|
318
|
+ ecc_param = sm2_param;
|
|
|
319
|
+
|
|
|
320
|
+ ECFieldElement ecc_gx_fieldelement;
|
|
|
321
|
+ ECFieldElement ecc_gy_fieldelement;
|
|
|
322
|
+
|
|
|
323
|
+ ecc_p = new BigInteger(ecc_param[0], 16);
|
|
|
324
|
+ ecc_a = new BigInteger(ecc_param[1], 16);
|
|
|
325
|
+ ecc_b = new BigInteger(ecc_param[2], 16);
|
|
|
326
|
+ ecc_n = new BigInteger(ecc_param[3], 16);
|
|
|
327
|
+ ecc_gx = new BigInteger(ecc_param[4], 16);
|
|
|
328
|
+ ecc_gy = new BigInteger(ecc_param[5], 16);
|
|
|
329
|
+
|
|
|
330
|
+
|
|
|
331
|
+ ecc_gx_fieldelement = new FpFieldElement(ecc_p, ecc_gx);
|
|
|
332
|
+ ecc_gy_fieldelement = new FpFieldElement(ecc_p, ecc_gy);
|
|
|
333
|
+
|
|
|
334
|
+ ecc_curve = new FpCurve(ecc_p, ecc_a, ecc_b);
|
|
|
335
|
+ ecc_point_g = new FpPoint(ecc_curve, ecc_gx_fieldelement, ecc_gy_fieldelement);
|
|
|
336
|
+
|
|
|
337
|
+ ecc_bc_spec = new ECDomainParameters(ecc_curve, ecc_point_g, ecc_n);
|
|
|
338
|
+
|
|
|
339
|
+ ECKeyGenerationParameters ecc_ecgenparam;
|
|
|
340
|
+ ecc_ecgenparam = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());
|
|
|
341
|
+
|
|
|
342
|
+ ecc_key_pair_generator = new ECKeyPairGenerator();
|
|
|
343
|
+ ecc_key_pair_generator.Init(ecc_ecgenparam);
|
|
|
344
|
+ }
|
|
|
345
|
+
|
|
|
346
|
+ public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey)
|
|
|
347
|
+ {
|
|
|
348
|
+ SM3Digest sm3 = new SM3Digest();
|
|
|
349
|
+ byte[] p;
|
|
|
350
|
+ // userId length
|
|
|
351
|
+ int len = userId.Length * 8;
|
|
|
352
|
+ sm3.Update((byte)(len >> 8 & 0x00ff));
|
|
|
353
|
+ sm3.Update((byte)(len & 0x00ff));
|
|
|
354
|
+
|
|
|
355
|
+ // userId
|
|
|
356
|
+ sm3.BlockUpdate(userId, 0, userId.Length);
|
|
|
357
|
+
|
|
|
358
|
+ // a,b
|
|
|
359
|
+ p = ecc_a.ToByteArray();
|
|
|
360
|
+ sm3.BlockUpdate(p, 0, p.Length);
|
|
|
361
|
+ p = ecc_b.ToByteArray();
|
|
|
362
|
+ sm3.BlockUpdate(p, 0, p.Length);
|
|
|
363
|
+ // gx,gy
|
|
|
364
|
+ p = ecc_gx.ToByteArray();
|
|
|
365
|
+ sm3.BlockUpdate(p, 0, p.Length);
|
|
|
366
|
+ p = ecc_gy.ToByteArray();
|
|
|
367
|
+ sm3.BlockUpdate(p, 0, p.Length);
|
|
|
368
|
+
|
|
|
369
|
+ // x,y
|
|
|
370
|
+ p = userKey.AffineXCoord.ToBigInteger().ToByteArray();
|
|
|
371
|
+ sm3.BlockUpdate(p, 0, p.Length);
|
|
|
372
|
+ p = userKey.AffineYCoord.ToBigInteger().ToByteArray();
|
|
|
373
|
+ sm3.BlockUpdate(p, 0, p.Length);
|
|
|
374
|
+
|
|
|
375
|
+ // Z
|
|
|
376
|
+ byte[] md = new byte[sm3.GetDigestSize()];
|
|
|
377
|
+ sm3.DoFinal(md, 0);
|
|
|
378
|
+
|
|
|
379
|
+ return md;
|
|
|
380
|
+ }
|
|
|
381
|
+ }
|
|
|
382
|
+ public class SM2Model
|
|
|
383
|
+ {
|
|
|
384
|
+ /// <summary>
|
|
|
385
|
+ /// 公钥
|
|
|
386
|
+ /// </summary>
|
|
|
387
|
+ public string PublicKey { get; set; }
|
|
|
388
|
+ /// <summary>
|
|
|
389
|
+ /// 私钥
|
|
|
390
|
+ /// </summary>
|
|
|
391
|
+ public string PrivateKey { get; set; }
|
|
|
392
|
+ }
|
|
|
393
|
+ /// <summary>
|
|
|
394
|
+ /// 公钥
|
|
|
395
|
+ /// </summary>
|
|
|
396
|
+ public static string PublicKey = "042DBA45E7B03394F603CADAFCDDEC854D3E01A4E9C52CD799B85B1A14BDB970137AE58BA" +
|
|
|
397
|
+ "553D79F058604DC1CD4B77DE5408BA3308E767584100C2B663510C819";
|
|
|
398
|
+
|
|
|
399
|
+ /// <summary>
|
|
|
400
|
+ /// 私钥
|
|
|
401
|
+ /// </summary>
|
|
|
402
|
+ public static string PrivateKey = "BF1F907B4E0487F798DC80AFD7BC2A6201E8514233002272EA3BE2FC6F797843";
|
|
|
403
|
+
|
|
|
404
|
+ /// <summary>
|
|
|
405
|
+ /// 公钥加密明文
|
|
|
406
|
+ /// </summary>
|
|
|
407
|
+ /// <param name="plainText">明文</param>
|
|
|
408
|
+ /// <returns>密文</returns>
|
|
|
409
|
+ public static string Encrypt(string plainText)
|
|
|
410
|
+ {
|
|
|
411
|
+ return Encrypt(PublicKey, plainText);
|
|
|
412
|
+ }
|
|
|
413
|
+
|
|
|
414
|
+ /// <summary>
|
|
|
415
|
+ /// SM2私钥解密密文
|
|
|
416
|
+ /// </summary>
|
|
|
417
|
+ /// <param name="cipherText">密文</param>
|
|
|
418
|
+ /// <returns>明文</returns>
|
|
|
419
|
+ public static string Decrypt(string cipherText)
|
|
|
420
|
+ {
|
|
|
421
|
+ if (!cipherText.StartsWith("04")) cipherText = "04" + cipherText;//
|
|
|
422
|
+ return Decrypt(PrivateKey, cipherText);
|
|
|
423
|
+
|
|
|
424
|
+ }
|
|
|
425
|
+ }
|
|
|
426
|
+}
|