using System; using System.Security.Cryptography; using System.IO; using System.Text; public class Security { public Security() { // // TODO: 在此处添加构造函数逻辑 // } public string Encrypt(string pToEncrypt,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 MemoryStream ms = new MemoryStream(); //使得输入密码必须输入英文文本 CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach(byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } /// /// DEC 解密过程 /// /// /// /// public string Decrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for(int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; }
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量,此值重要,不能修改 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
return System.Text.Encoding.Default.GetString(ms.ToArray()); } /// /// 检查己加密的字符串是否与原文相同 /// /// /// /// /// public bool ValidateString(string EnString, string FoString, int Mode) { switch (Mode) { default: case 1: if (Decrypt(EnString,_QueryStringKey) == FoString.ToString()) { return true; } else { return false; } case 2: if (Decrypt(EnString,_PassWordKey) == FoString.ToString()) { return true; } else { return false; } } } }
对称加密算法://加密 public string DESEncrypt(string encryptstring) { string strRtn; try { DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密 byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey); byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring); MemoryStream ms = new MemoryStream();//存储加密后的数据 CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write); cs.Write(data, 0, data.Length);//进行加密 cs.FlushFinalBlock(); strRtn = Convert.ToBase64String(ms.ToArray()); return strRtn; } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return null; } }=================================================================================================//解密 public string DESDecrypt(string decryptstring) { string strRtn; try { DESCryptoServiceProvider desc = new DESCryptoServiceProvider(); byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey); byte[] data = Convert.FromBase64String(decryptstring); MemoryStream ms = new MemoryStream();//存储解密后的数据 CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write); cs.Write(data, 0, data.Length);//解密数据 cs.FlushFinalBlock(); strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray()); return strRtn; } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return null; } }
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;namespace RedMouse.Helper
{
public class DEncryptHelper
{
#region 使用 缺省密钥字符串 加密/解密string /// <summary>
/// 使用缺省密钥字符串加密string
/// </summary>
/// <param name="original">明文</param>
/// <returns>密文</returns>
public static string Encrypt(string original)
{
return Encrypt(original,"onlyeasy");
} /// <summary>
/// 使用缺省密钥字符串解密string
/// </summary>
/// <param name="original">密文</param>
/// <returns>明文</returns>
public static string Decrypt(string original)
{
return Decrypt(original, "onlyeasy", System.Text.Encoding.Default);
} #endregion #region 使用 给定密钥字符串 加密/解密string
/// <summary>
/// 使用给定密钥字符串加密string
/// </summary>
/// <param name="original">原始文字</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>密文</returns>
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));
}
/// <summary>
/// 使用给定密钥字符串解密string
/// </summary>
/// <param name="original">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string Decrypt(string original, string key)
{
return Decrypt(original,key,System.Text.Encoding.Default);
} /// <summary>
/// 使用给定密钥字符串解密string,返回指定编码方式明文
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>明文</returns>
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[]
/// <summary>
/// 使用缺省密钥字符串解密byte[]
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] Decrypt(byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes("onlyeasy");
return Decrypt(encrypted,key);
}
/// <summary>
/// 使用缺省密钥字符串加密
/// </summary>
/// <param name="original">原始数据</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] Encrypt(byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes("onlyeasy");
return Encrypt(original,key);
}
#endregion #region 使用 给定密钥 加密/解密/byte[] /// <summary>
/// 生成MD5摘要
/// </summary>
/// <param name="original">数据源</param>
/// <returns>摘要</returns>
public static byte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
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);
} /// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
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);
}
public static string Encrypt(string password, int passwordFormat)
{
string passwordEncrypt = "";
switch (passwordFormat)
{
case 0:
passwordEncrypt = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
break;
case 1:
passwordEncrypt = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
break;
}
return passwordEncrypt;
}
#endregion
}
}
using System.Security.Cryptography;
using System.IO;
using System.Text; public class Security
{
public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string Encrypt(string pToEncrypt,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
MemoryStream ms = new MemoryStream(); //使得输入密码必须输入英文文本
CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
} ///
/// DEC 解密过程
///
///
///
///
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量,此值重要,不能修改
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
return System.Text.Encoding.Default.GetString(ms.ToArray());
} ///
/// 检查己加密的字符串是否与原文相同
///
///
///
///
///
public bool ValidateString(string EnString, string FoString, int Mode)
{
switch (Mode)
{
default:
case 1:
if (Decrypt(EnString,_QueryStringKey) == FoString.ToString())
{
return true;
}
else
{
return false;
}
case 2:
if (Decrypt(EnString,_PassWordKey) == FoString.ToString())
{
return true;
}
else
{
return false;
}
}
}
}
public string DESEncrypt(string encryptstring)
{
string strRtn;
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring);
MemoryStream ms = new MemoryStream();//存储加密后的数据
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);//进行加密
cs.FlushFinalBlock();
strRtn = Convert.ToBase64String(ms.ToArray());
return strRtn;
} catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}=================================================================================================//解密
public string DESDecrypt(string decryptstring)
{
string strRtn;
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
byte[] data = Convert.FromBase64String(decryptstring);
MemoryStream ms = new MemoryStream();//存储解密后的数据
CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);//解密数据
cs.FlushFinalBlock();
strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray());
return strRtn;
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}