Base 64 학습

7450 단어 base64
               ,                   0      ,         key     。

aes         Base64     (          ),            ,   Base64     。
Base64 。
/// Base64   , 6bit     (  6     0),        ,                 ,(  6bit   2 bit        '=')。

/// Base64   ,         (        ),        (   6bit  ),     8bit         。

/* 

 * 

 *            ,            ,            ,    

 * 

 *    :

 * 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',	//   0 -   9  

 * 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',	//  10 -  19  

 * 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',	//  20 -  29  

 * 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',	//  30 -  39  

 * 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',	//  40 -  49  

 * 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',	//  50 -  59  

 * '8', '9', '+', '/'									//  60 -  63

 

 *    :

 * 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//   0 -   9  

 * 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//  10 -  19  

 * 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//  20 -  29  

 * 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//  30 -  39  

 * 0,  0,  0,  62, 0,  0,  0,  63, 52, 53,	//  40 -  49  

 * 54, 55, 56, 57, 58, 59, 60, 61, 0,  0,	//  50 -  59  

 * 0, 61,  0,  0,  0,  0,  1,  2,  3,  4,	//  60 -  69  

 * 5,  6,  7,  8,  9, 10, 11, 12, 13, 14,	//  70 -  79  

 * 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,  //  80 -  89  

 * 25,  0,  0,  0,  0,  0,  0, 26, 27, 28,  //  90 -  99  

 * 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,  // 100 - 109  

 * 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,  // 110 - 119  

 * 49, 50, 51,  0,  0,  0,  0,  0			// 120 - 127  

 */



///   :ABCDE       01000001 01000010 01000011 01000100 01000101

///  6bit      :   010000 010100 001001 000011 010001 000100 010100(  6bit,   0)

///       :         Q      U      J      D      R      E      U        =

///       :         00010000 00010100 00001001 00000011 00010001 00000100 00010100

///         ,  8bit       :

///     :             01000001 01000010 01000011 01000100 01000101 00



///               ,         key  ^  ,        key^    





////////////////////////////     ////////////////////////////

#ifndef __PWD_H__

#define __PWD_H__



#include <string>

using std::string;



///   Base64   

class PWD

{

public:

	static string encode(const string & text, const string & key);

	static string decode(const string & pass, const string & key);

private:

	static const char _encode_table[64];

	static const char _decode_table[128];

};



#endif





////////////////////////////     ////////////////////////////

#include "PWD.h"



const char PWD::_encode_table[64] =   

{  

    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',	//   0 -   9  

    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',	//  10 -  19  

    'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',	//  20 -  29  

    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',	//  30 -  39  

    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',	//  40 -  49  

    'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',	//  50 -  59  

    '8', '9', '+', '/'									//  60 -  63

};



const char PWD::_decode_table[128] = 

{

    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//   0 -   9  

    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//  10 -  19  

    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//  20 -  29  

    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,	//  30 -  39  

    0,  0,  0,  62, 0,  0,  0,  63, 52, 53,	//  40 -  49  

    54, 55, 56, 57, 58, 59, 60, 61, 0,  0,	//  50 -  59  

    0, 61,  0,  0,  0,  0,  1,  2,  3,  4,	//  60 -  69  

    5,  6,  7,  8,  9, 10, 11, 12, 13, 14,	//  70 -  79  

    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //  80 -  89  

    25,  0,  0,  0,  0,  0,  0, 26, 27, 28, //  90 -  99  

    29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109  

    39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119  

    49, 50, 51,  0,  0,  0,  0,  0			// 120 - 127  

};



string PWD::encode(const string & text, const string & key)

{

	string pwd;

	unsigned char temp[4] = { 0 };

	size_t text_len = text.length();

	size_t key_len = key.length();

	int key_index = 0;



	if (key_len == 0)

	{

		return pwd;

	}



	for (int index = 0; index < text_len / 3; index++)

	{

		temp[1] = text[index * 3 + 0] ^ key[(++key_index) % key_len];

		temp[2] = text[index * 3 + 1] ^ key[(++key_index) % key_len];

		temp[3] = text[index * 3 + 2] ^ key[(++key_index) % key_len];

		pwd += _encode_table[ temp[1] >> 2 ];

		pwd += _encode_table[ ((temp[1] << 4) | (temp[2] >> 4)) & 0x3F ];

		pwd += _encode_table[ ((temp[2] << 2) | (temp[3] >> 6)) & 0x3F ];

		pwd += _encode_table[ temp[3] & 0x3F ];

	}

	int mod = text_len % 3;

	if (mod == 1)

	{

		temp[1] = text[text_len - 1] ^ key[(++key_index) % key_len];

		pwd += _encode_table[ (temp[1] & 0xFC) >> 2 ];

		pwd += _encode_table[ (temp[1] & 0x03) << 4 ];

		pwd += "==";

	}

	else if (mod == 2)

	{

		temp[1] = text[text_len - 2] ^ key[(++key_index) % key_len];

		temp[2] = text[text_len - 1] ^ key[(++key_index) % key_len];

		pwd += _encode_table[ (temp[1] & 0xFC) >> 2 ];

		pwd += _encode_table[ ((temp[1] & 0x03) << 4) | ((temp[2] & 0xF0) >> 4) ];

		pwd += _encode_table[ (temp[2] & 0x0F) << 2 ];

		pwd += "=";

	}

	return pwd;

}

string PWD::decode(const string & pass, const string & key)

{

	string text;

	int temp;

	size_t pass_len = pass.length();

	size_t key_len = key.length();

	int index = 0;

	int key_index = 0;

	if (key_len == 0)

	{

		return text;

	}



	while (index < pass_len)

	{

		temp = _decode_table[pass[index++]] << 18;

		temp += _decode_table[pass[index++]] << 12;

		text.push_back(((temp & 0xFF0000) >> 16) ^ key[(++key_index) % key_len]);

		if (pass[index] != '=')

		{

			temp += _decode_table[pass[index++]] << 6;

			text.push_back(((temp & 0xFF00) >> 8) ^ key[(++key_index) % key_len]);

			if (pass[index] != '=')

			{

				temp += _decode_table[pass[index++]];

				text.push_back((temp & 0xFF)  ^ key[(++key_index) % key_len]);

			}

			else

			{

				break;

			}

		}

		else

		{

			break;

		}

	}

	return text;

}


이상 의 설명 과 코드 는 참고 로 제공 되 며, 오류 가 발견 되면 지적 을 환영 합 니 다.

좋은 웹페이지 즐겨찾기