cdk
2025-03-24 9694541db429678cbfcf8b769184856e6a6ab603
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System.Security.Cryptography;
using System.Text;
 
public class CryptoHelperService
{
    #region TripleDES
 
    ///// <summary>
    ///// TripleDES加密
    ///// </summary>
    ///// <param name="clearText">明文字符串</param>
    ///// <param name="key">16位key</param>
    ///// <param name="iv">8位iv</param>
    ///// <returns></returns>
    //public static string TripleDESEncrypt(string clearText, string key, string iv)
    //{
    //    SymmetricAlgorithm provider = SymmetricAlgorithm.Create("TripleDES");
    //    provider.Key = Encoding.UTF8.GetBytes(key);//16位
    //    provider.IV = Encoding.UTF8.GetBytes(iv);//8位
    //    ICryptoTransform encryptor = provider.CreateEncryptor();
    //    return Encrypt(clearText, encryptor);
    //}
 
    ///// <summary>
    ///// TripleDES解密
    ///// </summary>
    ///// <param name="encryptedText">加密的文本</param>
    ///// <param name="key">16位key</param>
    ///// <param name="iv">8位iv</param>
    ///// <returns></returns>
    //public static string TripleDESDecrypt(string encryptedText, string key, string iv)
    //{
    //    try
    //    {
    //        SymmetricAlgorithm provider = SymmetricAlgorithm.Create("TripleDES");
    //        provider.Key = Encoding.UTF8.GetBytes(key);//16位
    //        provider.IV = Encoding.UTF8.GetBytes(iv);//8位
    //        ICryptoTransform decryptor = provider.CreateDecryptor();
    //        return Decrypt(encryptedText, decryptor);
    //    }
    //    catch
    //    {
    //        return string.Empty;
    //    }
    //}
 
    #endregion
 
    #region DES
 
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="clearText">明文字符串</param>
    /// <param name="key">8位key</param>
    /// <param name="iv">8位iv</param>
    /// <returns></returns>
    //public static string DESEncrypt(string clearText, string key, string iv)
    //{
    //    SymmetricAlgorithm provider = SymmetricAlgorithm.Create("DES");
    //    provider.Key = Encoding.UTF8.GetBytes(key);//8位
    //    provider.IV = Encoding.UTF8.GetBytes(iv);//8位
    //    ICryptoTransform encryptor = provider.CreateEncryptor();
    //    return Encrypt(clearText, encryptor);
    //}
 
    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="encryptedText">加密的文本</param>
    /// <param name="key">8位key</param>
    /// <param name="iv">8位iv</param>
    /// <returns></returns>
    //public static string DESDecrypt(string encryptedText, string key, string iv)
    //{
    //    SymmetricAlgorithm provider = SymmetricAlgorithm.Create("DES");
    //    provider.Key = Encoding.UTF8.GetBytes(key);//8位
    //    provider.IV = Encoding.UTF8.GetBytes(iv);//8位
    //    ICryptoTransform decryptor = provider.CreateDecryptor();
    //    return Decrypt(encryptedText, decryptor);
    //}
 
    #endregion
 
 
    //// DES,TripleDES通用加密算法
    //private static string Encrypt(string clearText, ICryptoTransform encryptor)
    //{
    //    // 创建明文流
    //    byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
    //    MemoryStream clearStream = new MemoryStream(clearBuffer);
 
    //    // 创建空的密文流
    //    MemoryStream encryptedStream = new MemoryStream();
 
    //    CryptoStream cryptoStream =
    //        new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);
 
    //    // 将明文流写入到buffer中
    //    // 将buffer中的数据写入到cryptoStream中
    //    int BufferSize = 1024;
    //    int bytesRead = 0;
    //    byte[] buffer = new byte[BufferSize];
    //    do
    //    {
    //        bytesRead = clearStream.Read(buffer, 0, BufferSize);
    //        cryptoStream.Write(buffer, 0, bytesRead);
    //    } while (bytesRead > 0);
 
    //    cryptoStream.FlushFinalBlock();
 
    //    // 获取加密后的文本
    //    buffer = encryptedStream.ToArray();
    //    string encryptedText = Convert.ToBase64String(buffer);
    //    return encryptedText;
    //}
 
    //// DES,TripleDES通用解密算法
    //private static string Decrypt(string encryptedText, ICryptoTransform decryptor)
    //{
    //    try
    //    {
    //        byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
    //        Stream encryptedStream = new MemoryStream(encryptedBuffer);
 
