Example #1
0
                public byte[] Decode()
                {
                    byte[] buffer2 = new byte[length2];
                    for (int i = 0; i < length; i++)
                    {
                        source[i] = (char)Base64Encoding.CharToSixbit(source[i]);
                    }

                    byte b, b1, b2, b3;
                    byte temp1, temp2, temp3, temp4;

                    for (int i = 0; i < blockCount; i++)
                    {
                        temp1 = (byte)source[i * 4];
                        temp2 = (byte)source[i * 4 + 1];
                        temp3 = (byte)source[i * 4 + 2];
                        temp4 = (byte)source[i * 4 + 3];

                        b   = (byte)(temp1 << 2);
                        b1  = (byte)((temp2 & 48) >> 4);
                        b1 += b;

                        b   = (byte)((temp2 & 15) << 4);
                        b2  = (byte)((temp3 & 60) >> 2);
                        b2 += b;

                        b   = (byte)((temp3 & 3) << 6);
                        b3  = temp4;
                        b3 += b;

                        buffer2[i * 3]     = b1;
                        buffer2[i * 3 + 1] = b2;
                        buffer2[i * 3 + 2] = b3;
                    }

                    byte[] result = null;

                    if (paddingCount > 0)
                    {
                        length3 = length2 - paddingCount;
                        result  = new byte[length3];

                        for (int i = 0; i < length3; i++)
                        {
                            result[i] = buffer2[i];
                        }
                    }
                    else
                    {
                        result = buffer2;
                    }

                    return(result);
                }
Example #2
0
                private void InitEncoder(byte[] input)
                {
                    Base64Encoding.BuidLookupTableDictionnary();
                    source = input;
                    length = input.Length;

                    if ((length % 3) == 0)
                    {
                        paddingCount = 0;
                        blockCount   = length / 3;
                    }
                    else
                    {
                        paddingCount = 3 - (length % 3);//need to add padding
                        blockCount   = (length + paddingCount) / 3;
                    }
                    length2 = length + paddingCount;//or blockCount *3
                }
Example #3
0
                private void InitDecoder(char[] input)
                {
                    Base64Encoding.BuidLookupTableDictionnary();
                    input = Base64Encoding.DeleteUnvalidChars(input);

                    int temp = 0;

                    source = input;
                    length = input.Length;

                    for (int i = 0; i < 2; i++)
                    {
                        if (input[length - i - 1] == '=')
                        {
                            temp++;
                        }
                    }
                    paddingCount = temp;
                    blockCount   = length / 4;
                    length2      = blockCount * 3;
                }
Example #4
0
                public char[] Encode()
                {
                    int i;

                    byte[] sourceBuffer;

                    if (length != length2)
                    {
                        sourceBuffer = new byte[length2];
                        for (i = 0; i < length2; i++)
                        {
                            if (i < length)
                            {
                                sourceBuffer[i] = source[i];
                            }
                            else
                            {
                                sourceBuffer[i] = 0;
                            }
                        }
                    }
                    else
                    {
                        sourceBuffer = source;
                    }

                    byte b1, b2, b3;
                    byte temp, temp1, temp2, temp3, temp4;

                    char[] result = new char[blockCount * 4];

                    for (i = 0; i < blockCount; i++)
                    {
                        b1 = sourceBuffer[i * 3];
                        b2 = sourceBuffer[i * 3 + 1];
                        b3 = sourceBuffer[i * 3 + 2];

                        temp1  = (byte)((b1 & 252) >> 2);
                        temp   = (byte)((b1 & 3) << 4);
                        temp2  = (byte)((b2 & 240) >> 4);
                        temp2 += temp;
                        temp   = (byte)((b2 & 15) << 2);
                        temp3  = (byte)((b3 & 192) >> 6);
                        temp3 += temp;
                        temp4  = (byte)(b3 & 63);

                        result[i * 4]     = Base64Encoding.SixbitToChar(temp1);
                        result[i * 4 + 1] = Base64Encoding.SixbitToChar(temp2);
                        result[i * 4 + 2] = Base64Encoding.SixbitToChar(temp3);
                        result[i * 4 + 3] = Base64Encoding.SixbitToChar(temp4);
                    }

                    //covert last "A"s to "=", based on paddingCount
                    switch (paddingCount)
                    {
                    case 0:
                        break;

                    case 1:
                        result[blockCount * 4 - 1] = '=';
                        break;

                    case 2:
                        result[blockCount * 4 - 1] = '=';
                        result[blockCount * 4 - 2] = '=';
                        break;

                    default:
                        break;
                    }
                    return(result);
                }
Example #5
0
 public MemoryBase64Encoder(string input, Encoding v)
 {
     byte[] inputBytes = v.GetBytes(Base64Encoding.DeleteUnvalidChars(input.ToCharArray()));
     this.InitEncoder(inputBytes);
 }