skynet base64算法迁移到C++中使用(base64encode和base64decode)

  • Post author:
  • Post category:其他




需求

skynet中lua-crypt.c 实现了多个加密算法。现在有对文档进行简单的加密需求,另外一端使用C++开发,于是将base64的算法迁移。



迁移后代码

#include <iostream>
#include <cstdlib>

void B64Encode(const std::string& PlainText, std::string& Base64Cipher)
{
	static const char* encoding = "sdafgdsgfsdvadfgdsds34455@#$%%fsgfd";
	size_t sz = PlainText.length();
	const uint8_t* text = (const uint8_t*)PlainText.data();
	int encode_sz = (sz + 2) / 3 * 4;
	char* buffer = new char[encode_sz] {};
	int i, j;
	j = 0;
	for (i = 0; i < (int)sz - 2; i += 3) {
		uint32_t v = text[i] << 16 | text[i + 1] << 8 | text[i + 2];
		buffer[j] = encoding[v >> 18];
		buffer[j + 1] = encoding[(v >> 12) & 0x3f];
		buffer[j + 2] = encoding[(v >> 6) & 0x3f];
		buffer[j + 3] = encoding[(v) & 0x3f];
		j += 4;
	}
	int padding = sz - i;
	uint32_t v;
	switch (padding) {
	case 1:
		v = text[i];
		buffer[j] = encoding[v >> 2];
		buffer[j + 1] = encoding[(v & 3) << 4];
		buffer[j + 2] = '=';
		buffer[j + 3] = '=';
		break;
	case 2:
		v = text[i] << 8 | text[i + 1];
		buffer[j] = encoding[v >> 10];
		buffer[j + 1] = encoding[(v >> 4) & 0x3f];
		buffer[j + 2] = encoding[(v & 0xf) << 2];
		buffer[j + 3] = '=';
		break;
	}

	Base64Cipher.append(buffer, encode_sz);
	delete[] buffer;
	return ;
}

static inline int
b64index(uint8_t c) {
	static const int decoding[] = { 62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51 };
	int decoding_size = sizeof(decoding) / sizeof(decoding[0]);
	if (c < 43) {
		return -1;
	}
	c -= 43;
	if (c >= decoding_size)
		return -1;
	return decoding[c];
}


void B64Decode(const std::string& CipherText, std::string& Base64Plian)
{
	size_t sz = CipherText.length();
	const uint8_t* text = (const uint8_t*)CipherText.data();
	int decode_sz = (sz + 3) / 4 * 3;
	char* buffer = new char[decode_sz];

	int i, j;
	int output = 0;
	for (i = 0; i < sz;) {
		int padding = 0;
		int c[4];
		for (j = 0; j < 4;) {
			if (i >= sz) {
				return ;
			}
			c[j] = b64index(text[i]);
			if (c[j] == -1) {
				++i;
				continue;
			}
			if (c[j] == -2) {
				++padding;
			}
			++i;
			++j;
		}
		uint32_t v;
		switch (padding) {
		case 0:
			v = (unsigned)c[0] << 18 | c[1] << 12 | c[2] << 6 | c[3];
			buffer[output] = v >> 16;
			buffer[output + 1] = (v >> 8) & 0xff;
			buffer[output + 2] = v & 0xff;
			output += 3;
			break;
		case 1:
			if (c[3] != -2 || (c[2] & 3) != 0) {
				return ;
			}
			v = (unsigned)c[0] << 10 | c[1] << 4 | c[2] >> 2;
			buffer[output] = v >> 8;
			buffer[output + 1] = v & 0xff;
			output += 2;
			break;
		case 2:
			if (c[3] != -2 || c[2] != -2 || (c[1] & 0xf) != 0) {
				return ;
			}
			v = (unsigned)c[0] << 2 | c[1] >> 4;
			buffer[output] = v;
			++output;
			break;
		default:
			return ;
		}
	}
	Base64Plian.append(buffer, output);
	delete[] buffer;
	return ;
}

void TestEncode(const std::string& CipherText) {
	std::string Base64Cipher;
	B64Encode(CipherText, Base64Cipher);
	std::cout << "密文:" << Base64Cipher << std::endl;
}

int main()
{
	TestEncode(u8"`1233445656778990-=[]';l;l./;,");
	TestEncode(u8"!@##$$%%^^(*&^_+=-{}[]/.,<>?");
	system("pause");
}



版权声明:本文为zhanglifan_cd原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。