C# Base64 編碼處理

  • 1125
  • 0
  • 2017-12-07

C# 中Base64的編碼處理

今天再看手邊專案的時候,發現同事有把要放在URL上Get給客戶AP的資料,做Base64加密的動作

紀錄一下今天看到的東西

寫了一個很簡單的Winform程式來做加解密

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoBase64Transfer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Transfer Base64 Encrypt 將輸入的字串轉為Base64的編碼
        /// 轉為Base64的編碼,如果最後剩下兩個輸入數據,在編碼結果後加1個「=」;如果最後剩下一個輸入數據,
        /// 編碼結果後加2個「=」;
        /// 如果沒有剩下任何數據,就什麼都不要加。
        /// 因此,如果編碼後的字串要放在URL上GET給其他系統,需要用EncryptSpecialChar來置換特殊字元(=)  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            String temp = this.textBox1.Text;
            byte[] bytes = System.Text.Encoding.GetEncoding("utf-8").GetBytes(temp);
            String strBase64 = Convert.ToBase64String(bytes);
            this.textBox2.Text = strBase64;
        }

        /// <summary>
        /// 將Base64的編碼還原回去
        /// 如果Base64字串的再編碼有使用過EncryptSpecialChar置換字元,
        /// 需要使用DecryptSpecialChar來置換特殊字元回去,再重新解碼
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            String strBase64 = this.textBox1.Text;                                    
            this.textBox2.Text = System.Text.Encoding.GetEncoding("utf-8").GetString(Convert.FromBase64String(strBase64));
        }

        /// <summary>
        /// 特殊字元置換(URL內不應該出現的字元,要替換掉)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static string EncryptSpecialChar(string data)
        {
            string returnStr = data;

            returnStr = returnStr.Replace("+", "0x2b");
            returnStr = returnStr.Replace("$", "0x24");
            returnStr = returnStr.Replace("&", "0x26");
            returnStr = returnStr.Replace(",", "0x2c");
            returnStr = returnStr.Replace("/", "0x2f");
            returnStr = returnStr.Replace(":", "0x3a");
            returnStr = returnStr.Replace(";", "0x3b");
            returnStr = returnStr.Replace("=", "0x3d");
            returnStr = returnStr.Replace("?", "0x3f");
            returnStr = returnStr.Replace("@", "0x40");
            returnStr = returnStr.Replace("\"", "0x22");
            returnStr = returnStr.Replace("<", "0x3c");
            returnStr = returnStr.Replace(">", "0x3e");
            returnStr = returnStr.Replace("#", "0x23");
            returnStr = returnStr.Replace("%", "0x25");
            returnStr = returnStr.Replace("{", "0x7b");
            returnStr = returnStr.Replace("}", "0x7d");
            returnStr = returnStr.Replace("|", "0x7c");
            returnStr = returnStr.Replace("\\", "0x5c");
            returnStr = returnStr.Replace("^", "0x5e");
            returnStr = returnStr.Replace("~", "0x7e");
            returnStr = returnStr.Replace("[", "0x5b");
            returnStr = returnStr.Replace("]", "0x5d");
            returnStr = returnStr.Replace("`", "0x60");

            return returnStr;

        }

        /// <summary>
        /// 置換特殊字元(解密為原始字元)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static string DecryptSpecialChar(string data)
        {
            string returnStr = data;

            returnStr = returnStr.Replace("0x2b", "+");
            returnStr = returnStr.Replace("0x24", "$");
            returnStr = returnStr.Replace("0x26", "&");
            returnStr = returnStr.Replace("0x2c", ",");
            returnStr = returnStr.Replace("0x2f", "/");
            returnStr = returnStr.Replace("0x3a", ":");
            returnStr = returnStr.Replace("0x3b", ";");
            returnStr = returnStr.Replace("0x3d", "=");
            returnStr = returnStr.Replace("0x3f", "?");
            returnStr = returnStr.Replace("0x40", "@");
            returnStr = returnStr.Replace("0x22", "\"");
            returnStr = returnStr.Replace("0x3c", "<");
            returnStr = returnStr.Replace("0x3e", ">");
            returnStr = returnStr.Replace("0x23", "#");
            returnStr = returnStr.Replace("0x25", "%");
            returnStr = returnStr.Replace("0x7b", "{");
            returnStr = returnStr.Replace("0x7d", "}");
            returnStr = returnStr.Replace("0x7c", "|");
            returnStr = returnStr.Replace("0x5c", "\\");
            returnStr = returnStr.Replace("0x5e", "^");
            returnStr = returnStr.Replace("0x7e", "~");
            returnStr = returnStr.Replace("0x5b", "[");
            returnStr = returnStr.Replace("0x5d", "]");
            returnStr = returnStr.Replace("0x60", "`");
            return returnStr;
        }
    }
}

參考資料 : [WinForm][演算法] Base64 原理初探 及 C#實作