Question

I have a block of c++ code which is not behaving as expected and I am unable to figure out why

I have two encrypted keys (Both 512 bytes long) encoded in base64.

First key decodes successfully and is able to be decrypted. Second key only decodes about half and therefore is invalid, yet decoder returns 512 as decoded length

main.cpp:

#include <openssl/bio.h>
#include <openssl/evp.h>

int base64Decode(const char *encoded_bytes, unsigned char **decoded_bytes)
{
    BIO *b64, *bioMem;
    size_t buffer_length, decoded_length;

    bioMem = BIO_new_mem_buf((void *)encoded_bytes, -1);
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bioMem = BIO_push(b64, bioMem);

    buffer_length = BIO_get_mem_data(bioMem, NULL);
    *decoded_bytes = (unsigned char*)malloc(buffer_length);
    decoded_length = BIO_read(bioMem, *decoded_bytes, buffer_length);

    // Here to see what is actually getting decoded
    printf("%s\n", *decoded_bytes);

    BIO_free_all(bioMem);

    return decoded_length;
}

int main(){
    unsigned char *binaryBuffer = (unsigned char*)malloc(512);

    // Test if "YOYO!" gets correctly decoded (Which it seems to)
    printf("Length is: %d\n\n", base64Decode("WU9ZTyEA", &binaryBuffer));

    // Key #1 - Decodes successfully - 512 returned
    printf("Length is: %d\n\n", base64Decode("va+Kr/EHdeIwdgzymP40nnyIoFqpb6Y0sWAgrpz8B1DQx+9flmJyDF7ZeaQSnIBRdB17alzf5HmNga9kmAWLJvAeBSBexTrZvPSuA0Ug4cvI+hPAtPecWjm4s1s+3NmWuWI3Wv2trkv/uQNz2WzFaXPz4wT4gcl+zEYf1wie5VO2hzF0xSgmSR+AnF1gx6C4ncAKBnnm9K/sQ8Ldf+SyE5OJM9znczVRZRB6s+3zG3UPC17Atnyvh2pBxRCPoRrV4DDP+DcaijIr+KbCgp1G56r2faT9d0nfbAKarnNAhBXOH4QODW/hhirHSj2OKqtYSNmOJXja3R9d625EZcfcH/pU6bXB+RvupLG9Kkj1WF4bD8v5+RdqsRxlJVEE0I9o2VRG+kIOqDg8FLe2lDoefCXnpLgXh15j9xTh7zHXTAq0diTskfneo5KehwoveY7/TrxOXg3VOazj3TaiLmB1YDfZwPD2MwfQ9VyP5uAd2NB8qcXDSDVJcD7QPeF8hcPFu7wX5GsNXNOFdIFnzaaKfdiT5uz2m3QnORTFTloi2IM2ItYRsm2GErD6cyJTf1qnSPJo8r5lx6A1UKDvyF8kwWoC3UnzWA6djzi3fi9QGVO6agytUDNX8IiHpxU2V6EK0CRl3tRpogMhSNyr2F78IazNECStTt5MO0AyOMpkQt0=", &binaryBuffer));

    // Key #2 - Only decodes about half, yet returns 512
    printf("Length is: %d\n\n", base64Decode("U4tR8mRzyrqpNhhFjkbEe4I6LTYXhL7PxkbddNTQ1yZ6ofZ4s1R/UOQsq6x+CNxB+yddPirwT0yPgtm6IC1qYF9GGQsOqXHkpTrmXf0GiXDVpm91EMnyxtMu74B3OMIYgxmjoeua7HoKQkW6/GRuCpgWIoZQq7uOaKIsc3k9HGgfAFk6vTGER1YJlG28lOhsiGccl0EqD0uhrBGNhFERfAzB2gaJjI1oRO87Q2NbevKHeZycpyXgazvtw9JigA+Hp3+Cy9LUIRvF6k5uv0DKxOs5cynqYslb1LfKqT0IvLjBl4gNHl+pG5/Ur70XzZTiO1+n5jWITPoslZ4slVkl4qiTaqNWHgLT6aSUhWwPlvK+7wlk+st5ykAuSIE2e3Lia+omBRH2LQfG1v7KaOJApF3k4D0li/4QWOJ3zLwBDHB6WCwMQfNS8vTRWM1yIO/o9417wJEpBlcr/B308vGheoTF9+qRKGDe0M5PNHeBbEHhgNkLsKvcS/31HK6Xd36cg85yvyLghQRr9Gyn7TUU5m6f6iSlx3u+yo1vT7BBV6OjbxPklwCIYCZWIIOJq10JXC+bSGPbTKZYXjQW90URKesUOMi9s+DS7BKVEr471AnEyazividrgivfHDNWQisIcOctpDFCfEBAa28PYjIj4KJo5bDkSluRVcVDJVrP2Ns=", &binaryBuffer));

}

I know that both 64 encoded strings are valid as when I pipe them to the base64 -d command, they spit out the encrypted key in its entirety (Also same if I use Base64.decode64() in ruby)

Compiled with:

g++ -o main.o -c main.cpp
g++ -o test main.o -lcrypto

Cheers

Was it helpful?

Solution

You verify by printing out the key as a zero-terminated string. This may work for the first key, since it does not have an embedded zero char, but the second key has an embedded zero char at position 0x81 (129, meaning the 130th character) and will thus terminate output early.

Be aware that your code is also leaking memory!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top