C# Base64Engine

BASE64是一种很常见的编码方式,它的编码规则

为将三个字节容器内的数据通过位运算计算到四字

节容器内寄存,每76个字符尾部加=具体可以参考

百度百科、结尾符号使用=号表示

using System;
using System.Collections.Generic;
using System.Text;

namespace Base64Engine
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Base64Engine";

            string strLrcValue = "渺云烟 看不透凡间 月楼轻舞衣袖蹁跹";
            string strBase64CodeX = Convert.ToBase64String(Encoding.Default.GetBytes(strLrcValue));
            string strRlstValueX = Encoding.Default.GetString(Convert.FromBase64String(strBase64CodeX));
            string strBase64CodeY = Base64Encrypt(Encoding.Default.GetBytes(strLrcValue));
            string strRlstValueY = Encoding.Default.GetString(Base64Decrypt(strBase64CodeY));

            Console.WriteLine("var strLrcValue:= " + strLrcValue);
            Console.WriteLine("var strBase64CodeX:= " + strBase64CodeX);
            Console.WriteLine("var strRlstValueX:= " + strRlstValueX);
            Console.WriteLine("var strBase64CodeY:= " + strBase64CodeY);
            Console.WriteLine("var strRlstValueY:= " + strRlstValueY);

            Console.ReadKey(false);
        }

        public static string Base64Encrypt(byte[] buffer)
        {
            int length, remainder;
            if (buffer == null && (length = buffer.Length) < 1)
                return string.Empty;
            if ((remainder = buffer.Length % 3) > 0)
            {
                List<byte> bytes = new List<byte>();
                bytes.AddRange(buffer);
                bytes.AddRange(new byte[3 - remainder]);
                buffer = bytes.ToArray();
            }
            length = buffer.Length;
            byte[] cache = new byte[3];
            char[] strc = new char[length * 4 / 3];
            string items = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            for (int i = 0, j = 0; j < length; j += 3, i += 4)
            {
                cache[0] = buffer[j];
                cache[1] = buffer[j + 1];
                cache[2] = buffer[j + 2];
                strc[i] = items[cache[0] >> 2];
                strc[i + 1] = items[((cache[0] & 3) << 4) + (cache[1] >> 4)];
                strc[i + 2] = items[((cache[1] & 15) << 2) + (cache[2] >> 6)];
                strc[i + 3] = items[(cache[2] & 63)];
            }
            if (remainder > 0)
            {
                length = strc.Length;
                if (remainder == 1)
                    strc[length - 2] = '=';
                strc[length - 1] = '=';
            }
            return new string(strc);
        }

        public static byte[] Base64Decrypt(string buffer)
        {
            int length, multiple, encode, n = 0;
            List<byte> retVal = new List<byte>();
            if (buffer != null && (length = buffer.Length) > 0)
            {
                multiple = length / 4;
                if (length % 4 != 0)
                    multiple += 1;
                byte[] s3c = new byte[3]; // 三字节
                byte[] s4c = new byte[4]; // 四字节
                string items = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                for (int i = 0; i < multiple; i++)
                {
                    for (n = 0; n < 4; n++)
                    {
                        s4c[n] = (byte)buffer[i * 4 + n];
                        if ((encode = items.IndexOf((char)s4c[n])) < 0)
                            break;
                        s4c[n] = (byte)encode;
                    }
                    s3c[0] = (byte)(s4c[0] * 4 | s4c[1] / 16);
                    s3c[1] = (byte)(s4c[1] * 16 | s4c[2] / 4);
                    s3c[2] = (byte)(s4c[2] * 64 | s4c[3]);
                    retVal.AddRange(s3c);
                }
                if (n <= 4)
                {
                    n = 4 - n;
                    length = retVal.Count;
                    for (int i = 0; i < n; i++)
                        retVal.RemoveAt(--length);
                }
            }
            return retVal.ToArray();
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值