bouncy castle Base 64 Encode and Decode - CSharp System

CSharp examples for System:String Base64

Description

bouncy castle Base 64 Encode and Decode

Demo Code


namespace org.bouncycastle.util.encoders
{
    using System;

    public class Base64
    {//from w  ww  .j ava 2 s  .  c  om
        private static byte[] decodingTable = new byte[0x80];
        private static readonly byte[] encodingTable = new byte[] { 
            0x41, 0x42, 0x43, 0x44, 0x45, 70, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 80,
            0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 90, 0x61, 0x62, 0x63, 100, 0x65, 0x66,
            0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 110, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
            0x77, 120, 0x79, 0x7a, 0x30, 0x31, 50, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f
        };

        static Base64()
        {
            for (int i = 0x41; i <= 90; i++)
            {
                decodingTable[i] = (byte) (i - 0x41);
            }
            for (int j = 0x61; j <= 0x7a; j++)
            {
                decodingTable[j] = (byte) ((j - 0x61) + 0x1a);
            }
            for (int k = 0x30; k <= 0x39; k++)
            {
                decodingTable[k] = (byte) ((k - 0x30) + 0x34);
            }
            decodingTable[0x2b] = 0x3e;
            decodingTable[0x2f] = 0x3f;
        }

        public static byte[] decode(byte[] data)
        {
            byte[] buffer;
            byte num;
            byte num2;
            byte num3;
            byte num4;
            if (data[data.Length - 2] == 0x3d)
            {
                buffer = new byte[(((data.Length / 4) - 1) * 3) + 1];
            }
            else if (data[data.Length - 1] == 0x3d)
            {
                buffer = new byte[(((data.Length / 4) - 1) * 3) + 2];
            }
            else
            {
                buffer = new byte[(data.Length / 4) * 3];
            }
            int index = 0;
            for (int i = 0; index < (data.Length - 4); i += 3)
            {
                num = decodingTable[data[index]];
                num2 = decodingTable[data[index + 1]];
                num3 = decodingTable[data[index + 2]];
                num4 = decodingTable[data[index + 3]];
                buffer[i] = (byte) ((num << 2) | (num2 >> 4));
                buffer[i + 1] = (byte) ((num2 << 4) | (num3 >> 2));
                buffer[i + 2] = (byte) ((num3 << 6) | num4);
                index += 4;
            }
            if (data[data.Length - 2] == 0x3d)
            {
                num = decodingTable[data[data.Length - 4]];
                num2 = decodingTable[data[data.Length - 3]];
                buffer[buffer.Length - 1] = (byte) ((num << 2) | (num2 >> 4));
                return buffer;
            }
            if (data[data.Length - 1] == 0x3d)
            {
                num = decodingTable[data[data.Length - 4]];
                num2 = decodingTable[data[data.Length - 3]];
                num3 = decodingTable[data[data.Length - 2]];
                buffer[buffer.Length - 2] = (byte) ((num << 2) | (num2 >> 4));
                buffer[buffer.Length - 1] = (byte) ((num2 << 4) | (num3 >> 2));
                return buffer;
            }
            num = decodingTable[data[data.Length - 4]];
            num2 = decodingTable[data[data.Length - 3]];
            num3 = decodingTable[data[data.Length - 2]];
            num4 = decodingTable[data[data.Length - 1]];
            buffer[buffer.Length - 3] = (byte) ((num << 2) | (num2 >> 4));
            buffer[buffer.Length - 2] = (byte) ((num2 << 4) | (num3 >> 2));
            buffer[buffer.Length - 1] = (byte) ((num3 << 6) | num4);
            return buffer;
        }

