Skip to content

Bad / Missing ending on decoded content #16

@morgan-wild

Description

@morgan-wild

Hi! I'm trying to use your library but I have a problem, probably i use it the bad way.

I encode and decode the same string or array of bytes but I can't obtain the same result.

    char m[] = "'I FINK U FREEKY' by DIE ANTWOORD xxx (Official)";

    Serial.println(m);

    char mBase64[128];

    int base64Length = Base64.encode(mBase64, (char *)m, sizeof(m));

    Serial.println(mBase64);

    int dLength = Base64.decodedLength(mBase64, strlen(mBase64)); 

    char m2[dLength];

    Base64.decode(m2, mBase64, dLength);

    Serial.println(m2);

Result

'I FINK U FREEKY' by DIE ANTWOORD xxx (Official)
J0kgRklOSyBVIEZSRUVLWScgYnkgRElFIEFOVFdPT1JEIHh4eCAoT2ZmaWNpYWwpAA==
'I FINK U FREEKY' by DIE ANTWOORD xx <- Humm?
    char m[] = { 0x57, 0x2a, 0x63, 0x79, 0x3a, 0x9f, 0x76, 0x60, 0x66, 0xcf, 0x50, 0x59, 0x1c, 0x8f, 0x97, 0x49, 0x92, 0xba, 0x71, 0x14, 0x27, 0xb5, 0x2c, 0x6c, 0xf1, 0xa4, 0x7a, 0x4b, 0xc8, 0x13, 0xe6, 0x93 };

    Serial.println(m);

    for (uint8_t i = 0; i < sizeof(m); i++)
    {
        Serial.print(m[i], HEX);
        Serial.print(" ");
    }

    Serial.println();

    char mBase64[128];

    int base64Length = Base64.encode(mBase64, (char *)m, sizeof(m));

    Serial.println(mBase64);

    int dLength = Base64.decodedLength(mBase64, strlen(mBase64)); 

    char m2[dLength];

    Base64.decode(m2, mBase64, dLength);

    for (uint8_t i = 0; i < sizeof(m2); i++)
    {
        Serial.print(m2[i], HEX);
        Serial.print(" ");
    }

Result

57 2A 63 79 3A 9F 76 60 66 CF 50 59 1C 8F 97 49 92 BA 71 14 27 B5 2C 6C F1 A4 7A 4B C8 13 E6 93 
VypjeTqfdmBmz1BZHI+XSZK6cRQntSxs8aR6S8gT5pM=
57 2A 63 79 3A 9F 76 60 66 CF 50 59 1C 8F 97 49 92 BA 71 14 27 B5 2C 6C 0 A5 A5 A5 A5 A5 A5 A5  <- Humm?

Same exercice using another library (densaugeo/base64@^1.4.0):

    char m[] = "'I FINK U FREEKY' by DIE ANTWOORD xxx (Official)";

    Serial.println(m);

    char mBase64[128];

    int base64_length = encode_base64((unsigned char *)m, sizeof(m), (unsigned char *)mBase64);

    Serial.println(mBase64);

    char m2[128];

    int decoded_length = decode_base64((unsigned char *)mBase64, (unsigned char *) m2);

    Serial.println(m2);

Result

'I FINK U FREEKY' by DIE ANTWOORD xxx (Official)
J0kgRklOSyBVIEZSRUVLWScgYnkgRElFIEFOVFdPT1JEIHh4eCAoT2ZmaWNpYWwpAA==
'I FINK U FREEKY' by DIE ANTWOORD xxx (Official) <- OK!
    char m[] = { 0x57, 0x2a, 0x63, 0x79, 0x3a, 0x9f, 0x76, 0x60, 0x66, 0xcf, 0x50, 0x59, 0x1c, 0x8f, 0x97, 0x49, 0x92, 0xba, 0x71, 0x14, 0x27, 0xb5, 0x2c, 0x6c, 0xf1, 0xa4, 0x7a, 0x4b, 0xc8, 0x13, 0xe6, 0x93 };

    for (uint8_t i = 0; i < sizeof(m); i++)
    {
        Serial.print(m[i], HEX);
        Serial.print(" ");
    }

    Serial.println();

    char mBase64[128];

    int base64_length = encode_base64((unsigned char *)m, sizeof(m), (unsigned char *)mBase64);

    Serial.println(mBase64);

    char m2[128];

    int decoded_length = decode_base64((unsigned char *)mBase64, (unsigned char *) m2);

    for (uint8_t i = 0; i < decoded_length; i++)
    {
        Serial.print(m2[i], HEX);
        Serial.print(" ");
    }

Result

57 2A 63 79 3A 9F 76 60 66 CF 50 59 1C 8F 97 49 92 BA 71 14 27 B5 2C 6C F1 A4 7A 4B C8 13 E6 93
VypjeTqfdmBmz1BZHI+XSZK6cRQntSxs8aR6S8gT5pM=
57 2A 63 79 3A 9F 76 60 66 CF 50 59 1C 8F 97 49 92 BA 71 14 27 B5 2C 6C F1 A4 7A 4B C8 13 E6 93 <- OK!

The encoding part looks fine, I have the same result with both libraries and I checked it with an online tool.

But there is something bad on the decoding side.

Any idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions