码迷,mamicode.com
首页 > 其他好文 > 详细

base64编码

时间:2018-10-13 02:30:45      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:最大   就是   nbsp   pre   进制   需求   size   a.out   std   

作用

在互联网上传输二进制数据。一个字节是8位,能表示最大的数是255。但是ASCII表能表示的最大数是127,两者需要转换一下。base64就是将3个8位的数据,转为4个6位的数据。转换后的字符都是可打印字符。一般设置为"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"。当然,你可以根据自己的需求,使用别的可打印字符

下面是参考nginx实现的base64编码和解码

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


void base64_encode(u_char* dst, const u_char* src)
{
    char basis[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    int len = strlen((char*)src);

    while (len > 2)
    {
        *dst++ = basis[(src[0] >> 2) & 0x3f];
        *dst++ = basis[((src[0] & 3) << 4) | (src[1] >> 4)];
        *dst++ = basis[((src[1] & 0x0f) << 2) | (src[2] >> 6)];
        *dst++ = basis[src[2] & 0x3f];

        src += 3;
        len -= 3;
    }

    if (len == 1)
    {
        *dst++ = basis[src[0] >> 2];
        *dst++ = basis[(src[0] & 0x3) << 4];
        *dst++ = =;
        *dst++ = =;
    }

    if (len == 2)
    {
        *dst++ = basis[src[0] >> 2];
        *dst++ = basis[((src[0] & 3) << 4) | (src[1] >> 4)];
        *dst++ = basis[(src[1] & 0xf) << 2];
        *dst++ = =;
    }
    *dst = \0;
}


int base64_decode(u_char* dst, const u_char* src)
{
    static u_char   basis64[] = {
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
        77,  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, 77, 77, 77, 77, 77,
        77, 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, 77, 77, 77, 77, 77,

        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
    };

    int srclen = strlen((char*)src);
    int len = 0;

    for (; len < srclen; len++)
    {
        if (src[len] == =)
        {
            break;
        }

        if (basis64[src[len]] == 77)
        {
            return -1;
        }
    }

    if ((len >> 2) == 1)
    {
        return -1;
    }

    while (len > 3)
    {
        *dst++ = (u_char)((basis64[src[0]] << 2) | (basis64[src[1]] >> 4));
        *dst++ = (u_char)((basis64[src[1]] << 4) | (basis64[src[2]] >> 2));
        *dst++ = (u_char)((basis64[src[2]] << 6) | basis64[src[3]]);

        src += 4;
        len -= 4;
    }

    if (len > 1)
    {
        *dst++ = (u_char)((basis64[src[0]] << 2) | (basis64[src[1]] >> 4));
    }
    if (len > 2)
    {
        *dst++ = (u_char)((basis64[src[1]] << 4) | (basis64[src[2]] >> 2));
    }
    *dst = \0;
}


int main(int argc, char **argv)
{
    u_char szencode[100] = {0};
    u_char szdecode[100] = {0};

    base64_encode(szencode, (u_char*)argv[1]);
    printf("%s\n", szencode);

    base64_decode(szdecode, szencode);
    printf("%s\n", szdecode);

    return 0;
}


./a.out ‘test测试english和and中文chinese‘
dGVzdOa1i+ivlWVuZ2xpc2jlkoxhbmTkuK3mlodjaGluZXNl
test测试english和and中文chinese

 

base64编码

标签:最大   就是   nbsp   pre   进制   需求   size   a.out   std   

原文地址:https://www.cnblogs.com/zuofaqi/p/9781251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!