using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace WebService
{
public class Encryption64
{
//private byte[] key = {};
//private byte[] IV = {10, 20, 30, 40, 50, 60, 70, 80}; // it can be any byte value
public static string Decrypt(string stringToDecrypt,
string sEncryptionKey)
{
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
return string.Empty;
}
}
public static string Encrypt(string stringToEncrypt,
string sEncryptionKey)
{
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray; //Convert.ToByte(stringToEncrypt.Length)
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception ex)
{
return string.Empty;
}
}
}
///
/// Encrypt 的摘要说明。
/// LiTianPing
///
public class DEncrypt
{
///
/// 构造方法
///
public DEncrypt()
{
}
#region 使用 缺省密钥字符串 加密/解密string
///
/// 使用缺省密钥字符串加密string
///
/// 明文
/// 密文
public static string Encrypt(string original)
{
return Encrypt(original, "%FSxh#s&A!Csd");
}
///
/// 使用缺省密钥字符串解密string
///
/// 密文
/// 明文
public static string Decrypt(string original)
{
return Decrypt(original, "%FSxh#s&A!Csd", System.Text.Encoding.Default);
}
#endregion
#region 使用 给定密钥字符串 加密/解密string
///
/// 使用给定密钥字符串加密string
///
/// 原始文字
/// 密钥
/// 字符编码方案
/// 密文
public static string Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff, kb));
}
///
/// 使用给定密钥字符串解密string
///
/// 密文
/// 密钥
/// 明文
public static string Decrypt(string original, string key)
{
return Decrypt(original, key, System.Text.Encoding.Default);
}
///
/// 使用给定密钥字符串解密string,返回指定编码方式明文
///
/// 密文
/// 密钥
/// 字符编码方案
/// 明文
public static string Decrypt(string encrypted, string key, Encoding encoding)
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff, kb));
}
#endregion
#region 使用 缺省密钥字符串 加密/解密/byte[]
///
/// 使用缺省密钥字符串解密byte[]
///
/// 密文
/// 密钥
/// 明文
public static byte[] Decrypt(byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes("%FSxh#s&A!Csd");
return Decrypt(encrypted, key);
}
///
/// 使用缺省密钥字符串加密
///
/// 原始数据
/// 密钥
/// 密文
public static byte[] Encrypt(byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes("%FSxh#s&A!Csd");
return Encrypt(original, key);
}
#endregion
#region 使用 给定密钥 加密/解密/byte[]
///
/// 生成MD5摘要
///
/// 数据源
/// 摘要
public static byte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
///
/// 使用给定密钥加密
///
/// 明文
/// 密钥
/// 密文
public static byte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
///
/// 使用给定密钥解密数据
///
/// 密文
/// 密钥
/// 明文
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
#endregion
}
}