標籤:
一、MD5加密解密
1、加密
public static string ToMd5(string clearString)
{ Byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearString); string hashedPwd = BitConverter.ToString(((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes)); return hashedPwd; }
2、32位MD5加密
public static string GetMd5Base32(string sInput)
{ System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); System.Security.Cryptography.MD5 md5; byte[] bytesSrc; byte[] result; StringBuilder sb = new StringBuilder(); bytesSrc = encoding.GetBytes(sInput); md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); result = md5.ComputeHash(bytesSrc); for (int i = 0; i < result.Length; i++) { sb.AppendFormat("{0:x2}", result[i]); } return sb.ToString(); }
二、base64演算法加密解密
/// <summary> /// 有關base64編碼演算法的相關操作 /// </summary> public class Base64 { public Base64() { } /// <summary> /// 將字串使用base64演算法加密 /// </summary> /// <param name="sourceString">待加密的字串</param> /// <param name="ens">System.Text.Encoding 對象,如建立中文編碼集對象:System.Text.Encoding.GetEncoding(54936)</param> /// <returns>加碼後的文本字串</returns> public static string EncodingForString(string sourceString, System.Text.Encoding ens) { return Convert.ToBase64String(ens.GetBytes(sourceString)); }
/// <summary> /// 將字串使用base64演算法加密 /// </summary> /// <param name="sourceString">待加密的字串</param> /// <returns>加碼後的文本字串</returns> public static string EncodingForString(string sourceString) { return EncodingForString(sourceString, System.Text.Encoding.GetEncoding(54936)); } /// <summary> /// 從base64編碼的字串中還原字串,支援中文 /// </summary> /// <param name="base64String">base64加密後的字串</param> /// <param name="ens">System.Text.Encoding 對象,如建立中文編碼集對象:System.Text.Encoding.GetEncoding(54936)</param> /// <returns>還原後的文本字串</returns> public static string DecodingForString(string base64String, System.Text.Encoding ens) { /** * *********************************************************** * * 從base64String中取得的位元組值為字元的機內碼(ansi字元編碼) * 一般的,用機內碼轉換為漢字是公式: * (char)(第一位元組的二進位值*256+第二位元組值) * 而在c#中的char或string由於採用了unicode編碼,就不能按照上面的公式計算了 * ansi的位元組編和unicode編碼不相容 * 故利用.net類庫提供的編碼類別實現從ansi編碼到unicode代碼的轉換 * * GetEncoding 方法依賴於基礎平台支援大部分字碼頁。但是,對於下列情況提供系統支援:預設編碼,即在執行此方法的電腦的地區設定中指定的編碼;Little- Endian Unicode (UTF-16LE);Big-Endian Unicode (UTF-16BE);Windows 作業系統 (windows-1252);UTF-7;UTF-8;ASCII 以及 GB18030(簡體中文)。 * *指定下表 中列出的其中一個名稱以擷取具有對應字碼頁的系統支援的編碼。 * * 字碼頁 名稱 * 1200 “UTF-16LE”、“utf-16”、“ucs-2”、“unicode”或“ISO-10646-UCS-2” * 1201 “UTF-16BE”或“unicodeFFFE” * 1252 “windows-1252” * 65000 “utf-7”、“csUnicode11UTF7”、“unicode-1-1-utf-7”、“unicode-2-0-utf-7”、“x- unicode-1-1-utf-7”或“x-unicode-2-0-utf-7” * 65001 “utf-8”、“unicode-1-1-utf-8”、“unicode-2-0-utf-8”、“x-unicode-1-1-utf-8”或 “x-unicode-2-0-utf-8” * 20127 “us-ascii”、“us”、“ascii”、“ANSI_X3.4-1968”、“ANSI_X3.4-1986”、“cp367”、 “csASCII”、“IBM367”、“iso-ir-6”、“ISO646-US”或“ISO_646.irv:1991” * 54936 “GB18030” * * 某些平台可能不支援特定的字碼頁。例如,Windows 98 的美國版本可能不支援日語 Shift-jis 字碼頁(字碼頁 932)。這種情況下,GetEncoding 方法將在執行下面的 C# 代碼時引發 NotSupportedException: * * Encoding enc = Encoding.GetEncoding("shift-jis"); **************************************************************/ // 從base64String中得到原始字元 return ens.GetString(Convert.FromBase64String(base64String)); } /// <summary> /// 從base64編碼的字串中還原字串,支援中文 /// </summary> /// <param name="base64String">base64加密後的字串</param> /// <returns>還原後的文本字串</returns> public static string DecodingForString(string base64String) { return DecodingForString(base64String, System.Text.Encoding.GetEncoding(54936)); } //-------------------------------------------------------------------------------------- /// <summary> /// 對任意類型的檔案進行base64加碼 /// </summary> /// <param name="fileName">檔案的路徑和檔案名稱</param> /// <returns>對檔案進行base64編碼後的字串</returns> public static string EncodingForFile(string fileName) { System.IO.FileStream fs = System.IO.File.OpenRead(fileName); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); /*System.Byte[] b=new System.Byte[fs.Length]; fs.Read(b,0,Convert.ToInt32(fs.Length));*/ string base64String = Convert.ToBase64String(br.ReadBytes((int)fs.Length)); br.Close(); fs.Close(); return base64String; } /// <summary> /// 把經過base64編碼的字串儲存為檔案 /// </summary> /// <param name="base64String">經base64加碼後的字串</param> /// <param name="fileName">儲存檔案的路徑和檔案名稱</param> /// <returns>儲存檔案是否成功</returns> public static bool SaveDecodingToFile(string base64String, string fileName) { System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs); bw.Write(Convert.FromBase64String(base64String)); bw.Close(); fs.Close(); return true; } //------------------------------------------------------------------------------- /// <summary> /// 從網路地址一取得檔案並轉化為base64編碼 /// </summary> /// <param name="url">檔案的url地址,一個絕對的url地址</param> /// <param name="objWebClient">System.Net.WebClient 對象</param> /// <returns></returns> public static string EncodingFileFromUrl(string url, System.Net.WebClient objWebClient) { return Convert.ToBase64String(objWebClient.DownloadData(url)); } /// <summary> /// 從網路地址一取得檔案並轉化為base64編碼 /// </summary> /// <param name="url">檔案的url地址,一個絕對的url地址</param> /// <returns>將檔案轉化後的base64字串</returns> public static string EncodingFileFromUrl(string url) { //System.Net.WebClient myWebClient = new System.Net.WebClient(); return EncodingFileFromUrl(url, new System.Net.WebClient()); } }
三、SHA1加密
private string getSHA(string code)
{ if (!String.IsNullOrEmpty(code)) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(code, "SHA1").ToLower(); } else { return string.Empty; } }
四、AES加密解密
/// <summary> /// AES加密解密 /// </summary> public class AES { /// <summary> /// 擷取密鑰 /// </summary> private static string Key { get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z‘e9M"; } } /// <summary> /// 擷取向量 /// </summary> private static string IV { get { return @"L+/~f4,Ir)b$=pkf"; } } /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字串</param> /// <returns>密文</returns> public static string AESEncrypt(string plainStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } } catch { } aes.Clear(); return encrypt; } /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字串</param> /// <param name="returnNull">加密失敗時是否返回 null,false 返回 String.Empty</param> /// <returns>密文</returns> public static string AESEncrypt(string plainStr, bool returnNull) { string encrypt = AESEncrypt(plainStr); return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt); } /// <summary> /// AES解密 /// </summary> /// <param name="encryptStr">密文字串</param> /// <returns>明文</returns> public static string AESDecrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch { } aes.Clear(); return decrypt; } /// <summary> /// AES解密 /// </summary> /// <param name="encryptStr">密文字串</param> /// <param name="returnNull">解密失敗時是否返回 null,false 返回 String.Empty</param> /// <returns>明文</returns> public static string AESDecrypt(string encryptStr, bool returnNull) { string decrypt = AESDecrypt(encryptStr); return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt); } }
C#_加密解密