MD5 加密 解密

加密:

View Code
 1 using System;
 2  using System.Collections.Generic;
 3  using System.Linq;
 4  using System.Text;
 5  using System.Security.Cryptography;
 6  using System.IO;
 7  
 8 namespace ENPOT.Manufacture.Security.DL.DataLogic
 9  {
10      public class CryptUtil
11      {
12          static string encryptionKey = "12345678";
13          static byte[] rgbKey = new byte[0];
14          static byte[] rgbIV = new byte[] { 10, 20, 30, 40, 50, 60, 70, 80 };
15  
16       //编码
17  
18         public static string Encrypt(string textToEncrypt)
19          {
20              rgbKey = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
21              DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
22              byte[] bytes = Encoding.UTF8.GetBytes(textToEncrypt);
23              MemoryStream stream = new MemoryStream();
24              CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
25              stream2.Write(bytes, 0, bytes.Length);
26              stream2.FlushFinalBlock();
27              return Convert.ToBase64String(stream.ToArray());
28          }

解码:

View Code
 1 public static string Decrypt(string textToDecrypt)
 2          {
 3              byte[] buffer = new byte[textToDecrypt.Length];
 4              rgbKey = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
 5              DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
 6              buffer = Convert.FromBase64String(textToDecrypt);
 7              MemoryStream stream = new MemoryStream();
 8              CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
 9              stream2.Write(buffer, 0, buffer.Length);
10              stream2.FlushFinalBlock();
11              return Encoding.UTF8.GetString(stream.ToArray());
12          }
13      }
14  
15 }

 

posted @ 2013-03-15 13:42  银河系上的地球  阅读(306)  评论(0编辑  收藏  举报