using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Regent.TCommons
{
/// <summary>
/// '********************************************************
/// 模块名称: 安全类
/// 模块功能: 常用的对称加密算法代码。
/// </summary>
public class TSecurity
{
#if debug
public
#endif
static byte[] K_KEY = new byte[] { 111, 122, 112, 103, 109, 111, 110, 100 };
#if debug
public
#endif
static byte[] K_VI = new byte[] { 121, 108, 10, 110, 119, 97, 110, 103 };
/// <summary>
/// 编码字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string Encode(string data)
{
DES des = DES.Create();
des.Key = K_KEY;
des.IV = K_VI;
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream,
des.CreateEncryptor(),
CryptoStreamMode.Write);
byte[] toEncrypt = ASCIIEncoding.UTF8.GetBytes(data);
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
byte[] bs = mStream.ToArray();
cStream.Close();
mStream.Close();
return Convert.ToBase64String(bs);
}
/// <summary>
/// 解码字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string Decode(string data)
{
byte[] bytes = Convert.FromBase64String(data);
MemoryStream msDecrypt = new MemoryStream(bytes);
DES des = DES.Create();
des.Key = K_KEY;
des.IV = K_VI;
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
des.CreateDecryptor(),
CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[data.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return ASCIIEncoding.UTF8.GetString(fromEncrypt).Trim('\0');
}
}
}