#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
C/C++ base64算法实现
最新推荐文章于 2024-07-21 23:54:33 发布
本文详细介绍了如何使用C/C++语言实现Base64编码和解码算法,包括基本原理和具体步骤,适用于数据的加密解密场景。
摘要由CSDN通过智能技术生成