Core DES加、解密

安装 Portable.BouncyCastle Nuget包
Install-Package Portable.BouncyCastle
 

 

 1 using Org.BouncyCastle.Crypto;
 2 using Org.BouncyCastle.Crypto.Engines;
 3 using Org.BouncyCastle.Crypto.Modes;
 4 using Org.BouncyCastle.Crypto.Paddings;
 5 using Org.BouncyCastle.Crypto.Parameters;
 6 using System;
 7 using System.Collections.Generic;
 8 using System.IO;
 9 using System.Security.Cryptography;
10 using System.Text;
11 
12 namespace ConsoleAppDES
13 {
14     class Program
15     {
16         public static string desData = "fu kai hang";
17         public static string desKey = "12345678";
18         public static string desIV = "12345678";
19         static void Main(string[] args)
20         {
21             var encrypt = DESHelper.EncryptDES(desData, desKey, desIV);
22             Console.WriteLine(encrypt);
23             var decrypt = DESHelper.DecryptDES(encrypt, desKey, desIV);
24             Console.WriteLine(decrypt);
25             Console.ReadKey();
26         }
27     }
28     public class DESHelper
29     {
30         static IBlockCipher engine = new DesEngine();
31         /// <summary>
32         /// 使用DES加密
33         /// </summary>
34         /// <param name="data">待加密的字符串</param>
35         /// <param name="key">加密密钥,要求8位</param>
36         /// <param name="iv">偏移向量</param>
37         /// <returns></returns>
38         public static string EncryptDES(string data, string key, string iv)
39         {
40             byte[] byKey = Encoding.UTF8.GetBytes(key);
41             byte[] byIV = Encoding.UTF8.GetBytes(iv);
42             byte[] byData = Encoding.UTF8.GetBytes(data);
43 
44             BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding());
45             cipher.Init(true, new ParametersWithIV(new DesParameters(byKey), byIV));
46             byte[] rv = new byte[cipher.GetOutputSize(byData.Length)];
47             int tam = cipher.ProcessBytes(byData, 0, byData.Length, rv, 0);
48             cipher.DoFinal(rv, tam);
49             return Convert.ToBase64String(rv);
50         }
51         /// <summary>
52         /// 使用DES解密
53         /// </summary>
54         /// <param name="data">待解密的字符串</param>
55         /// <param name="key">解密密钥,要求8位</param>
56         /// <param name="vi">偏移向量</param>
57         /// <returns></returns>
58         public static string DecryptDES(string data, string key, string vi)
59         {
60             StringBuilder ret = new StringBuilder();
61             foreach (byte b in Convert.FromBase64String(data))
62             {
63                 ret.AppendFormat("{0:X2}", b);
64             }
65             byte[] byData = new byte[ret.ToString().Length / 2];
66             for (int x = 0; x < ret.ToString().Length / 2; x++)
67             {
68                 int i = (Convert.ToInt32(ret.ToString().Substring(x * 2, 2), 16));
69                 byData[x] = (byte)i;
70             }
71             byte[] byKey = Encoding.UTF8.GetBytes(key);
72             byte[] byVI = Encoding.UTF8.GetBytes(vi);
73             BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
74             cipher.Init(false, new ParametersWithIV(new DesParameters(byKey), byVI));
75             byte[] rv = new byte[cipher.GetOutputSize(byData.Length)];
76             int tam = cipher.ProcessBytes(byData, 0, byData.Length, rv, 0);
77             cipher.DoFinal(rv, tam);
78             var rvl = new List<byte>();
79             rvl.AddRange(rv);
80             rvl.RemoveAll(b => b == 0);
81             rv = rvl.ToArray();
82             return Encoding.UTF8.GetString(rv);
83         }
84     }
85 }

 

转载于:https://www.cnblogs.com/a-dou/p/6810675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值