    //        MemoryStream clearStream = new MemoryStream();
    //        CryptoStream cryptoStream =
    //            new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);
 
    //        int BufferSize = 1024;
    //        int bytesRead = 0;
    //        byte[] buffer = new byte[BufferSize];
 
    //        do
    //        {
    //            bytesRead = cryptoStream.Read(buffer, 0, BufferSize);
    //            clearStream.Write(buffer, 0, bytesRead);
    //        } while (bytesRead > 0);
 
    //        buffer = clearStream.GetBuffer();
    //        string clearText =
    //            Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);
 
    //        return clearText;
    //    }
    //    catch
    //    {
    //        return string.Empty;
    //    }
    //}
 
 
    #region Rijndael
 
    /// <summary>    
    /// 获得密钥    
    /// </summary>    
    /// <returns>密钥</returns>    
    private static byte[] GetLegalKey(RijndaelManaged Rijndael, string key)
    {
        string KeyTemp = key;
        Rijndael.GenerateKey();
        byte[] bytTemp = Rijndael.Key;
        int KeyLength = bytTemp.Length;
        if (KeyTemp.Length > KeyLength)
            KeyTemp = KeyTemp.Substring(0, KeyLength);
        else if (KeyTemp.Length < KeyLength)
            KeyTemp = KeyTemp.PadRight(KeyLength, ' ');
        return ASCIIEncoding.ASCII.GetBytes(KeyTemp);
    }
    /// <summary>    
    /// 获得初始向量IV    
    /// </summary>    
    /// <returns>初试向量IV</returns>    
    private static byte[] GetLegalIV(RijndaelManaged Rijndael, string iv)
    {
        string IVTemp = iv;
        Rijndael.GenerateIV();
        byte[] bytTemp = Rijndael.IV;
        int IVLength = bytTemp.Length;
        if (IVTemp.Length > IVLength)
            IVTemp = IVTemp.Substring(0, IVLength);
        else if (IVTemp.Length < IVLength)
            IVTemp = IVTemp.PadRight(IVLength, ' ');
        return ASCIIEncoding.ASCII.GetBytes(IVTemp);
    }
 
    /// <summary>
    /// Rijndael加密
    /// </summary>
    /// <param name="clearText">明文</param>
    /// <param name="key">key:8位</param>
    /// <param name="iv">iv:8位</param>
    /// <returns></returns>
    public static string RijndaelEncrypt(string clearText, string key, string iv)
    {
        RijndaelManaged Rijndael = new RijndaelManaged();
 
        byte[] bytIn = UTF8Encoding.UTF8.GetBytes(clearText);
        MemoryStream ms = new MemoryStream();
        Rijndael.Key = GetLegalKey(Rijndael, key);
        Rijndael.IV = GetLegalIV(Rijndael, iv);
        ICryptoTransform encrypto = Rijndael.CreateEncryptor();
        CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
        cs.Write(bytIn, 0, bytIn.Length);
        cs.FlushFinalBlock();
        ms.Close();
        byte[] bytOut = ms.ToArray();
        return Convert.ToBase64String(bytOut);
    }
 
