C#Base64編碼解碼__編碼

來源:互聯網
上載者:User
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;        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.