linux系统base64加密算法,Linux下C语言实现的base64加解密

本文最后更新于2017年8月26日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

学习、提高需要

正文:

参考解答:

#include

#include

#include

#include

#include

#include

#include

#include

//base64_encode

int base64_encode(const char *input, int input_size, int with_new_line, char *buff, int buff_size)

{

if (input == NULL || input_size <= 0 || buff == NULL || buff_size <= 0) {

return -1;

}

BIO *bmem = NULL;

BUF_MEM *bptr = NULL;

BIO *b64 = NULL;

b64 = BIO_new(BIO_f_base64());

if(!with_new_line) {

BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

}

bmem = BIO_new(BIO_s_mem());

b64 = BIO_push(b64, bmem);

BIO_write(b64, input, input_size);

BIO_flush(b64);

BIO_get_mem_ptr(b64, &bptr);

if (buff_size < (bptr->length + 1)) {

fprintf(stderr, "base64_encode output buff too small, need %ld, now %d\n", bptr->length + 1, buff_size);

BIO_free_all(b64);

return -1;

}

// char *buff = (char *)malloc(bptr->length + 1);

memcpy(buff, bptr->data, bptr->length);

buff[bptr->length] = 0;

BIO_free_all(b64);

return 0;

}

//base64_decode

int base64_decode(char *input, int input_size, int with_new_line, char *buff, int buff_size)

{

if (input == NULL || input_size <= 0 || buff == NULL || buff_size <= 0) {

return -1;

}

memset(buff, 0, buff_size);

BIO *bmem = NULL;

BIO *b64 = NULL;

b64 = BIO_new(BIO_f_base64());

if(!with_new_line) {

BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

}

bmem = BIO_new_mem_buf(input, input_size);

bmem = BIO_push(b64, bmem);

BIO_read(bmem, buff, input_size);

BIO_free_all(bmem);

CRYPTO_cleanup_all_ex_data();//free crypto_malloc

return 0;

}

// int base64_encode(const char *input, int input_size, int with_new_line, char *buff, int buff_size);

// int base64_decode(char *input, int input_size, int with_new_line, char *buff, int buff_size);

// $ gcc openssl_base64.c -lcrypto

int main(int argc, char const *argv[])

{

if (argc < 2) {

printf("Usage:\n\t%s str_to_be_base64_encode\n\n", argv[0]);

return 0;

}

char buff[1024] = "\0";

int ret = -1;

ret = base64_encode(argv[1], strlen(argv[1]), 0, buff, sizeof(buff));

if (ret == 0) {

printf("input = %s\n", argv[1]);

printf("base64_encode = %s\n", buff);

}

char buff_decode[1024] = "\0";

ret = base64_decode(buff, strlen(buff), 0, buff_decode, sizeof(buff_decode));

if (ret == 0) {

printf("input = %s\n", buff);

printf("buff_decode = %s\n", buff_decode);

}

return 0;

}

参考链接:

=END=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 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 的官方文档或其他资源获取更多信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值