C#加密幫助類EncryptHelper
///之前這篇加密幫助類覺得不夠嚴謹,不夠強,所以特意修改,也對自己負責一點,如果對你有幫助可以看看,
///呵呵!~這個類都經過本人測試過都沒問題.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using log4net;
namespace Utility
{
///
/// @Author:梁繼龍
/// @Date:2012/7/30
/// @Descripte:EncryptHelper加密幫助類.
///
public class EncryptHelper
{
///
///方法一:
///此種加密之後的字符串是三十二位的(字母加數據)字符串
/// Example: password是admin 加密變成後21232f297a57a5a743894a0e4a801fc3
///
///
///
public string MD5Encrypt(string beforeStr)
{
string afterString = "";
try
{
MD5 md5 = MD5.Create();
byte[] hashs = md5.ComputeHash(Encoding.UTF8.GetBytes(beforeStr));
foreach (byte by in hashs)
//這裡是字母加上數據進行加密.//3y 可以,y3不可以或 x3j等應該是超過32位不可以
afterString += by.ToString("x2");
}
catch (Exception ex)
{
ILog log = log4net.LogManager.GetLogger(this.GetType());
log.Error("==============你引起了一個錯誤是==============" + ex.Message.ToString());
}
return afterString;
}
///
/// 方法二
/// HashAlgorithm加密
/// 這種加密是 字母加-加字符
/// Example: password是admin 加密變成後19-A2-85-41-44-B6-3A-8F-76-17-A6-F2-25-01-9B-12
///
///
///
public String HashEncrypt(string password)
{
Byte[] hashedBytes = null;
try
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
}
catch (Exception ex)
{
ILog log = log4net.LogManager.GetLogger(this.GetType());
log.Error("==============你引起了一個錯誤是==============" + ex.Message.ToString());
}
return BitConverter.ToString(hashedBytes);//MD5加密
}
///
/// 方法三:
/// MD5 + HashCode加密
/// Example: password是admin 加密變成後 895b7da64943134be17b825ce118456c
///
///
public String MD5HashCodeEncrypt(string EncryptPwd)
{
return MD5Encrypt(HashEncrypt(EncryptPwd)); //在HashEncrypt基礎上再MD5
}
///
/// 方法四:
/// HashCode +MD5 加密
/// Example: password是admin 加密變成後EB-1D-6D-E2-FC-F1-CD-94-4D-75-78-E6-3D-7A-12-32
///
///
///
public String HashCodeMD5Encrypt(string EncryptPwd)
{
return HashEncrypt(MD5Encrypt(EncryptPwd)); //在MD5基礎再HashCode
}
///
/// 方法五
///
///
///
public String HashMD5Encrypt(string EncryptPwd)
{
return HashCodeMD5Encrypt(HashCodeMD5Encrypt(EncryptPwd)); //在HashCodeMD5Encrypt基礎再HashCode
}
///
/// 方法六
/// 哈哈是不是有點暈呢?
/// 大家伙可以繼續寫.
///
///
///
public String MD5HashEncrypt(string EncryptPwd)
{
return MD5HashCodeEncrypt(MD5HashCodeEncrypt(EncryptPwd)); //在MD5基礎再HashCode
}
///
/// 64位雙重MD5小寫
///
///
public static string Last64(string s)
{
if (s.Length != 32)
return "";
string s1 = s.Substring(0, 16);
string s2 = s.Substring(16, 16);
return Lower32(s1) + Lower32(s2);
}
///
/// 32位大寫
///
///
public static string Upper32(string s)
{
s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
return s.ToUpper();
}
///
/// 32位小寫
///
///
public static string Lower32(string s)
{
s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
return s.ToLower();
}
///
/// 16位大寫
///
///
public static string Upper16(string s)
{
s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
return s.ToUpper().Substring(8, 16);
}
///
/// 16位小寫
///
///
public static string Lower16(string s)
{
s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
return s.ToLower().Substring(8, 16);
}
}
}