我是靠谱客的博主 呆萌便当,最近开发中收集的这篇文章主要介绍[crypto]-50-base64_encode和base64_decode的C语言实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

base64_encode和base64_decode的C语言实现

方法一(笨方法):

const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char * base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
    int i, j;
    unsigned char current;

    for ( i = 0, j = 0 ; i < binlength ; i += 3 )
    {
        current = (bindata[i] >> 2) ;
        current &= (unsigned char)0x3F;
        base64[j++] = base64char[(int)current];

        current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
        if ( i + 1 >= binlength )
        {
            base64[j++] = base64char[(int)current];
            base64[j++] = '=';
            base64[j++] = '=';
            break;
        }
        current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
        base64[j++] = base64char[(int)current];

        current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
        if ( i + 2 >= binlength )
        {
            base64[j++] = base64char[(int)current];
            base64[j++] = '=';
            break;
        }
        current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
        base64[j++] = base64char[(int)current];

        current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
        base64[j++] = base64char[(int)current];
    }
    base64[j] = '';
    return base64;
}

int base64_decode( const char * base64, unsigned char * bindata )
{
    int i, j;
    unsigned char k;
    unsigned char temp[4];
    for ( i = 0, j = 0; base64[i] != '' ; i += 4 )
    {
        memset( temp, 0xFF, sizeof(temp) );
        for ( k = 0 ; k < 64 ; k ++ )
        {
            if ( base64char[k] == base64[i] )
                temp[0]= k;
        }
        for ( k = 0 ; k < 64 ; k ++ )
        {
            if ( base64char[k] == base64[i+1] )
                temp[1]= k;
        }
        for ( k = 0 ; k < 64 ; k ++ )
        {
            if ( base64char[k] == base64[i+2] )
                temp[2]= k;
        }
        for ( k = 0 ; k < 64 ; k ++ )
        {
            if ( base64char[k] == base64[i+3] )
                temp[3]= k;
        }

        bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
                ((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
        if ( base64[i+2] == '=' )
            break;

        bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
                ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
        if ( base64[i+3] == '=' )
            break;

        bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
                ((unsigned char)(temp[3]&0x3F));
    }
    return j;
}

方法二:

static const unsigned char base64_suffix_map[] =
{
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    62,        // '+'
    0, 0, 0,
    63,        // '/'
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61,        // '0'-'9'
    0, 0, 0, 0, 0, 0, 0,
    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,        // 'A'-'Z'
    0, 0, 0, 0, 0, 0,
    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,        // 'a'-'z'
};

int base64_decode(char *indata, uint32_t inlen, char *outdata, uint32_t *outlen)
{
	int ret = 0;
	int t = 0, x = 0, y = 0, i = 0;
	unsigned char c = 0;
	int g = 3;
	if (indata == NULL || inlen <= 0 || outdata == NULL || outlen == NULL) {
		return ret = -1;
	}
	if (inlen % 4 != 0) {
		return ret = -1;
	}

	while (indata[x] != 0) {
		c = base64_suffix_map[(uint8_t)indata[x++]];
		if (c == 255) return -1;
		if (c == 253) continue;
		if (c == 254) { c = 0; g--; }
		t = (t<<6) | c;
		if (++y == 4) {
			outdata[i++] = (unsigned char)((t>>16)&0xff);
			if (g > 1) outdata[i++] = (unsigned char)((t>>8)&0xff);
			if (g > 2) outdata[i++] = (unsigned char)(t&0xff);
			y = t = 0;
		}
	}
	if (outlen != NULL) {
		*outlen = i;
	}
	return ret;
}

相关推荐:
         [crypto]-01-对称加解密AES原理概念详解
         [crypto]-02-非对称加解密RSA原理概念详解
         [crypto]-03-数字摘要HASH原理概念详解
         [crypto]-04-国产密码算法(国密算法sm2/sm3/sm4)介绍
         [crypto]-05-转载:PKCS #1 RSA Encryption Version 1.5介绍
         [crypto]-05.1-PKCS PKCS#1 PKCS#7 PKCS#11的介绍
         [crypto]-06-CA证书介绍和使用方法


         [crypto]-30-The Armv8 Cryptographic Extension在linux中的应用
         [crypto]-31-crypto engion的学习和总结


         [crypto]-50-base64_encode和base64_decode的C语言实现
         [crypto]-51-RSA私钥pem转换成der, 在将der解析出n e d p q dp dq qp
         [crypto]-52-python3中rsa(签名验签加密解密)aes(ecb cbc ctr)hmac的使用,以及unittest测试用
         [crypto]-53-openssl命令行的使用(aes/rsa签名校验/rsa加密解密/hmac)


         [crypto]-90-crypto的一些术语和思考

最后

以上就是呆萌便当为你收集整理的[crypto]-50-base64_encode和base64_decode的C语言实现的全部内容,希望文章能够帮你解决[crypto]-50-base64_encode和base64_decode的C语言实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部