        public static byte[] decode(string data)
        {
            byte[] buffer;
            byte num;
            byte num2;
            byte num3;
            byte num4;
            if (data[data.Length - 2] == '=')
            {
                buffer = new byte[(((data.Length / 4) - 1) * 3) + 1];
            }
            else if (data[data.Length - 1] == '=')
            {
                buffer = new byte[(((data.Length / 4) - 1) * 3) + 2];
            }
            else
            {
                buffer = new byte[(data.Length / 4) * 3];
            }
            int num5 = 0;
            for (int i = 0; num5 < (data.Length - 4); i += 3)
            {
                num = decodingTable[data[num5]];
                num2 = decodingTable[data[num5 + 1]];
                num3 = decodingTable[data[num5 + 2]];
                num4 = decodingTable[data[num5 + 3]];
                buffer[i] = (byte) ((num << 2) | (num2 >> 4));
                buffer[i + 1] = (byte) ((num2 << 4) | (num3 >> 2));
                buffer[i + 2] = (byte) ((num3 << 6) | num4);
                num5 += 4;
            }
            if (data[data.Length - 2] == '=')
            {
                num = decodingTable[data[data.Length - 4]];
                num2 = decodingTable[data[data.Length - 3]];
                buffer[buffer.Length - 1] = (byte) ((num << 2) | (num2 >> 4));
                return buffer;
            }
            if (data[data.Length - 1] == '=')
            {
                num = decodingTable[data[data.Length - 4]];
                num2 = decodingTable[data[data.Length - 3]];
                num3 = decodingTable[data[data.Length - 2]];
                buffer[buffer.Length - 2] = (byte) ((num << 2) | (num2 >> 4));
                buffer[buffer.Length - 1] = (byte) ((num2 << 4) | (num3 >> 2));
                return buffer;
            }
            num = decodingTable[data[data.Length - 4]];
            num2 = decodingTable[data[data.Length - 3]];
            num3 = decodingTable[data[data.Length - 2]];
            num4 = decodingTable[data[data.Length - 1]];
            buffer[buffer.Length - 3] = (byte) ((num << 2) | (num2 >> 4));
            buffer[buffer.Length - 2] = (byte) ((num2 << 4) | (num3 >> 2));
            buffer[buffer.Length - 1] = (byte) ((num3 << 6) | num4);
            return buffer;
        }

        public static byte[] encode(byte[] data)
        {
            byte[] buffer;
            int num8;
            int num9;
            int num11;
            int num = data.Length % 3;
            if (num == 0)
            {
                buffer = new byte[(4 * data.Length) / 3];
            }
            else
            {
                buffer = new byte[4 * ((data.Length / 3) + 1)];
            }
            int num2 = data.Length - num;
            int index = 0;
            for (int i = 0; index < num2; i += 4)
            {
                int num3 = data[index] & 0xff;
                int num4 = data[index + 1] & 0xff;
                int num5 = data[index + 2] & 0xff;
                buffer[i] = encodingTable[(num3 >> 2) & 0x3f];
                buffer[i + 1] = encodingTable[((num3 << 4) | (num4 >> 4)) & 0x3f];
                buffer[i + 2] = encodingTable[((num4 << 2) | (num5 >> 6)) & 0x3f];
                buffer[i + 3] = encodingTable[num5 & 0x3f];
                index += 3;
            }
            switch (num)
            {
                case 0:
                    return buffer;

                case 1:
                    num11 = data[data.Length - 1] & 0xff;
                    num8 = (num11 >> 2) & 0x3f;
                    num9 = (num11 << 4) & 0x3f;
                    buffer[buffer.Length - 4] = encodingTable[num8];
                    buffer[buffer.Length - 3] = encodingTable[num9];
                    buffer[buffer.Length - 2] = 0x3d;
                    buffer[buffer.Length - 1] = 0x3d;
                    return buffer;

                case 2:
                {
                    num11 = data[data.Length - 2] & 0xff;
                    int num12 = data[data.Length - 1] & 0xff;
                    num8 = (num11 >> 2) & 0x3f;
                    num9 = ((num11 << 4) | (num12 >> 4)) & 0x3f;
                    int num10 = (num12 << 2) & 0x3f;
                    buffer[buffer.Length - 4] = encodingTable[num8];
                    buffer[buffer.Length - 3] = encodingTable[num9];
                    buffer[buffer.Length - 2] = encodingTable[num10];
                    buffer[buffer.Length - 1] = 0x3d;
                    return buffer;
                }
            }
            return buffer;
        }
    }
}

Related Tutorials