常用的加密方式
WEB上常用的两种加密方式:MD5和SHA512。
这里分享两个使用的方法
都需要用到.net的以下两个空间
using System.Text
using System.Security.Cryptography
1 MD5
(不带密钥,任何文本使用MD5加密后的结果是一致的,有安全隐患)
public string PWDByMd5(string sText)
{
string pwd = "";
MD5 md5 = MD5.Create();
// 加密后是一个字节类型的数组
byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(sText));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("x");
}
return pwd;
}
2 SHA512
(带密钥加密,文本需要在相同密钥的情况下,加密出的结果才会一致)
public static string PWDBySHA512(
string sKey, //密钥
string sText //需要加密的文本
)
{
byte[] HmacKey = System.Text.Encoding.UTF8.GetBytes(sKey);
byte[] HmacData = System.Text.Encoding.UTF8.GetBytes(sText);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HMACSHA512 Hmac = new HMACSHA512(HmacKey);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
cs.Write(HmacData, 0, HmacData.Length);
cs.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] Result = Hmac.Hash;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Convert.ToBase64String(Result); //返回长度为28字节字符串
}
这里分享两个使用的方法
都需要用到.net的以下两个空间
using System.Text
using System.Security.Cryptography
1 MD5
(不带密钥,任何文本使用MD5加密后的结果是一致的,有安全隐患)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
2 SHA512
(带密钥加密,文本需要在相同密钥的情况下,加密出的结果才会一致)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)