using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ENPOT.Manufacture.Security.DL.DataLogic
{
public class CryptUtil
{
static string encryptionKey = "12345678";
static byte[] rgbKey = new byte[0];
static byte[] rgbIV = new byte[] { 10, 20, 30, 40, 50, 60, 70, 80 };
//编码
public static string Encrypt(string textToEncrypt)
{
rgbKey = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.UTF8.GetBytes(textToEncrypt);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
//解码
public static string Decrypt(string textToDecrypt)
{
byte[] buffer = new byte[textToDecrypt.Length];
rgbKey = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
buffer = Convert.FromBase64String(textToDecrypt);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}