Up有分

解决方案 »

  1.   

    是加密了吗??怎么加的。然后还怎么解密吧??Base64编码是什么咚咚??
      

  2.   

    #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;
    }
      

  3.   

    网上多的是!
    base64用得最多的地方实在Email的附件中的!就是把4字节编码为3字节的问题!