31static char base46_map[] = {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
32 'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
33 'Z',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
34 'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
35 '3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
40 unsigned char counts = 0;
42 char* cipher = malloc(strlen(plain) * 4 / 3 + 4);
46 perror(
"base64_encode");
50 for(i = 0; plain[i] !=
'\0'; i++) {
51 buffer[counts++] = plain[i];
53 cipher[c++] = base46_map[
buffer[0] >> 2];
54 cipher[c++] = base46_map[((
buffer[0] & 0x03) << 4) + (
buffer[1] >> 4)];
55 cipher[c++] = base46_map[((
buffer[1] & 0x0f) << 2) + (
buffer[2] >> 6)];
56 cipher[c++] = base46_map[
buffer[2] & 0x3f];
62 cipher[c++] = base46_map[
buffer[0] >> 2];
64 cipher[c++] = base46_map[(
buffer[0] & 0x03) << 4];
67 cipher[c++] = base46_map[((
buffer[0] & 0x03) << 4) + (
buffer[1] >> 4)];
68 cipher[c++] = base46_map[(
buffer[1] & 0x0f) << 2];
79 unsigned char counts = 0;
81 char* plain = malloc(strlen(cipher) * 3 / 4 + 1);
85 perror(
"base64_decode");
89 for(i = 0; cipher[i] !=
'\0'; i++) {
91 for(k = 0 ; k < 64 && base46_map[k] != cipher[i]; k++);
char * base64_encode(char *plain)
Encode a zero terminated C string in Base64.
char * base64_decode(char *cipher)
Decode a zero terminated C Base64 encoded string.
Simple Base64 encoding and decoding functions.