Skip to content

Instantly share code, notes, and snippets.

@ikcaro
Last active September 25, 2015 09:57
Show Gist options
  • Save ikcaro/903063 to your computer and use it in GitHub Desktop.
Save ikcaro/903063 to your computer and use it in GitHub Desktop.
codificador en base 64
/*
* b64
* Angel Rodolfo Pérez Canseco (ikcaro)
* Algoritmo para codificar datos en base 64
*/
#include <stdio.h>
#include <string.h>
#define TBASE 64
static char base_64[TBASE] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void encodeB64(const unsigned char *m, char *c, int s)
{
int d = 3 - (s % 3);
int j = 0;
int i = 0;
int t = 0;
while (j < s) {
t = (m[j++] << 0x10);
t += (j < s)? (m[j++] << 0x08): 0;
t += (j < s)? (m[j++] << 0x00): 0;
c[i++] = base_64[(t >> 0x12) & 0x3F];
c[i++] = base_64[(t >> 0x0C) & 0x3F];
c[i++] = base_64[(t >> 0x06) & 0x3F];
c[i++] = base_64[(t >> 0x00) & 0x3F];
}
for (j = d; (d < 3) && (j > 0); j--)
c[i - j] = '=';
c[i] = 0;
}
int main()
{
char str[] = "any carnal pleasure.";
char code[1000];
encodeB64(str, code, strlen(str));
printf("[%s] => [%s]\n", str, code);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment