ASP.NET(C#)常用数据加密和解密方法

1、C#常用加密解密类库代码如下:

 

以下是代码片段:
  ///
  /// MD5 加密静态方法
  ///
  /// 待加密的密文
  /// returns
  public static string MD5Encrypt(string EncryptString)
  {
  if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
  MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
  string m_strEncrypt = "";
  try
  {
  m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
  }
  catch (ArgumentException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_ClassMD5.Clear(); }
  return m_strEncrypt;
  }
  ///
  /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
  ///
  /// 待加密的密文
  /// 加密的密钥
  /// returns
  public static string DESEncrypt(string EncryptString, string EncryptKey)
  {
  if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
  if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
  if (EncryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
  byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  string m_strEncrypt = "";
  DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
  try
  {
  byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
  MemoryStream m_stream = new MemoryStream();
  CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
  m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
  m_cstream.FlushFinalBlock();
  m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
  m_stream.Close(); m_stream.Dispose();
  m_cstream.Close(); m_cstream.Dispose();
  }
  catch (IOException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (ArgumentException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_DESProvider.Clear(); }
  return m_strEncrypt;
  }
  ///
  /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
  ///
  /// 待解密的密文
  /// 解密的密钥
  /// returns
  public static string DESDecrypt(string DecryptString, string DecryptKey)
  {
  if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
  if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
  if (DecryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
  byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  string m_strDecrypt = "";
  DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
  try
  {
  byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
  MemoryStream m_stream = new MemoryStream();
  CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
  m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
  m_cstream.FlushFinalBlock();
  m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
  m_stream.Close(); m_stream.Dispose();
  m_cstream.Close(); m_cstream.Dispose();
  }
  catch (IOException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (ArgumentException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_DESProvider.Clear(); }
  return m_strDecrypt;
  }
  ///
  /// RC2 加密(用变长密钥对大量数据进行加密)
  ///
  /// 待加密密文
  /// 加密密钥
  /// returns
  public static string RC2Encrypt(string EncryptString, string EncryptKey)
  {
  if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
  if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
  if (EncryptKey.Length < 5 || EncryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
  string m_strEncrypt = "";
  byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
  try
  {
  byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
  MemoryStream m_stream = new MemoryStream();
  CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
  m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
  m_cstream.FlushFinalBlock();
  m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
  m_stream.Close(); m_stream.Dispose();
  m_cstream.Close(); m_cstream.Dispose();
  }
  catch (IOException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (ArgumentException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_RC2Provider.Clear(); }
  return m_strEncrypt;
  }
  ///
  /// RC2 解密(用变长密钥对大量数据进行加密)
  ///
  /// 待解密密文
  /// 解密密钥
  /// returns
  public static string RC2Decrypt(string DecryptString, string DecryptKey)
  {
  if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
  if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
  if (DecryptKey.Length < 5 || DecryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
  byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  string m_strDecrypt = "";
  RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
  try
  {
  byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
  MemoryStream m_stream = new MemoryStream();
  CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
  m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
  m_cstream.FlushFinalBlock();
  m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
  m_stream.Close(); m_stream.Dispose();
  m_cstream.Close(); m_cstream.Dispose();
  }
  catch (IOException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (ArgumentException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_RC2Provider.Clear(); }
  return m_strDecrypt;
  }
  ///
  /// 3DES 加密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
  ///
  /// 待加密密文
  /// 密钥一
  /// 密钥二
  /// 密钥三
  /// returns
  public static string DES3Encrypt(string EncryptString, string EncryptKey1, string EncryptKey2, string EncryptKey3)
  {
  string m_strEncrypt = "";
  try
  {
  m_strEncrypt = DESEncrypt(EncryptString, EncryptKey3);
  m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey2);
  m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey1);
  }
  catch (Exception ex) { throw ex; }
  return m_strEncrypt;
  }
  ///
  /// 3DES 解密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
  ///
  /// 待解密密文
  /// 密钥一
  /// 密钥二
  /// 密钥三
  /// returns
  public static string DES3Decrypt(string DecryptString, string DecryptKey1, string DecryptKey2, string DecryptKey3)
  {
  string m_strDecrypt = "";
  try
  {
  m_strDecrypt = DESDecrypt(DecryptString, DecryptKey1);
  m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey2);
  m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey3);
  }
  catch (Exception ex) { throw ex; }
  return m_strDecrypt;
  }
  ///
  /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
  ///
  /// 待加密密文
  /// 加密密钥
  ///
  public static string AESEncrypt(string EncryptString, string EncryptKey)
  {
  if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
  if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
  string m_strEncrypt = "";
  byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
  Rijndael m_AESProvider = Rijndael.Create();
  try
  {
  byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
  MemoryStream m_stream = new MemoryStream();
  CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
  m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_csstream.FlushFinalBlock();
  m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
  m_stream.Close(); m_stream.Dispose();
  m_csstream.Close(); m_csstream.Dispose();
  }
  catch (IOException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (ArgumentException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_AESProvider.Clear(); }
  return m_strEncrypt;
  }
  ///
  /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
  ///
  /// 待解密密文
  /// 解密密钥
  ///
  public static string AESDecrypt(string DecryptString, string DecryptKey)
  {
  if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
  if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
  string m_strDecrypt = "";
  byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
  Rijndael m_AESProvider = Rijndael.Create();
  try
  {
  byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
  MemoryStream m_stream = new MemoryStream();
  CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
  m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
  m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
  m_stream.Close(); m_stream.Dispose();
  m_csstream.Close(); m_csstream.Dispose();
  }
  catch (IOException ex) { throw ex; }
  catch (CryptographicException ex) { throw ex; }
  catch (ArgumentException ex) { throw ex; }
  catch (Exception ex) { throw ex; }
  finally { m_AESProvider.Clear(); }
  return m_strDecrypt;
  }

  2、数据加密和解密简单代码调用如下:

以下是代码片段:
  Response.Write("
-----------MD5加密---------------
");
  Response.Write(SDKSecurity.MD5Encrypt("仰天一笑"));
  Response.Write("
-----------DES加密---------------
");
  Response.Write(SDKSecurity.DESEncrypt("仰天一笑", "anson-xu"));
  Response.Write("
-----------DES解密---------------
");
  Response.Write(SDKSecurity.DESDecrypt("l06JvJ45r/lb9iKzSXl47Q==", "anson-xu"));
  Response.Write("
-----------AES加密---------------
");
  Response.Write(SDKSecurity.AESEncrypt("仰天一笑", "ansonxuyu"));
  Response.Write("
-----------AES解密---------------
");
  Response.Write(SDKSecurity.AESDecrypt("avwKL+MO8+zoLHvzk0+TBA==", "ansonxuyu"));

  3、数据加密和解密调用后运行效果图如下:

 

 

 

 

 

 

  • using System;   
  • using System.Security.Cryptography;    
  • using System.IO;   
  • using System.Data;   
  • using System.Web;   
  • using System.Text;   
  • using System.Security;   
  • namespace CMIS.Common   
  • {   
  •     /// <summary>   
  •     /// 加密解密   
  •     /// </summary>   
  •     public class Encrypt   
  •     {   
  •         #region DES密钥   
  •         /// <summary>   
  •         /// DES密钥   
  •         /// </summary>   
  •         public static byte[] DESKey = new byte[] {0x520xBC0xA10x6A0xD50x870x3B0xE60x820x7A0x2A0x640x3A0x7F0x320xBB0x1B0x670xE20x5E0x060x190xB90x2D0x680xB30x4F0xFB0xBF0xDD0x55 ,0xB8 };    
  •         #endregion   
  •   
  •         #region DES加密解密   
  •         /// <summary>   
  •         /// DES加密   
  •         /// </summary>   
  •         /// <param name="strSource">待加密字串</param>   
  •         /// <param name="key">32位Key值</param>   
  •         /// <returns>加密后的字符串</returns>   
  •         public static string DESEncrypt(string strSource)    
  •         {   
  •             return DESEncrypt(strSource, DESKey);   
  •         }   
  •         public static string DESEncrypt(string strSource,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[] byt = Encoding.Unicode.GetBytes(strSource);   
  •             cs.Write(byt, 0, byt.Length);   
  •             cs.FlushFinalBlock();   
  •             cs.Close();   
  •             return Convert.ToBase64String(ms.ToArray());   
  •         }   
  •         /// <summary>   
  •         /// DES解密   
  •         /// </summary>   
  •         /// <param name="strSource">待解密的字串</param>   
  •         /// <param name="key">32位Key值</param>   
  •         /// <returns>解密后的字符串</returns>   
  •         public static string DESDecrypt(string strSource)    
  •         {   
  •             return DESDecrypt(strSource, DESKey);   
  •         }   
  •         public static string DESDecrypt(string strSource,byte[] key)   
  •         {   
  •             SymmetricAlgorithm sa = Rijndael.Create();   
  •             sa.Key = key;   
  •             sa.Mode = CipherMode.ECB;   
  •             sa.Padding = PaddingMode.Zeros;   
  •             ICryptoTransform ct = sa.CreateDecryptor();   
  •             byte[] byt = Convert.FromBase64String(strSource);   
  •             MemoryStream ms = new MemoryStream(byt);   
  •             CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);   
  •             StreamReader sr = new StreamReader(cs, Encoding.Unicode);   
  •             return sr.ReadToEnd();   
  •         }   
  •         #endregion   
  •   
  •         #region 一个用hash实现的加密解密方法   
  •         /// <summary>   
  •         /// 加密   
  •         /// </summary>   
  •         /// <param name="src"></param>   
  •         /// <returns></returns>   
  •         public static string EncryptStrByHash(string src)      
  •         {    
  •             if (src.Length==0)   
  •             {   
  •                 return   "";    
  •             }   
  •             byte[] HaKey=System.Text.Encoding.ASCII.GetBytes((src+"Test").ToCharArray());    
  •             byte[] HaData=new byte[20];    
  •             HMACSHA1 Hmac=new HMACSHA1(HaKey);    
  •             CryptoStream cs=new CryptoStream(Stream.Null,Hmac,CryptoStreamMode.Write);    
  •             try      
  •             {    
  •                 cs.Write(HaData,0,HaData.Length);    
  •             }      
  •             finally      
  •             {    
  •                 cs.Close();    
  •             }    
  •             string   HaResult   =   System.Convert.ToBase64String(Hmac.Hash).Substring(0,16);    
  •             byte[]   RiKey   =   System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray());    
  •             byte[]   RiDataBuf   =   System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());    
  •             byte[]   EncodedBytes   =   {};    
  •             MemoryStream   ms   =   new   MemoryStream();    
  •             RijndaelManaged   rv   =   new   RijndaelManaged();    
  •             cs   =   new   CryptoStream(ms,   rv.CreateEncryptor(RiKey,   RiKey),   CryptoStreamMode.Write);    
  •             try      
  •             {    
  •                 cs.Write(RiDataBuf,   0,   RiDataBuf.Length);    
  •                 cs.FlushFinalBlock();    
  •                 EncodedBytes   =   ms.ToArray();    
  •             }      
  •             finally      
  •             {    
  •                 ms.Close();    
  •                 cs.Close();    
  •             }    
  •             return   HaResult+System.Convert.ToBase64String(EncodedBytes);    
  •         }    
  •   
  •         /// <summary>   
  •         /// 解密   
  •         /// </summary>   
  •         /// <param name="src"></param>   
  •         /// <returns></returns>   
  •         public static string DecrypStrByHash(string src)    
  •         {    
  •             if   (src.Length<40)   return   "";    
  •             byte[]   SrcBytes   =   System.Convert.FromBase64String(src.Substring(16));    
  •             byte[]   RiKey   =   System.Text.Encoding.ASCII.GetBytes(src.Substring(0,16).ToCharArray());    
  •             byte[]   InitialText   =   new   byte[SrcBytes.Length];    
  •             RijndaelManaged   rv   =   new   RijndaelManaged();    
  •             MemoryStream   ms   =   new   MemoryStream(SrcBytes);    
  •             CryptoStream   cs   =   new   CryptoStream(ms,   rv.CreateDecryptor(RiKey,   RiKey),   CryptoStreamMode.Read);    
  •             try      
  •             {    
  •                 cs.Read(InitialText,   0,   InitialText.Length);    
  •             }      
  •             finally      
  •             {    
  •                 ms.Close();    
  •                 cs.Close();    
  •             }    
  •             System.Text.StringBuilder   Result   =   new   System.Text.StringBuilder();    
  •             for(int   i=0;   i   <   InitialText.Length;   ++i)   if   (InitialText[i]>0)   Result.Append((char)   InitialText[i]);    
  •             return   Result.ToString();    
  •         }    
  •       
  •         /// <summary>   
  •         /// 对加密后的密文重新编码,如果密文长>16,则去掉前16个字符,如果长度小于16,返回空字符串   
  •         /// </summary>   
  •         /// <param name="s"></param>   
  •         /// <returns></returns>   
  •         public static string ReEncryptStrByHash(string   s)      
  •         {    
  •             //string e= (new Encrypt()).EncryptStrByHash(s);    
  •             string e= EncryptStrByHash(s);    
  •             return ((e.Length>16)?e.Substring(16):"");    
  •         }   
  •         #endregion   
  •   
  •         #region Md5加密,生成16位或32位,生成的密文都是大写   
  •         /// <summary>   
  •         /// MD5 16位加密   
  •         /// </summary>   
  •         /// <param name="str"></param>   
  •         /// <returns></returns>   
  •         public static string Md5To16(string str)   
  •         {   
  •             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();   
  •             string pwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 48);   
  •             pwd = pwd.Replace("-""");   
  •             return pwd;   
  •         }   
  •      
  •         /// <summary>   
  •         /// MD5 32位加密   
  •         /// </summary>   
  •         /// <param name="str"></param>   
  •         /// <returns></returns>   
  •         public static string Md5To32(string str)   
  •         {   
  •             string pwd = "";   
  •             MD5 md5 = MD5.Create();   
  •             byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));   
  •             for (int i = 0; i < s.Length; i++){   
  •                 pwd = pwd + s[i].ToString("X2");   
  •             }   
  •             return pwd;   
  •         }   
  •         #endregion   
  •   
  •         #region 3DES加密解密   
  •         /// <summary>   
  •         /// 3DES加密   
  •         /// </summary>   
  •         /// <param name="str"></param>   
  •         /// <returns></returns>   
  •         public static string Encrypt3DES(string str)   
  •         {   
  •             //密钥   
  •             string sKey = "wyw308";   
  •             //    //矢量,可为空   
  •             string sIV = "scf521";   
  •             //    //构造对称算法   
  •             SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();   
  •   
  •             ICryptoTransform ct;   
  •             MemoryStream ms;   
  •             CryptoStream cs;   
  •             byte[] byt;   
  •             mCSP.Key = Convert.FromBase64String(sKey);   
  •             mCSP.IV = Convert.FromBase64String(sIV);   
  •             mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;   
  •             mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;   
  •             ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);   
  •             byt = Encoding.UTF8.GetBytes(str);   
  •             ms = new MemoryStream();   
  •             cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);   
  •             cs.Write(byt, 0, byt.Length);   
  •             cs.FlushFinalBlock();   
  •             cs.Close();   
  •             return Convert.ToBase64String(ms.ToArray());   
  •         }   
  •         /// <summary>   
  •         /// 带指定密钥和矢量的3DES加密   
  •         /// </summary>   
  •         /// <param name="str"></param>   
  •         /// <param name="sKey"></param>   
  •         /// <param name="sIV"></param>   
  •         /// <returns></returns>   
  •         public static string Encrypt3DES(string str,string sKey,string sIV)   
  •         {   
  •             SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();   
  •             ICryptoTransform ct;   
  •             MemoryStream ms;   
  •             CryptoStream cs;   
  •             byte[] byt;   
  •             mCSP.Key = Convert.FromBase64String(sKey);   
  •             mCSP.IV = Convert.FromBase64String(sIV);   
  •             mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;   
  •             mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;   
  •             ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);   
  •             byt = Encoding.UTF8.GetBytes(str);   
  •             ms = new MemoryStream();   
  •             cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);   
  •             cs.Write(byt, 0, byt.Length);   
  •             cs.FlushFinalBlock();   
  •             cs.Close();   
  •             return Convert.ToBase64String(ms.ToArray());   
  •         }   
  •   
  •         /// <summary>   
  •         /// 3DES解密   
  •         /// </summary>   
  •         /// <param name="Value"></param>   
  •         /// <returns></returns>   
  •         public static string Decrypt3DES(string Value)   
  •         {   
  •             string sKey = "wyw308";   
  •             string sIV = "scf521";   
  •             SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();   
  •             ICryptoTransform ct;   
  •             MemoryStream ms;   
  •             CryptoStream cs;   
  •             byte[] byt;   
  •             mCSP.Key = Convert.FromBase64String(sKey);   
  •             mCSP.IV = Convert.FromBase64String(sIV);   
  •             mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;   
  •             mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;   
  •             ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);   
  •             byt = Convert.FromBase64String(Value);   
  •             ms = new MemoryStream();   
  •             cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);   
  •             cs.Write(byt, 0, byt.Length);   
  •             cs.FlushFinalBlock();   
  •             cs.Close();   
  •             return Encoding.UTF8.GetString(ms.ToArray());   
  •         }    
  •         /// <summary>   
  •         /// 带指定密钥和矢量的3DES解密   
  •         /// </summary>   
  •         /// <param name="Value"></param>   
  •         /// <param name="sKey"></param>   
  •         /// <param name="sIV"></param>   
  •         /// <returns></returns>   
  •         public static string Decrypt3DES(string str,string sKey,string sIV)   
  •         {   
  •             SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();   
  •             ICryptoTransform ct;   
  •             MemoryStream ms;   
  •             CryptoStream cs;   
  •             byte[] byt;   
  •             mCSP.Key = Convert.FromBase64String(sKey);   
  •             mCSP.IV = Convert.FromBase64String(sIV);   
  •             mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;   
  •             mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;   
  •             ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);   
  •             byt = Convert.FromBase64String(str);   
  •             ms = new MemoryStream();   
  •             cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);   
  •             cs.Write(byt, 0, byt.Length);   
  •             cs.FlushFinalBlock();   
  •             cs.Close();   
  •             return Encoding.UTF8.GetString(ms.ToArray());   
  •         }    
  •         #endregion   
  •   
  •         #region 一个简单的加密解密方法,只支持英文   
  •         public static string EnCryptEnStr( string str ) //倒序加1加密   
  •         {   
  •             byte[] by=new byte[str.Length];   
  •             forint i=0;   
  •                 i<=str.Length-1;   
  •                 i++ )   
  •             {   
  •                 by[i]=( byte )( ( byte )str[i]+1 );   
  •             }   
  •             str="";   
  •             forint i=by.Length-1;   
  •                 i>=0;   
  •                 i-- )   
  •             {   
  •                 str+=( ( char )by[i] ).ToString( );   
  •             }   
  •             return str;   
  •         }   
  •         public static string DeCryptEnStr( string str ) //顺序减1解码   
  •         {   
  •             byte[] by=new byte[str.Length];   
  •             forint i=0;   
  •                 i<=str.Length-1;   
  •                 i++ )   
  •             {   
  •                 by[i]=( byte )( ( byte )str[i]-1 );   
  •             }   
  •             str="";   
  •             forint i=by.Length-1;   
  •                 i>=0;   
  •                 i-- )   
  •             {   
  •                 str+=( ( char )by[i] ).ToString( );   
  •             }   
  •             return str;   
  •         }   
  •         #endregion   
  •   
  •         #region 一个简单的加密解密方法,在上一个的基础上支持中文   
  •         public static string EnCryptCnStr(string str)   
  •         {   
  •             string htext = ""// blank text   
  •   
  •             for ( int i = 0; i < str.Length; i++)   
  •             {   
  •                 htext = htext + (char) (str[i] + 10 - 1 * 2);   
  •             }   
  •             return htext;   
  •         }   
  •   
  •         public static string DeCryptCnStr(string str)   
  •         {   
  •             string dtext = "";   
  •   
  •             for ( int i=0; i < str.Length; i++)   
  •             {   
  •                 dtext = dtext + (char) (str[i] - 10 + 1*2);   
  •             }   
  •             return dtext;   
  •         }   
  •         #endregion   
  •   
  •         #region Discuz论坛Md5加密删除   
  •         /// <summary>   
  •         /// Discuz论坛Md5加密   
  •         /// </summary>   
  •         /// <param name="str"></param>   
  •         /// <returns></returns>   
  •         public static string Md5(string str)   
  •         {   
  •             MD5 md5Hasher = MD5.Create();   
  •             string re="";   
  •             byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(str+"%$#@!"));//yc   
  •             for (int i = 0; i < data.Length; i++){   
  •                 re += data[i].ToString("x2");   
  •             }   
  •             return re;   
  •         }           
  •         #endregion   
  •     }   
posted @ 2012-07-30 22:44  星火卓越  阅读(541)  评论(0编辑  收藏  举报