Beginner's Guide to Base64 Encoding and Decoding in C

Get started with Base64 encoding and decoding in C programming using OpenSSL and libb64. Our guide is perfect for beginners in web development.
On this page

Beginner's Guide to Base64 Encoding and Decoding in C

Excerpt

Get started with Base64 encoding and decoding in C programming using OpenSSL and libb64. Our guide is perfect for beginners in web development.

Introduction

In C programming, there are several libraries available for performing Base64 encoding and decoding, such as OpenSSL and libb64. In this guide, we will explore how to use both OpenSSL and libb64 for Base64 encoding and decoding in C programming.

Using OpenSSL for Base64 Encoding and Decoding

Download and Extract OpenSSL Library Files

To get started with OpenSSL, you need to download the library files from the official website. The latest version of OpenSSL can be found at https://www.openssl.org/source/. Once you have downloaded the file, extract it using your preferred archive tool.

Include OpenSSL Header Files in Program

After extracting the OpenSSL library files, you need to include the necessary header files in your program. The header files provide access to the functions and data structures defined by the library.

To include the required header files, add the following lines at the top of your C program:

1#include <openssl/evp.h>

Use EVP_EncodeBlock Function for Encoding

You can use the EVP_EncodeBlock function to encode a string in Base64 format. This function takes three arguments: an output buffer to store the encoded data, a source buffer containing the input data to be encoded, and the length of the input data.

The following code snippet demonstrates how to use EVP_EncodeBlock function:

 1#include <openssl/evp.h>
 2
 3int main()
 4{
 5	unsigned char	input[] = "Hello World!";
 6	unsigned char	output[256];
 7	int		len;
 8	len		= EVP_EncodeBlock( output, input, strlen( input ) );
 9	output[len]	= '\0';
10	printf( "Encoded string: %s\n", output );
11	return(0);
12}

Use EVP_DecodeBlock Function for Decoding

To decode a Base64-encoded string using OpenSSL, you can use EVP_DecodeBlock function. This function takes three arguments: an output buffer to store decoded data, a source buffer containing encoded data to be decoded, and length of encoded data.

Here’s an example code snippet demonstrating how to use EVP_DecodeBlock function:

 1#include <openssl/evp.h>
 2
 3int main()
 4{
 5unsigned char input[] = "SGVsbG8gV29ybGQh";
 6unsigned char output[256];
 7int len;
 8
 9len = EVP_DecodeBlock(output, input, strlen(input));
10output[len] = '\0';
11
12printf("Decoded string: %s\n", output);
13
14return 0;
15}

Using libb64 for Base64 Encoding and Decoding

If you are looking for a lightweight and simple library to perform Base64 encoding and decoding in C programming, libb64 is an excellent option. It provides functions specifically designed for this purpose.

Download and Extract libb64 Library Files

The first step in using libb64 is to download the library files from the official website.

Include b64 Header File in Program

To include the b64.h header file in your program, simply add the following line of code at the beginning of your source code:

1#include "b64.h"

Use b64_encode Function for Encoding

You can use the b64_encode function to encode a string in Base64 format. The function takes three parameters: a pointer to the input data, the length of the input data (in bytes), and a pointer to an output buffer where the encoded data will be stored.

Here is an example code snippet that demonstrates how to use the b64_encode function:

 1char* input_string = "Hello World!";
 2int input_length = strlen(input_string);
 3// Allocate memory for output buffer
 4int output_length = (4 * ((input_length + 2) / 3)) + 1;
 5char* output_buffer = (char*)malloc(output_length);
 6// Encode input string
 7b64_encode((const unsigned char*)input_string, input_length,
 8(unsigned char*)output_buffer);
 9// Print encoded string
10printf("Encoded string: %s\n", output_buffer);
11// Free memory
12free(output_buffer);

The free Base64 Encode verification tool is as follows:

Use b64_decode Function for Decoding

To decode a Base64-encoded string using libb46, you can use the b46_decode() function. This function takes three parameters: a pointer to the encoded data, its length (in bytes), and a pointer to an output buffer where decoded data will be stored.

Here’s how you can use this function:

 1char* encoded_string = "SGVsbG8gV29ybGQh";
 2int encoded_length = strlen(encoded_string);
 3// Allocate memory for decoded buffer
 4int decoded_length = (encoded_length / 4) * 3;
 5char* decoded_buffer = (char*)malloc(decoded_length);
 6// Decode encoded string
 7b46_decode((const unsigned char*)encoded_string,
 8encoded_length,
 9(unsigned char*)decoded_buffer);
10// Print decoded string
11printf("Decoded string: %s\n", decoded_buffer);
12// Free memory
13free(decoded_buffer);

The free Base64 Decode verification tool is as follows:

How to choose between OpenSSL and libb64?

OpenSSL is a reliable cryptographic library with built-in functions for Base64 encoding and decoding. Its extensive documentation and community support make it an excellent choice for those who require additional cryptographic functions. On the other hand, libb64 is a lightweight library that provides simple functions for Base64 encoding and decoding without the need for additional cryptographic functions. It is an excellent option for those who prefer smaller libraries or have limited resources on their system.