编程语言

img bgu

[我的ASP.net学习历程]调用类库函库的简单加密方法

发表于2004/9/24 11:01:00  1104人阅读

ASP.net自带了一个MD5和SHA1加密类库!
下面是调用此类库的两种加密方法:

=====================

  public string GetMD5(string strData)
  {
   //使用MD5加密方法:
   System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
   byte[] md5Bytes = System.Text.Encoding.Default.GetBytes(strData);
   byte[] cryString = md5.ComputeHash(md5Bytes);
   string md5Str = string.Empty;
   for (int i=0;i<cryString.Length;i++)
   {
    md5Str += cryString[i].ToString("X");
   }
   return md5Str;
  }

  public string GetEncrypt(string strData,string strType)
  {
   //使用鍵值哈希算法中的MD5或SHA1的加密方法:
   string strCryData = string.Empty;
   if (strType.ToUpper() == "SHA1")
   {
    strCryData = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strData,"SHA1");
   }
   else if (strType.ToUpper() == "MD5")
   {
    strCryData = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strData,"MD5");
   }
   return strCryData;
  }

  public string GetSHA256(string strData)
  {
   //使用SHA256加密算法:
   System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();
   byte[] sha256Bytes = System.Text.Encoding.Default.GetBytes(strData);
   byte[] cryString = sha256.ComputeHash(sha256Bytes);
   string sha256Str = string.Empty;
   for (int i=0;i<cryString.Length;i++)
   {
    sha256Str += cryString[i].ToString("X");
   }
   return sha256Str;
  }

  public string GetSHA384(string strData)
  {
   //使用SHA384加密算法:
   System.Security.Cryptography.SHA384 sha384 = new System.Security.Cryptography.SHA384Managed();
   byte[] sha384Bytes = System.Text.Encoding.Default.GetBytes(strData);
   byte[] cryString = sha384.ComputeHash(sha384Bytes);
   string sha384Str = string.Empty;
   for (int i=0;i<cryString.Length;i++)
   {
    sha384Str += cryString[i].ToString("X");
   }
   return sha384Str;
  }

  public string GetSHA512(string strData)
  {
   //使用SHA512加密算法:
   System.Security.Cryptography.SHA512 sha512 = new System.Security.Cryptography.SHA512Managed();
   byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes(strData);
   byte[] cryString = sha512.ComputeHash(sha512Bytes);
   string sha512Str = string.Empty;
   for (int i=0;i<cryString.Length;i++)
   {
    sha512Str += cryString[i].ToString("X");
   }
   return sha512Str;
  }

阅读全文
0 0

相关文章推荐

img
取 消
img