Herewith is an example of encoding to and from base64 using OpenSSL's C library. Code presented here is both binary safe, and portable (i.e. it should work on any Posix compliant system e.g. FreeBSD and Linux).
-
-
Save ysoyipek/c40e00d11608764bbe5b09baf9b235a9 to your computer and use it in GitHub Desktop.
OpenSSL Base64 En/Decode: Portable and binary safe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Decodes Base64 | |
| #include <openssl/bio.h> | |
| #include <openssl/evp.h> | |
| #include <stdint.h> | |
| #include <assert.h> | |
| size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded string | |
| size_t len = strlen(b64input), | |
| padding = 0; | |
| if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are = | |
| padding = 2; | |
| else if (b64input[len-1] == '=') //last char is = | |
| padding = 1; | |
| return (len*3)/4 - padding; | |
| } | |
| int Base64Decode(char* b64message, uint8_t** buffer, size_t* length) { //Decodes a base64 encoded string | |
| BIO *bio, *b64; | |
| int decodeLen = calcDecodeLength(b64message); | |
| *buffer = (uint8_t*)malloc(decodeLen); | |
| bio = BIO_new_mem_buf(b64message, -1); | |
| b64 = BIO_new(BIO_f_base64()); | |
| bio = BIO_push(b64, bio); | |
| BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer | |
| *length = BIO_read(bio, *buffer, strlen(b64message)); | |
| assert(*length == decodeLen); //length should equal decodeLen, else something went horribly wrong | |
| BIO_free_all(bio); | |
| return (0); //success | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Encodes Base64 | |
| #include <openssl/bio.h> | |
| #include <openssl/evp.h> | |
| #include <openssl/buffer.h> | |
| #include <stdint.h> | |
| int Base64Encode(const uint8_t* buffer, size_t length, char** b64text) { //Encodes a binary safe base 64 string | |
| BIO *bio, *b64; | |
| BUF_MEM *bufferPtr; | |
| b64 = BIO_new(BIO_f_base64()); | |
| bio = BIO_new(BIO_s_mem()); | |
| bio = BIO_push(b64, bio); | |
| BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line | |
| BIO_write(bio, buffer, length); | |
| BIO_flush(bio); | |
| BIO_get_mem_ptr(bio, &bufferPtr); | |
| BIO_set_close(bio, BIO_NOCLOSE); | |
| BIO_free_all(bio); | |
| *b64text=(*bufferPtr).data; | |
| return (0); //success | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main() { | |
| //Encode To Base64 | |
| char* base64EncodeOutput, *text="Hello World"; | |
| Base64Encode(text, strlen(text), &base64EncodeOutput); | |
| printf("Output (base64): %s\n", base64EncodeOutput); | |
| //Decode From Base64 | |
| char* base64DecodeOutput; | |
| size_t test; | |
| Base64Decode("SGVsbG8gV29ybGQ=", &base64DecodeOutput, &test); | |
| printf("Output: %s %d\n", base64DecodeOutput, test); | |
| return(0); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| all: | |
| gcc -o base64 Main.c Base64Encode.c Base64Decode.c -lcrypto -lm -w |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment