鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 网站安全 > 加密解密 > >

c#密钥 加密 解密 和 MD5加密

来源:互联网 作者:佚名 时间:2015-10-09 06:14
//密钥 加密 , 解密 //C# 加密 Encrypt("中国123asd!@#$", "#s^un2ye31cn%|aoXpR,+vh"); internal static string Encrypt(string pToEncrypt, string sKey) { try { if (pToEncrypt == "") return ""; else { System.Security.Cryptography.TripleDESCrypto

//密钥加密解密
    //C# 加密      Encrypt("中国123asd!@#$", "#s^un2ye31<cn%|aoXpR,+vh");
internal static string Encrypt(string pToEncrypt, string sKey)
        {
            try
            {
                if (pToEncrypt == "")
                    return "";
                else
                {
                    System.Security.Cryptography.TripleDESCryptoServiceProvider des = new

System.Security.Cryptography.TripleDESCryptoServiceProvider();

//System.Security.Cryptography.DESCryptoServiceProvider();
                    des.Key = System.Text.Encoding.UTF8.GetBytes(sKey);
                    des.Mode = System.Security.Cryptography.CipherMode.ECB;
                    System.Security.Cryptography.ICryptoTransform DESEncrypt =

des.CreateEncryptor();
                    byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(pToEncrypt);
                    string retValue = Convert.ToBase64String(DESEncrypt.TransformFinalBlock

(Buffer, 0, Buffer.Length));
                    DESEncrypt.Dispose();
                    return retValue;
                }
            }
            catch { return ""; }
        }

//c#解密  Decrypt("....", "#s^un2ye31<cn%|aoXpR,+vh");
  public static string Decrypt(string pToDecrypt, string sKey)
        {
            try
            {
                if (pToDecrypt == "")
                    return "";
                else
                {
                    System.Security.Cryptography.TripleDESCryptoServiceProvider des = new

System.Security.Cryptography.TripleDESCryptoServiceProvider();
                    des.Key = System.Text.Encoding.UTF8.GetBytes(sKey);
                    des.Mode = System.Security.Cryptography.CipherMode.ECB;
                    des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                    System.Security.Cryptography.ICryptoTransform DESDecrypt =

des.CreateDecryptor();

                    string result = "";

                    byte[] Buffer = Convert.FromBase64String(pToDecrypt);
                    result = System.Text.Encoding.UTF8.GetString

(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
                    DESDecrypt.Dispose();
                    return result;
                }
            }
            catch { return ""; }
        }


//md5 加密
public string MD5(String str)
    {
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new

System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] data = System.Text.Encoding.Default.GetBytes(str);
      byte[] result = md5.ComputeHash(data);
      string s = BitConverter.ToString(result);
      return s;
    }

网友评论
<