개발 노트
[C/C++] base64 인코딩과 디코딩 구현
무병장수권력자
2008. 12. 18. 17:12
작성자 : 김문규
최초 작성일 : 2008.12.18
인터넷을 뒤지다보니 좋은 소스를 발견했고, 그 소스를 이용하는 방법을 간단하게 소개합니다.
base64 인코딩의 원리에 대해서 알고 싶으시다면 아래 링크를 클릭하세요~
http://www.iamcorean.net/130
라이브러리 출처 : http://wiki.kldp.org/wiki.php/CSocketFAQ
int _tmain(int argc, _TCHAR* argv[])
{
char* plain_text = "테스트Test`1234567890-=\[];',./";
char* b64encoded_text = NULL;
최초 작성일 : 2008.12.18
인터넷을 뒤지다보니 좋은 소스를 발견했고, 그 소스를 이용하는 방법을 간단하게 소개합니다.
base64 인코딩의 원리에 대해서 알고 싶으시다면 아래 링크를 클릭하세요~
http://www.iamcorean.net/130
라이브러리 출처 : http://wiki.kldp.org/wiki.php/CSocketFAQ
#include <stdafx.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
/*------ Base64 Encoding Table ------*/
static const char MimeBase64[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
static const char MimeBase64[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
/*------ Base64 Decoding Table ------*/
static int DecodeMimeBase64[256] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
};
static int DecodeMimeBase64[256] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
};
int base64_decode(char *text, unsigned char *dst, int numBytes )
{
const char* cp;
int space_idx = 0, phase;
int d, prev_d = 0;
unsigned char c;
{
const char* cp;
int space_idx = 0, phase;
int d, prev_d = 0;
unsigned char c;
space_idx = 0;
phase = 0;
phase = 0;
for ( cp = text; *cp != '\0'; ++cp ) {
d = DecodeMimeBase64[(int) *cp];
if ( d != -1 ) {
switch ( phase ) {
case 0:
++phase;
break;
case 1:
c = ( ( prev_d << 2 ) | ( ( d & 0x30 ) >> 4 ) );
if ( space_idx < numBytes )
dst[space_idx++] = c;
++phase;
break;
case 2:
c = ( ( ( prev_d & 0xf ) << 4 ) | ( ( d & 0x3c ) >> 2 ) );
if ( space_idx < numBytes )
dst[space_idx++] = c;
++phase;
break;
case 3:
c = ( ( ( prev_d & 0x03 ) << 6 ) | d );
if ( space_idx < numBytes )
dst[space_idx++] = c;
phase = 0;
break;
}
prev_d = d;
}
}
d = DecodeMimeBase64[(int) *cp];
if ( d != -1 ) {
switch ( phase ) {
case 0:
++phase;
break;
case 1:
c = ( ( prev_d << 2 ) | ( ( d & 0x30 ) >> 4 ) );
if ( space_idx < numBytes )
dst[space_idx++] = c;
++phase;
break;
case 2:
c = ( ( ( prev_d & 0xf ) << 4 ) | ( ( d & 0x3c ) >> 2 ) );
if ( space_idx < numBytes )
dst[space_idx++] = c;
++phase;
break;
case 3:
c = ( ( ( prev_d & 0x03 ) << 6 ) | d );
if ( space_idx < numBytes )
dst[space_idx++] = c;
phase = 0;
break;
}
prev_d = d;
}
}
return space_idx;
}
int base64_encode(char *text, int numBytes, char **encodedText)
{
unsigned char input[3] = {0,0,0};
unsigned char output[4] = {0,0,0,0};
int index, i, j, size;
char *p, *plen;
{
unsigned char input[3] = {0,0,0};
unsigned char output[4] = {0,0,0,0};
int index, i, j, size;
char *p, *plen;
plen = text + numBytes - 1;
size = (4 * (numBytes / 3)) + (numBytes % 3? 4 : 0) + 1;
(*encodedText) = (char*)malloc(size);
j = 0;
size = (4 * (numBytes / 3)) + (numBytes % 3? 4 : 0) + 1;
(*encodedText) = (char*)malloc(size);
j = 0;
for (i = 0, p = text;p <= plen; i++, p++) {
index = i % 3;
input[index] = *p;
index = i % 3;
input[index] = *p;
if (index == 2 || p == plen) {
output[0] = ((input[0] & 0xFC) >> 2);
output[1] = ((input[0] & 0x3) << 4) | ((input[1] & 0xF0) >> 4);
output[2] = ((input[1] & 0xF) << 2) | ((input[2] & 0xC0) >> 6);
output[3] = (input[2] & 0x3F);
output[0] = ((input[0] & 0xFC) >> 2);
output[1] = ((input[0] & 0x3) << 4) | ((input[1] & 0xF0) >> 4);
output[2] = ((input[1] & 0xF) << 2) | ((input[2] & 0xC0) >> 6);
output[3] = (input[2] & 0x3F);
(*encodedText)[j++] = MimeBase64[output[0]];
(*encodedText)[j++] = MimeBase64[output[1]];
(*encodedText)[j++] = index == 0? '=' : MimeBase64[output[2]];
(*encodedText)[j++] = index < 2? '=' : MimeBase64[output[3]];
(*encodedText)[j++] = MimeBase64[output[1]];
(*encodedText)[j++] = index == 0? '=' : MimeBase64[output[2]];
(*encodedText)[j++] = index < 2? '=' : MimeBase64[output[3]];
input[0] = input[1] = input[2] = 0;
}
}
}
}
(*encodedText)[j] = '\0';
return size;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char* plain_text = "테스트Test`1234567890-=\[];',./";
char* b64encoded_text = NULL;
printf("plain text\n");
printf("%s\n\n", plain_text);
printf("%s\n\n", plain_text);
// base 64 인코딩
// base64_encode(평문을 가리키는 포인터, 평문 길이, b64 인코딩 결과문을 가리키는 포인터)
// 3번째 인자는 이중 포인터임에 유의해야하며, base64_encode 함수 내부에서 메모리가 할당되므로, 외부에서 꼭 해제 시켜야 한다.
int len_b64_encoded = base64_encode(plain_text, strlen(plain_text)+1, &b64encoded_text);
printf("b64 encoded text\n");
printf("%s\n\n", b64encoded_text);
// base64_encode(평문을 가리키는 포인터, 평문 길이, b64 인코딩 결과문을 가리키는 포인터)
// 3번째 인자는 이중 포인터임에 유의해야하며, base64_encode 함수 내부에서 메모리가 할당되므로, 외부에서 꼭 해제 시켜야 한다.
int len_b64_encoded = base64_encode(plain_text, strlen(plain_text)+1, &b64encoded_text);
printf("b64 encoded text\n");
printf("%s\n\n", b64encoded_text);
// base 64 디코딩
// 적절한 길이의 메모리 공간을 확보하고
// 해당 공간에 디코딩된 결과를 받습니다.
unsigned char *dst = (unsigned char*)malloc(1000);
int len_b64 = base64_decode(b64encoded_text,dst,1000);
// 적절한 길이의 메모리 공간을 확보하고
// 해당 공간에 디코딩된 결과를 받습니다.
unsigned char *dst = (unsigned char*)malloc(1000);
int len_b64 = base64_decode(b64encoded_text,dst,1000);
printf("b64 decoded tex\n");
printf("%s\n\n", dst);
printf("%s\n\n", dst);
if(dst != NULL) free(dst);
if(b64encoded_text != NULL) free(b64encoded_text);
if(b64encoded_text != NULL) free(b64encoded_text);
getchar();
return 0;
}
}