namespace com.hitrust.b2b.trustpay.client { using System; using System.Text; public class Base64 { private const int EIGHT_BIT_MASK = 0xff; private const int LOWER_CASE_A_VALUE = 0x1a; private int mIndex = 0; private string mString; private const int PLUS_VALUE = 0x3e; private const int SIX_BIT_MASK = 0x3f; private const int SLASH_VALUE = 0x3f; private const int ZERO_VALUE = 0x34; private int convertUnsignedByteToInt(byte b) { if (b >= 0) { return b; } return (0x100 + b); } public byte[] decode(string data) { this.mString = data; this.mIndex = 0; int num = 0; int length = this.mString.Length; for (int i = 0; i < length; i++) { if (this.isUsefulChar(this.mString[i])) { num++; } } int num4 = (num * 3) / 4; byte[] buffer = new byte[num4]; int num5 = 0; int index = 0; while ((index + 2) < num4) { num5 = this.mapCharToInt(this.NextUsefulChar) << 6; num5 |= this.mapCharToInt(this.NextUsefulChar); num5 = num5 << 6; num5 |= this.mapCharToInt(this.NextUsefulChar); num5 = num5 << 6; num5 |= this.mapCharToInt(this.NextUsefulChar); buffer[index + 2] = (byte) (num5 & 0xff); num5 = num5 >> 8; buffer[index + 1] = (byte) (num5 & 0xff); num5 = num5 >> 8; buffer[index] = (byte) (num5 & 0xff); index += 3; } if (index == (num4 - 1)) { num5 = this.mapCharToInt(this.NextUsefulChar) << 6; num5 |= this.mapCharToInt(this.NextUsefulChar); num5 = num5 >> 4; buffer[index] = (byte) (num5 & 0xff); } if (index == (num4 - 2)) { num5 = this.mapCharToInt(this.NextUsefulChar) << 6; num5 |= this.mapCharToInt(this.NextUsefulChar); num5 = num5 << 6; num5 |= this.mapCharToInt(this.NextUsefulChar); num5 = num5 >> 2; buffer[index + 1] = (byte) (num5 & 0xff); num5 = num5 >> 8; buffer[index] = (byte) (num5 & 0xff); } return buffer; } public string encode(byte[] data) { int num = ((data.Length * 4) / 3) + 4; StringBuilder builder = new StringBuilder((num * 0x4d) / 0x4c); int length = data.Length; int num3 = 0; int num4 = 0; while (num3 < (length - 2)) { num4 = this.convertUnsignedByteToInt(data[num3++]) << 8; num4 |= this.convertUnsignedByteToInt(data[num3++]); num4 = num4 << 8; num4 |= this.convertUnsignedByteToInt(data[num3++]); byte b = (byte) (0x3f & num4); num4 = num4 >> 6; byte num6 = (byte) (0x3f & num4); num4 = num4 >> 6; byte num7 = (byte) (0x3f & num4); num4 = num4 >> 6; byte num8 = (byte) (0x3f & num4); builder.Append(this.mapByteToChar(num8)); builder.Append(this.mapByteToChar(num7)); builder.Append(this.mapByteToChar(num6)); builder.Append(this.mapByteToChar(b)); } if (num3 == (length - 1)) { num4 = this.convertUnsignedByteToInt(data[num3++]) << 4; byte num9 = (byte) (0x3f & num4); num4 = num4 >> 6; byte num10 = (byte) (0x3f & num4); builder.Append(this.mapByteToChar(num10)); builder.Append(this.mapByteToChar(num9)); builder.Append("=="); } if (num3 == (length - 2)) { num4 = this.convertUnsignedByteToInt(data[num3++]) << 8; num4 |= this.convertUnsignedByteToInt(data[num3++]); num4 = num4 << 2; byte num11 = (byte) (0x3f & num4); num4 = num4 >> 6; byte num12 = (byte) (0x3f & num4); num4 = num4 >> 6; byte num13 = (byte) (0x3f & num4); builder.Append(this.mapByteToChar(num13)); builder.Append(this.mapByteToChar(num12)); builder.Append(this.mapByteToChar(num11)); builder.Append("="); } return builder.ToString(); } private bool isUsefulChar(char c) { if ((((c < 'A') || (c > 'Z')) && ((c < 'a') || (c > 'z'))) && (((c < '0') || (c > '9')) && (c != '+'))) { return (c == '/'); } return true; } private char mapByteToChar(byte b) { if (b < 0x1a) { return (char) (0x41 + b); } if (b < 0x34) { return (char) (0x61 + (b - 0x1a)); } if (b < 0x3e) { return (char) (0x30 + (b - 0x34)); } if (b == 0x3e) { return '+'; } if (b != 0x3f) { throw new ArgumentException("Byte " + b + " is not a valid Base64 value"); } return '/'; } private int mapCharToInt(char c) { if ((c >= 'A') && (c <= 'Z')) { return (c - 'A'); } if ((c >= 'a') && (c <= 'z')) { return ((c - 'a') + 0x1a); } if ((c >= '0') && (c <= '9')) { return ((c - '0') + 0x34); } if (c == '+') { return 0x3e; } if (c != '/') { throw new ArgumentException(c + " is not a valid Base64 character."); } return 0x3f; } private char NextUsefulChar { get { char c = '_'; while (!this.isUsefulChar(c)) { c = this.mString[this.mIndex++]; } return c; } } } }