Base64编码、解码 C语言例子(使用OpenSSL库)


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

#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/evp.h>

int base64_encode(char *in_str, int in_len, char *out_str)
{
    BIO *b64, *bio;
    BUF_MEM *bptr = NULL;
    size_t size = 0;

    if (in_str == NULL || out_str == NULL)
        return -1;

    b64 = BIO_new(BIO_f_base64());
    bio = BIO_new(BIO_s_mem());
    bio = BIO_push(b64, bio);

    BIO_write(bio, in_str, in_len);
    BIO_flush(bio);

    BIO_get_mem_ptr(bio, &bptr);
    memcpy(out_str, bptr->data, bptr->length);
    out_str[bptr->length] = '\0';
    size = bptr->length;

    BIO_free_all(bio);
    return size;
}

int base64_decode(char *in_str, int in_len, char *out_str)
{
    BIO *b64, *bio;
    BUF_MEM *bptr = NULL;
    int counts;
    int size = 0;

    if (in_str == NULL || out_str == NULL)
        return -1;

    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

    bio = BIO_new_mem_buf(in_str, in_len);
    bio = BIO_push(b64, bio);

    size = BIO_read(bio, out_str, in_len);
    out_str[size] = '\0';

    BIO_free_all(bio);
    return size;
}

int main()
{
	char instr[] = "hello";
	char outstr1[1024] = {0};
	base64_encode(instr,5,outstr1);
	printf("base64:%s\n",outstr1);

	char outstr2[1024] = {0};
	base64_decode(outstr1,strlen(outstr1),outstr2);
	printf("str:%s\n",outstr2);
	return 0;
}

编译及运行:

$ gcc base64.c -lcrypto

./a.out

结果:

base64:aGVsbG8=

str:hello



注意:

在base64_encode的时候返回的base64编码自带换行,所以大家在使用的时候要注意,以免因为多了个换行而出现bug


  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 C 语言中,您可以使用 OpenSSL 来实现 Base64 解码。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> int base64_decode(const unsigned char* encoded_data, size_t encoded_length, unsigned char* decoded_data, size_t* decoded_length) { BIO *bio, *b64; size_t length = 0; // 创建 Base64 解码的 BIO b64 = BIO_new(BIO_f_base64()); bio = BIO_new_mem_buf(encoded_data, encoded_length); bio = BIO_push(b64, bio); // 解码 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); length = BIO_read(bio, decoded_data, encoded_length); // 清理并设置解码后的长度 BIO_free_all(bio); *decoded_length = length; return 0; } int main() { const char* encoded_text = "SGVsbG8gd29ybGQh"; // 输入要解码Base64 编码字符串 size_t encoded_length = strlen(encoded_text); size_t decoded_length = ((encoded_length + 3) / 4) * 3; unsigned char* decoded_text = (unsigned char*)malloc(decoded_length + 1); if (decoded_text == NULL) { printf("内存分配失败!\n"); return 1; } int result = base64_decode((const unsigned char*)encoded_text, encoded_length, decoded_text, &decoded_length); if (result != 0) { printf("解码失败:%d\n", result); free(decoded_text); return 1; } decoded_text[decoded_length] = '\0'; printf("解码结果:%s\n", decoded_text); free(decoded_text); return 0; } ``` 在上述示例代码中,我们使用OpenSSL 的 `BIO`(I/O 抽象)和 `EVP`(加密算法)来实现 Base64 解码。需要注意的是,您需要先安装 OpenSSL 并确保编译时链接了正确的文件。 编译和运行上述代码后,将输出解码结果:"Hello world!"。 请注意,此示例仅演示了如何使用 OpenSSL 来进行 Base64 解码。如果您需要进行更复杂的加密/解密操作,可以参考 OpenSSL 的官方文档或其他资源获取更多信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值