neelkanth_surekha#cat Main.c
#include <stdio.h>#include <string.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.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, unsigned char** buffer, size_t* length) { //Decodes a base64 encoded string
BIO *bio, *b64;
int decodeLen = calcDecodeLength(b64message);
*buffer = (unsigned char*)malloc(decodeLen + 1);
(*buffer)[decodeLen] = '\0';
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
}
int Base64Encode(const unsigned char* 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);
strcpy(b64text ,(*bufferPtr).data) ;
return (0); //success
}
int main(int argc, char *argv[]) {
//Encode To Base64
char text[100] = {"0"};
if (argc != 2) {
printf("Enter a string as argument\n");
exit(1);
}
strncpy(text, argv[1], sizeof(text));
char base64EncodeOutput[100] = {0};
printf("Input string: %s\n", text);
Base64Encode(text, strlen(text), base64EncodeOutput);
printf("encoded Output (base64): %s length: %ld\n",
base64EncodeOutput, strlen(base64EncodeOutput));
//Decode From Base64
char* base64DecodeOutput;
size_t test;
Base64Decode(base64EncodeOutput, &base64DecodeOutput, &test);
printf("Decoded Output: %s string length: %d\n", base64DecodeOutput, test);
return(0);
}
neelkanth_surekha#cat makefile
all:
gcc -o base64 Main.c -lcrypto -lm -w