using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Cat8637CallCenter{ public class CBase64 { /// <summary> /// 將字串使用base64演算法加密 /// </summary> /// <param name="code_type">編碼類別型(編碼名稱) /// * 字碼頁 名稱 /// * 1200 "UTF-16LE"、"utf-16"、"ucs-2"、"unicode"或"ISO-10646-UCS-2" /// * 1201 /// <summary> /// 將字串使用base64演算法加密 /// </summary> /// <param name="code_type">編碼類別型(編碼名稱) /// * 字碼頁 名稱 /// * 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" /// </param> /// <param name="code">待加密的字串</param> /// <returns>加密後的字串</returns> public static string EncodeBase64(string code_type, string code) { string encode = ""; byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code); //將一組字元編碼為一個位元組序列. try { encode = Convert.ToBase64String(bytes); //將8位不帶正負號的整數數組的子集轉換為其等效的,以64為基的數字編碼的字串形式. } catch { encode = code; } return encode; } public static string EncodeBase64(string code) { string encode = ""; byte[] bytes = Encoding.Default.GetBytes(code); //將一組字元編碼為一個位元組序列. try { encode = Convert.ToBase64String(bytes); //將8位不帶正負號的整數數組的子集轉換為其等效的,以64為基的數字編碼的字串形式. } catch { encode = code; } return encode; } /// <summary> /// 2013-11-05 新增 /// 將字串使用base64演算法解密 /// </summary> /// <param name="code_type">編碼類別型</param> /// <param name="code">已用base64演算法加密的字串</param> /// <returns>解密後的字串</returns> public static string DecodeBase64(string code_type, string code) { string decode = ""; byte[] bytes = Convert.FromBase64String(code); //將2進位編碼轉換為8位不帶正負號的整數數組. try { decode = Encoding.GetEncoding(code_type).GetString(bytes); //將指定位元組數組中的一個位元組序列解碼為一個字串。 } catch { decode = code; } return decode; } public static string DecodeBase64(string code) { string decode = ""; byte[] bytes = Convert.FromBase64String(code); //將2進位編碼轉換為8位不帶正負號的整數數組. try { decode = Encoding.Default.GetString(bytes); //將指定位元組數組中的一個位元組序列解碼為一個字串。 } catch { decode = code; } return decode; } }}