using System;
using System.IO;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Collections;
namespace CS
{
/// <summary>
/// DES加密、解密
/// </summary>
public class DES
{
private static byte[] Key = new byte[] {0x82, 0xBC, 0xA1, 0x6A, 0xF5, 0x87, 0x3B, 0xE6, 0x59, 0x6A, 0x32, 0x64, 0x7F, 0x3A, 0x2A, 0xBB, 0x2B, 0x68, 0xE2, 0x5F, 0x06, 0xFB, 0xB8, 0x2D, 0x67, 0xB3, 0x55, 0x19, 0x4E, 0xB8, 0xBF, 0xDD };
private DES()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="source">待加密字串</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string source)
{
return Encrypt(source, Key);
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="source">待加密字串</param>
/// <param name="key">Key值</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string source,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode= CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] bytes = Encoding.Unicode.GetBytes(source);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="source">待解密的字串</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string strSource)
{
return Decrypt(source, Key);
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="source">待解密的字串</param>
/// <param name="key">Key值</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string source,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] bytes = Convert.FromBase64String(source);
MemoryStream ms = new MemoryStream(bytes);
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}
}
}