Last active
September 25, 2015 09:57
-
-
Save ikcaro/903063 to your computer and use it in GitHub Desktop.
codificador en base 64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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