bai_jimmy

导航

php base64 原理

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char base64_table[] ={ 
        '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', '+', '/', '\0'
};

unsigned char *base64_encode(const unsigned char *str)
{
        static const char base64_pad = '=';
        const unsigned char *current = str;
        int length = strlen(str);
        unsigned char *p, *result;

        //检测

        result = (unsigned char *)malloc(((length + 2) / 3) * 4 * sizeof(char) + 1);
        p = result;

        while(length > 2){
                *p++ = base64_table[current[0] >> 2];
                *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
                *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
                *p++ = base64_table[current[2] & 0x3f];

                current += 3;
                length -= 3;
        }

        if(length == 2){
                *p++ = base64_table[current[0] >> 2];
                *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
                *p++ = base64_table[(current[1] & 0x0f) << 2];
                *p++ = base64_pad;
        }else if(length == 1){
                *p++ = base64_table[current[0] >> 2];
                *p++ = base64_table[(current[0] & 0x03) << 4];
                *p++ = base64_pad;
                *p++ = base64_pad;
        }

        *p = '\0';
        return result;
}

static int base64_decode_map[256] = {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0   - 15
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16  - 31
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32  - 47
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48  - 63
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, // 64  - 79
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80  - 95
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96  - 111
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112 - 127
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128 - 143
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144 - 159 
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160 - 175
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176 - 191
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192 - 207
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208 - 223
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224 - 239
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 240 - 255
};

char *base64_decode(const char *input, char *output)
{
        output[0] = '\0';

        int input_len = strlen(input);
        if(input_len < 4 || input_len % 4 != 0){
                return output;
        }

        char *p = (char *)input;
        char *p_out = output;
        char *p_end = (char *)input + input_len;

        for(; p < p_end; p += 4){
                *p_out++ = ((base64_decode_map[p[0]] << 2) & 0xFC) | ((base64_decode_map[p[1]] >> 4) & 0x03);
                *p_out++ = ((base64_decode_map[p[1]] << 4) & 0xF0) | ((base64_decode_map[p[2]] >> 2) & 0x0F);
                *p_out++ = ((base64_decode_map[p[2]] << 6) & 0xC0) | ((base64_decode_map[p[3]]));
        }

        if(*(input + input_len - 2) == '='){
                *(p_out - 2) = '\0';
        }else if(*(input + input_len - 1) == '='){
                *(p_out - 1) = '\0';
        }

        return output;
}

int main(){
        unsigned char *ps = "12";
        unsigned char *ret = base64_encode(ps);
        char output[100];
        memset(output, 0, sizeof output);
        base64_decode(ret, output);
        printf("%s, %s\n", ret, output);
}
View Code

 

posted on 2016-03-06 12:38  bai_jimmy  阅读(215)  评论(0)    收藏  举报