    /// <summary>
    /// Rijndael解密
    /// </summary>
    /// <param name="encryptedText">加密的文本</param>
    /// <param name="key">key:8位</param>
    /// <param name="iv">iv:8位</param>
    /// <returns></returns>
    public static string RijndaelDecrypt(string encryptedText, string key, string iv)
    {
        try
        {
            RijndaelManaged Rijndael = new RijndaelManaged();
            byte[] bytIn = Convert.FromBase64String(encryptedText);
            MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            Rijndael.Key = GetLegalKey(Rijndael, key);
            Rijndael.IV = GetLegalIV(Rijndael, iv);
            ICryptoTransform encrypto = Rijndael.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs);
            return sr.ReadToEnd();
        }
        catch
        {
            return string.Empty;
        }
    }
 
    #endregion
 
    #region RSA
 
    /// <summary>
    /// 获取秘钥
    /// </summary>
    /// <param name="privateKey">私钥</param>
    /// <param name="publicKey">公钥</param>
    public static void RSAGetKey(out string privateKey, out string publicKey)
    {
        //加密解密用到的公钥与私钥 
        RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
        privateKey = oRSA.ToXmlString(true);//私钥 
        publicKey = oRSA.ToXmlString(false);//公钥 
    }
 
    /// <summary>
    /// RSA加密
    /// </summary>
    /// <param name="clearText">明文</param>
    /// <param name="publicKey">公钥</param>
    /// <returns></returns>
    public static string RSAEncrypt(string clearText, string publicKey)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(clearText); //需要加密的数据 
 
        //公钥加密 
        RSACryptoServiceProvider oRSA1 = new RSACryptoServiceProvider();
        oRSA1.FromXmlString(publicKey); //加密要用到公钥所以导入公钥 
        byte[] AOutput = oRSA1.Encrypt(bytes, true); //AOutput 加密以后的数据, true:OAEP填充方式
        return Convert.ToBase64String(AOutput);
    }
 
    /// <summary>
    /// RSA解密
    /// </summary>
    /// <param name="encryptedText">RSA加密文本</param>
    /// <param name="privateKey">私钥</param>
    /// <returns></returns>
    public static string RSADecrypt(string encryptedText, string privateKey)
    {
        try
        {
            RSACryptoServiceProvider oRSA2 = new RSACryptoServiceProvider();
            oRSA2.FromXmlString(privateKey);
            byte[] AOutput = Convert.FromBase64String(encryptedText);
            byte[] AInput = oRSA2.Decrypt(AOutput, true);//true:OAEP填充方式
            string reslut = Encoding.UTF8.GetString(AInput);
            return reslut;
        }
        catch
        {
            return string.Empty;
        }
    }
 
    /// <summary>
    /// RSA数字签名
    /// </summary>
    /// <param name="clearText">明文</param>
    /// <param name="privateKey">私钥</param>
    /// <returns></returns>
    public static string RSASign(string clearText, string privateKey)
    {
        byte[] bs = Encoding.UTF8.GetBytes(clearText);
 
        RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
 
        //私钥签名 
        oRSA.FromXmlString(privateKey);
        byte[] AOutput = oRSA.SignData(bs, "SHA1");
        return Convert.ToBase64String(AOutput);
    }
 
    /// <summary>
    /// RSA验证数字签名
    /// </summary>
    /// <param name="clearText">明文</param>
    /// <param name="signEncryptedText">数字签名</param>
    /// <param name="publicKey">公钥</param>
    /// <returns></returns>
    public static bool RSAVerify(string clearText, string signEncryptedText, string publicKey)
    {
        byte[] bs1 = Encoding.UTF8.GetBytes(clearText);
        byte[] bs2 = Convert.FromBase64String(signEncryptedText);
 
        RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
 
        //公钥验证             
        oRSA.FromXmlString(publicKey);
        bool bVerify = oRSA.VerifyData(bs1, "SHA1", bs2);
        return bVerify;
    }
 
 
    #endregion
 
    #region DSA
 
    /// <summary>
    /// 获取秘钥
    /// </summary>
    /// <param name="privateKey">私钥</param>
    /// <param name="publicKey">公钥</param>
    public static void DSAGetKey(out string privateKey, out string publicKey)
    {
        //加密解密用到的公钥与私钥 
        DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();
        privateKey = DSA.ToXmlString(true);//私钥 
        publicKey = DSA.ToXmlString(false);//公钥 
    }
 
    /// <summary>
    /// 获取数字签名
    /// </summary>
    /// <param name="clearText">明文</param>
    /// <param name="privateKey">私钥</param>
    /// <returns></returns>
    public static string DSASign(string clearText, string privateKey)
    {
        byte[] bs = Encoding.UTF8.GetBytes(clearText);
 
        DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();
 
        //私钥签名 
        DSA.FromXmlString(privateKey);
        byte[] AOutput = DSA.SignData(bs);
        return Convert.ToBase64String(AOutput);
    }
 
    /// <summary>
    /// 验证签名
    /// </summary>
    /// <param name="clearText">明文</param>
    /// <param name="signEncryptedText">明文的数字签名</param>
    /// <param name="publicKey">公钥</param>
    /// <returns></returns>
    public static bool DSAVerify(string clearText, string signEncryptedText, string publicKey)
    {
        byte[] bs1 = Encoding.UTF8.GetBytes(clearText);
        byte[] bs2 = Convert.FromBase64String(signEncryptedText);
 
        DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();
 
        //公钥验证             
        DSA.FromXmlString(publicKey);
        bool bVerify = DSA.VerifyData(bs1, bs2);
        return bVerify;
    }
 
 
    #endregion
}