- lizexuan's blog
Base64代码加密c++
- 2023-5-13 12:33:34 @
// Code written by Li Zexuan (Looks handsome)
// This code is written by DataErr0r and plagiarism is prohibited!!!
#include <bits/stdc++.h>
#ifndef BASE_64_H
#define BASE_64_H
using namespace std;
class Base64 {
private:
std::string _base64_table;
static const char base64_pad = '=';
public:
Base64() {
_base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
std::string Encode(const unsigned char * str, int bytes);
std::string Decode(const char *str, int bytes);
void Debug(bool open = true);
};
#endif
string Base64::Encode(const unsigned char * str, int bytes) {
int num = 0, bin = 0, i;
string _encode_result;
const unsigned char * current;
current = str;
while (bytes > 2) {
_encode_result += _base64_table[current[0] >> 2];
_encode_result += _base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
_encode_result += _base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
_encode_result += _base64_table[current[2] & 0x3f];
current += 3;
bytes -= 3;
}
if (bytes > 0) {
_encode_result += _base64_table[current[0] >> 2];
if (bytes % 3 == 1) {
_encode_result += _base64_table[(current[0] & 0x03) << 4];
_encode_result += "==";
} else if (bytes % 3 == 2) {
_encode_result += _base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
_encode_result += _base64_table[(current[1] & 0x0f) << 2];
_encode_result += "=";
}
}
return _encode_result;
}
string Base64::Decode(const char *str, int length) {
const char DecodeTable[] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 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, -2, -2, -2, -2, -2,
-2, 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, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};
int bin = 0, i = 0, pos = 0;
string _decode_result;
const char *current = str;
char ch;
while ( (ch = *current++) != '\0' && length-- > 0 ) {
if (ch == base64_pad) {
if (*current != '=' && (i % 4) == 1) {
return NULL;
}
continue;
}
ch = DecodeTable[ch];
if (ch < 0 ) {
continue;
}
switch (i % 4) {
case 0:
bin = ch << 2;
break;
case 1:
bin |= ch >> 4;
_decode_result += bin;
bin = ( ch & 0x0f ) << 4;
break;
case 2:
bin |= ch >> 2;
_decode_result += bin;
bin = ( ch & 0x03 ) << 6;
break;
case 3:
bin |= ch;
_decode_result += bin;
break;
}
i++;
}
return _decode_result;
}
int main() {
string str;
getline(cin, str);
int n = str.length();
str += '0';
unsigned char uch[1000000];
strcpy((char *)uch, str.c_str());
strncpy((char *)uch, str.c_str(), str.length());
memcpy(uch, str.c_str(), str.length());
snprintf((char *)uch, str.length(), str.c_str());
string normal, encoded;
int len = sizeof(str) - 1;
Base64 *base = new Base64();
encoded = base->Encode(uch, len);
cout << "编码后为:" << encoded << endl;
len = encoded.length();
const char * uch2 = encoded.c_str();
normal = base->Decode(uch2, len);
cout << "解码后为:" << normal << endl;
return 0;
}
四周前:改加密条将unsigned char str[] = "你知道吗,这个Base64很不好写的qwq";
改成你要加密的改你知道吗,这个Base64很不好写的qwq
部分