C#編寫的Base64加密息爭密類。本站提示廣大學習愛好者:(C#編寫的Base64加密息爭密類)文章只能為提供參考,不一定能成為您想要的結果。以下是C#編寫的Base64加密息爭密類正文
這個C#類是一個基於Base64的加密息爭密類,用戶可以可使用默許的秘鑰停止加密、解密,也能夠本身設定秘鑰停止加密息爭密,異常適用
代碼一:異常精簡的代碼
/// <summary> /// Base64加密,解密辦法 /// </summary> /// <paramname="s">輸出字符串</param> /// <paramname="c">true-加密,false-解密</param> static public string base64(string s, bool c) { if (c) { returnSystem.Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(s)); } else { try { returnSystem.Text.Encoding.Default.GetString(System.Convert.FromBase64String(pwdString)); } catch(Exception exp) { returnexp.Message; } } }
代碼二:
using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// Encrypt 的摘要解釋。 /// </summary> public class DEncrypt { /// <summary> /// 結構辦法 /// </summary> public DEncrypt() { } #region 應用 缺省密鑰字符串 加密/解密string /// <summary> /// 應用缺省密鑰字符串加密string /// </summary> /// <param name="original">明文</param> /// <returns>密文</returns> public static string Encrypt(string original) { return Encrypt(original,"sharejs.com"); } /// <summary> /// 應用缺省密鑰字符串解密string /// </summary> /// <param name="original">密文</param> /// <returns>明文</returns> public static string Decrypt(string original) { return Decrypt(original,"sharejs.com",System.Text.Encoding.Default); } #endregion #region 應用 給定密鑰字符串 加密/解密string /// <summary> /// 應用給定密鑰字符串加密string /// </summary> /// <param name="original">原始文字</param> /// <param name="key">密鑰</param> /// <param name="encoding">字符編碼計劃</param> /// <returns>密文</returns> public static string Encrypt(string original, string key) { byte[] buff = System.Text.Encoding.Default.GetBytes(original); byte[] kb = System.Text.Encoding.Default.GetBytes(key); return Convert.ToBase64String(Encrypt(buff,kb)); } /// <summary> /// 應用給定密鑰字符串解密string /// </summary> /// <param name="original">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</returns> public static string Decrypt(string original, string key) { return Decrypt(original,key,System.Text.Encoding.Default); } /// <summary> /// 應用給定密鑰字符串解密string,前往指定編碼方法明文 /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <param name="encoding">字符編碼計劃</param> /// <returns>明文</returns> public static string Decrypt(string encrypted, string key,Encoding encoding) { byte[] buff = Convert.FromBase64String(encrypted); byte[] kb = System.Text.Encoding.Default.GetBytes(key); return encoding.GetString(Decrypt(buff,kb)); } #endregion #region 應用 缺省密鑰字符串 加密/解密/byte[] /// <summary> /// 應用缺省密鑰字符串解密byte[] /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] encrypted) { byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); return Decrypt(encrypted,key); } /// <summary> /// 應用缺省密鑰字符串加密 /// </summary> /// <param name="original">原始數據</param> /// <param name="key">密鑰</param> /// <returns>密文</returns> public static byte[] Encrypt(byte[] original) { byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); return Encrypt(original,key); } #endregion #region 應用 給定密鑰 加密/解密/byte[] /// <summary> /// 生成MD5摘要 /// </summary> /// <param name="original">數據源</param> /// <returns>摘要</returns> public static byte[] MakeMD5(byte[] original) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); byte[] keyhash = hashmd5.ComputeHash(original); hashmd5 = null; return keyhash; } /// <summary> /// 應用給定密鑰加密 /// </summary> /// <param name="original">明文</param> /// <param name="key">密鑰</param> /// <returns>密文</returns> public static byte[] Encrypt(byte[] original, byte[] key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Key = MakeMD5(key); des.Mode = CipherMode.ECB; return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length); } /// <summary> /// 應用給定密鑰解密數據 /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] encrypted, byte[] key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Key = MakeMD5(key); des.Mode = CipherMode.ECB; return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length); } #endregion } }
代碼三:
using System; using System.Collections.Generic; using System.Text; namespace MyLibs.encrypt { public class Base64 { private static string base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//編碼後的字符集 private static int[] base64DecodeChars = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };//對應ASICC字符的地位 public static string base64encode(string str) { //加密 string Out = ""; int i = 0, len = str.Length; char c1, c2, c3; while (i < len) { c1 = Convert.ToChar(str[i++] & 0xff); if (i == len) { Out += base64EncodeChars[c1 >> 2]; Out += base64EncodeChars[(c1 & 0x3) << 4]; Out += "=="; break; } c2 = str[i++]; if (i == len) { Out += base64EncodeChars[c1 >> 2]; Out += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; Out += base64EncodeChars[(c2 & 0xF) << 2]; Out += "="; break; } c3 = str[i++]; Out += base64EncodeChars[c1 >> 2]; Out += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; Out += base64EncodeChars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]; Out += base64EncodeChars[c3 & 0x3F]; } return Out; } public string utf16to8(string str) { string Out = ""; int i, len; char c;//char為16位Unicode字符,規模0~0xffff,感激vczh提示 len = str.Length; for (i = 0; i < len; i++) {//依據字符的分歧規模分離轉化 c = str[i]; if ((c >= 0x0001) && (c <= 0x007F)) { Out += str[i]; } else if (c > 0x07FF) { Out += (char)(0xE0 | ((c >> 12) & 0x0F)); Out += (char)(0x80 | ((c >> 6) & 0x3F)); Out += (char)(0x80 | ((c >> 0) & 0x3F)); } else { Out += (char)(0xC0 | ((c >> 6) & 0x1F)); Out += (char)(0x80 | ((c >> 0) & 0x3F)); } } return Out; } public static string base64decode(string str) {//解密 int c1, c2, c3, c4; int i, len; string Out; len = str.Length; i = 0; Out = ""; while (i < len) { do { c1 = base64DecodeChars[str[i++] & 0xff]; } while (i < len && c1 == -1); if (c1 == -1) break; do { c2 = base64DecodeChars[str[i++] & 0xff]; } while (i < len && c2 == -1); if (c2 == -1) break; Out += (char)((c1 << 2) | ((c2 & 0x30) >> 4)); do { c3 = str[i++] & 0xff; if (c3 == 61) return Out; c3 = base64DecodeChars[c3]; } while (i < len && c3 == -1); if (c3 == -1) break; Out += (char)(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); do { c4 = str[i++] & 0xff; if (c4 == 61) return Out; c4 = base64DecodeChars[c4]; } while (i < len && c4 == -1); if (c4 == -1) break; Out += (char)(((c3 & 0x03) << 6) | c4); } return Out; } public string utf8to16(string str) { string Out = ""; int i, len; char c, char2, char3; len = str.Length; i = 0; while (i < len) { c = str[i++]; switch (c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: Out += str[i - 1]; break; case 12: case 13: char2 = str[i++]; Out += (char)(((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: char2 = str[i++]; char3 = str[i++]; Out += (char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; } } return Out; } } }
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。