呼叫類庫函式的簡單加密(轉的)

曉風斜陽發表於2008-06-03
ASP.net自帶了一個MD5和SHA1加密類庫!下面是呼叫此類庫的兩種加密方法:public string GetMD5(string strData)  {   //使用MD5加密方法:   System.Security.Cryptography.MD5 md5 = newSystem.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,stringstrType)  {   //使用鍵值雜湊演算法中的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 = newSystem.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 = newSystem.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;  }

相關文章