Search code examples
c++ibm-midrangegsoap

Why Debian and AS400 are giving different base64 encoded strings?


I'm using gSOAP to use a service and I'm having the basic authentication string encoded in two different string, one from linux 32 bits and another one from AS400. The code is the same. I guess this could be the AS400 using EBCDIC, but I'm converting it to ASCII and it gives the same result. Has someone had the same problem or similar?

This is the Linux encoded string:

c2FudGFuZGVyY29uc3VtZXI6Z2Vyc29hMg==

This is the AS400 encoded string:

ooGVo4GVhIWZg5aVoqSUhZl6h4WZopaB8g==

This is the encoding code:

if (!t)
    t = (char*)soap_malloc(soap, output_length /*(n + 2) / 3 *
                                                * 4 + 1 */);
if (!t)
    return NULL;

p = t;

for (int i = 0, j = 0; i < input_length;) {

    uint32_t octet_a = i < input_length ? (unsigned char)s[i++] : 0;
    uint32_t octet_b = i < input_length ? (unsigned char)s[i++] : 0;
    uint32_t octet_c = i < input_length ? (unsigned char)s[i++] : 0;

    uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

    t[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
    t[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
    t[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
    t[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}

for (int i = 0; i < mod_table[input_length % 3]; i++)
    t[output_length - 1 - i] = '=';

t[output_length - mod_table[input_length % 3] + 2] = '\0';

The code from the Linux system is working fine. From AS400 isn't working. I guess it is some AS400 encoding problem, but I'm not sure and I have limited access to the AS400 system, so I can't trace a lot.


Solution

  • It is because you are converting a byte sequence which represents text encoded in EBCDIC.

    The first string is the following bytes:

    115,97,110,116,97,110,100,101,114,99,111,110,115,117,109,101,114,58,103,101,114,115,111,97,50
    

    Which decodes in ASCII as santanderconsumer:gersoa2. You are going to have to change that password now, by the way.

    The second Base64 string is the following bytes:

    162,129,149,163,129,149,132,133,153,131,150,149,162,164,148,133,153,122,135,133,153,162,150,129,242
    

    Checking the EBCDIC table at https://en.wikipedia.org/wiki/EBCDIC we can see that this is the same string.