This C program demonstrates base64 encoding and decoding. It uses the OpenSSL library to perform the base64 operations.
Here’s a breakdown of the code:
We include necessary headers, including OpenSSL headers for base64 operations.
We define two helper functions:
base64_encode: Encodes a string to base64.
base64_decode: Decodes a base64 string.
In the main function:
We define our input string.
We perform standard base64 encoding and decoding.
We demonstrate URL-safe base64 encoding by manually replacing ‘+’ with ‘-’ and ‘/’ with ‘_’.
We demonstrate URL-safe base64 decoding by reversing the character replacements and then decoding.
We print the results of each operation.
Finally, we free the allocated memory.
Note that C doesn’t have built-in support for base64 encoding/decoding, so we’re using the OpenSSL library. Also, C doesn’t have a built-in URL-safe base64 encoder, so we’re manually adjusting the standard encoded string for demonstration purposes.
To compile and run this program, you’ll need to have OpenSSL installed and link against it:
The output shows that both standard and URL-safe base64 encoding/decoding work as expected, producing the original string after decoding.