Skip to content

Instantly share code, notes, and snippets.

@hfeeki
Forked from barrysteyn/Base64Decode.c
Created February 18, 2014 03:35
Show Gist options
  • Save hfeeki/9064242 to your computer and use it in GitHub Desktop.
Save hfeeki/9064242 to your computer and use it in GitHub Desktop.
//Decodes Base64
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <string.h>
#include <stdio.h>
int calcDecodeLength(const char* b64input) { //Calculates the length of a decoded base64 string
int len = strlen(b64input);
int 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 (int)len*0.75 - padding;
}
int Base64Decode(char* b64message, char** buffer) { //Decodes a base64 encoded string
BIO *bio, *b64;
int decodeLen = calcDecodeLength(b64message),
len = 0;
*buffer = (char*)malloc(decodeLen+1);
FILE* stream = fmemopen(b64message, strlen(b64message), "r");
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
len = BIO_read(bio, *buffer, strlen(b64message));
//Can test here if len == decodeLen - if not, then return an error
(*buffer)[len] = '\0';
BIO_free_all(bio);
fclose(stream);
return (0); //success
}
//Encodes Base64
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
int Base64Encode(const char* message, char** buffer) { //Encodes a string to base64
BIO *bio, *b64;
FILE* stream;
int encodedSize = 4*ceil((double)strlen(message)/3);
*buffer = (char *)malloc(encodedSize+1);
stream = fmemopen(*buffer, encodedSize+1, "w");
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line
BIO_write(bio, message, strlen(message));
BIO_flush(bio);
BIO_free_all(bio);
fclose(stream);
return (0); //success
}
#include <stdio.h>
int main() {
//Encode To Base64
char* base64EncodeOutput;
Base64Encode("Hello World", &base64EncodeOutput);
printf("Output (base64): %s\n", base64EncodeOutput);
//Decode From Base64
char* base64DecodeOutput;
Base64Decode("SGVsbG8gV29ybGQ=", &base64DecodeOutput);
printf("Output: %s\n", base64DecodeOutput);
return(0);
}
all:
gcc -o base64 Main.c Base64Encode.c Base64Decode.c -lcrypto -lm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment