Пример #1
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);
                }