C/C++ base64算法实现

本文详细介绍了如何使用C/C++语言实现Base64编码和解码算法,包括基本原理和具体步骤,适用于数据的加密解密场景。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <string.h>
#include <assert.h>

typedef unsigned char u8;
typedef unsigned int  u32;


void encode(const u8 *src, u32 slen, u8 *dst, u32 *dlen)
{
    static const char b64[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    u32 i;
    unsigned i_bits = 0;
    int i_shift = 0;
    int out_len = 0;

    for (i = 0; i < slen; i++)
    {
        i_bits = (i_bits << 8) | src[i];
        i_shift += 8;
        while (i_shift >= 6)
        {
            dst[out_len++] = b64[(i_bits << 6 >> i_shift) & 0x3f];
            i_shift -= 6;
        }
    }
    while (i_shift > 0)
    {
        dst[out_len++] = b64[(i_bits << 6 >> i_shift) & 0x3f];
        i_shift -= 6;
    }
    while (out_len & 3)
    {
        dst[out_len++] = '=';
    }
    *dlen 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值