我的一个叫"hello.exe"的文件被别人用Base64编了码,现在变成这个样子"A=GHJK?=……",在一个txt文件里,我怎么才能把他还原成原来的样子 Up有分 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是加密了吗??怎么加的。然后还怎么解密吧??Base64编码是什么咚咚?? #define BASE64_ENCODE_LENGTH(len) (4 * (((len) + 2) / 3))#define BASE64_DECODE_LENGTH(len) (3 * (((len) + 3) / 4))static char encodeTable[65] = { '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 char decodeTable[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,};/** * src : bytes need to encode * dst : buffer to store result * length : length of src * * sizeof(dst) should be BASE64_ENCODE_LENGTH(length) */void base64_encode (const char *src, char *dst, int length) { int in, out; const unsigned char* p = (const unsigned char*)src; for (in = 0, out = 0; in < length; in += 3, out += 4) { bool quad = false, trip = false; uint32_t val = p[in]; val <<= 8; if ((in + 1) < length) { val |= p[in+1]; trip = true; } val <<= 8; if ((in + 2) < length) { val |= p[in+2]; quad = true; } dst[out+3] = encodeTable[quad ? (val & 0x3f) : 64]; val >>= 6; dst[out+2] = encodeTable[trip ? (val & 0x3f) : 64]; val >>= 6; dst[out+1] = encodeTable[val & 0x3f]; val >>= 6; dst[out] = encodeTable[val & 0x3f]; }}/** * src : bytes need to decode * dst : buffer to store result * length : length of src * * return : result bytes count in dst, less than or equal to BASE64_DECODE_LENGTH(length) */int base64_decode(const char *src, char *dst, int length) { int in, out; const unsigned char* p = (const unsigned char*)src; int shift = 0; uint32_t accum = 0; for (in = 0, out = 0; in < length; in++) { int value = decodeTable[ p[in] ]; if (value >= 0) { accum <<= 6; shift += 6; accum |= value; if (shift >= 8) { shift -= 8; dst[out++] = ((accum >> shift) & 0xff); } } } // if (shift != 0) : src is not a valid base64 buffer return out;}#include <iostream>#include <fstream>#include <cstring>using namespace std;// test and examle main() :// for repeated read, the buffer size must be divided by 3 and 4#define BUFSZ (3 * 1024)int main(int argc, char *argv[]) { if (argc < 3) { cerr << "usage : base64 [-d] inputFilename outputFilename" << endl; exit(1); } bool encodeFlag = true; if (argc == 4 && !strcmp(argv[1], "-d")) encodeFlag = false; ifstream ifs(argv[argc-2], ios::in | ios::binary); ofstream ofs(argv[argc-1], ios::out| ios::binary | ios::trunc); if (ifs.fail() || ofs.fail()) { cerr << "file error" << endl; exit(1); } char *inbuf = new char[BUFSZ]; char *outbuf; int len; if (encodeFlag) { cout << "Encode ..." << endl; outbuf = new char[BASE64_ENCODE_LENGTH(BUFSZ)]; while (!ifs.eof()) { ifs.read(inbuf, BUFSZ); len = ifs.gcount(); base64_encode(inbuf, outbuf, len); ofs.write(outbuf, BASE64_ENCODE_LENGTH(len)); } } else { cout << "Decode ..." << endl; outbuf = new char[BASE64_DECODE_LENGTH(BUFSZ)]; while (!ifs.eof()) { ifs.read(inbuf, BUFSZ); len = base64_decode(inbuf, outbuf, ifs.gcount()); ofs.write(outbuf, len); } } delete inbuf; delete outbuf; return 0;} 网上多的是!base64用得最多的地方实在Email的附件中的!就是把4字节编码为3字节的问题! boost库问题 字符处理 函数中有换行符! 如何得知系统当前使用的输入法? TCP/IP高手请进 用哪个函数可以打开一个网页? 100分求代理服务器 请教关于难问题 满分100!放入对话框的视为什么相应鼠标消息要出错???? http://www.csdn.net/Expert/Forum.asp?typenum=8&Roomid=1&whichpage=2&searchKeys=url&author=&tabletype=now什么意思啊? 一个opengl的显示问题 socket()函数中的IPPROTO_TCP,IPPROTO_IP,0有什么区别?
#define BASE64_DECODE_LENGTH(len) (3 * (((len) + 3) / 4))static char encodeTable[65] = {
'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 char decodeTable[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,};/**
* src : bytes need to encode
* dst : buffer to store result
* length : length of src
*
* sizeof(dst) should be BASE64_ENCODE_LENGTH(length)
*/
void base64_encode (const char *src, char *dst, int length) { int in, out;
const unsigned char* p = (const unsigned char*)src; for (in = 0, out = 0; in < length; in += 3, out += 4) {
bool quad = false, trip = false; uint32_t val = p[in];
val <<= 8;
if ((in + 1) < length) {
val |= p[in+1];
trip = true;
}
val <<= 8;
if ((in + 2) < length) {
val |= p[in+2];
quad = true;
} dst[out+3] = encodeTable[quad ? (val & 0x3f) : 64];
val >>= 6;
dst[out+2] = encodeTable[trip ? (val & 0x3f) : 64];
val >>= 6;
dst[out+1] = encodeTable[val & 0x3f];
val >>= 6;
dst[out] = encodeTable[val & 0x3f];
}}/**
* src : bytes need to decode
* dst : buffer to store result
* length : length of src
*
* return : result bytes count in dst, less than or equal to BASE64_DECODE_LENGTH(length)
*/
int base64_decode(const char *src, char *dst, int length) { int in, out;
const unsigned char* p = (const unsigned char*)src; int shift = 0;
uint32_t accum = 0; for (in = 0, out = 0; in < length; in++) {
int value = decodeTable[ p[in] ]; if (value >= 0) {
accum <<= 6;
shift += 6;
accum |= value;
if (shift >= 8) {
shift -= 8;
dst[out++] = ((accum >> shift) & 0xff);
}
}
} // if (shift != 0) : src is not a valid base64 buffer return out;}
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;// test and examle main() :// for repeated read, the buffer size must be divided by 3 and 4
#define BUFSZ (3 * 1024)int main(int argc, char *argv[]) { if (argc < 3) {
cerr << "usage : base64 [-d] inputFilename outputFilename" << endl;
exit(1);
} bool encodeFlag = true; if (argc == 4 && !strcmp(argv[1], "-d"))
encodeFlag = false; ifstream ifs(argv[argc-2], ios::in | ios::binary);
ofstream ofs(argv[argc-1], ios::out| ios::binary | ios::trunc); if (ifs.fail() || ofs.fail()) {
cerr << "file error" << endl;
exit(1);
} char *inbuf = new char[BUFSZ];
char *outbuf;
int len; if (encodeFlag) {
cout << "Encode ..." << endl;
outbuf = new char[BASE64_ENCODE_LENGTH(BUFSZ)];
while (!ifs.eof()) {
ifs.read(inbuf, BUFSZ);
len = ifs.gcount();
base64_encode(inbuf, outbuf, len);
ofs.write(outbuf, BASE64_ENCODE_LENGTH(len));
}
} else {
cout << "Decode ..." << endl;
outbuf = new char[BASE64_DECODE_LENGTH(BUFSZ)];
while (!ifs.eof()) {
ifs.read(inbuf, BUFSZ);
len = base64_decode(inbuf, outbuf, ifs.gcount());
ofs.write(outbuf, len);
}
} delete inbuf;
delete outbuf; return 0;
}
base64用得最多的地方实在Email的附件中的!就是把4字节编码为3字节的问题!