Core DES加解密
Posted ADOU-V
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Core DES加解密的主要内容,如果未能解决你的问题,请参考以下文章