Cryptography Reference
In-Depth Information
6
9
C
3
D
7
011010011100001111010111
0
11010
011100001111000111
26
28
15
7
ABC...
G
H
I
...
O
P
Q
...
3
4
5
6
...
Figure 1-3: Base64 Encoding
As you see in Listing 1-14, Base64 encoding is pretty simple to implement
after you understand it; most of the complexity deals with non-aligned input:
Listing 1-14: “base64.c” base64_encode
static char *base64 =
“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
void base64_encode( const unsigned char *input, int len, unsigned char *output )
{
do
{
*output++ = base64[ ( input[ 0 ] & 0xFC ) >> 2 ];
if ( len == 1 )
{
*output++ = base64[ ( ( input[ 0 ] & 0x03 ) << 4 ) ];
*output++ = '=';
*output++ = '=';
break;
}
*output++ = base64[
( ( input[ 0 ] & 0x03 ) << 4 ) | ( ( input[ 1 ] & 0xF0 ) >> 4 ) ];
if ( len == 2 )
{
*output++ = base64[ ( ( input[ 1 ] & 0x0F ) << 2 ) ];
*output++ = '=';
break;
}
*output++ = base64[
 
Search WWH ::




Custom Search