Linux下C语言实现的base64加解密


=Start=

缘由:

学习、提高需要

正文:

参考解答:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>


//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=


《“Linux下C语言实现的base64加解密”》 有 2 条评论

  1. 魔鬼在细节中:Base64 你可能不知道的几个细节
    https://liudanking.com/sitelog/%E9%AD%94%E9%AC%BC%E5%9C%A8%E7%BB%86%E8%8A%82%E4%B8%AD%EF%BC%9Abase64-%E4%BD%A0%E5%8F%AF%E8%83%BD%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84%E5%87%A0%E4%B8%AA%E7%BB%86%E8%8A%82/
    `
    Base64 是什么?
    Base64 不是什么?
    Base64 编码结果是唯一的吗?
    不是的。Base64 根据编码字典表不同以及是否 padding (使用=作为 padding 字符),对同一数据的编码结果可能不同。
    Base64 是 url/filename safe 的吗?
    Base64 可以自定义特殊字符吗?
    Base64 编码结果中的等号(=)可以省略吗?是多余的设计吗?可以省略,但不是多余的设计。
    `
    https://tools.ietf.org/html/rfc4648
    https://stackoverflow.com/questions/4080988/why-does-Base64-encoding-require-padding-if-the-input-length-is-not-divisible-by

  2. Base64 的原理、实现及应用
    https://juejin.im/post/5b7d50106fb9a019d7475785
    `
    一、Base64编码原理
    二、Base64解码原理
    三、Base64编码字符串实例
    四、Base64编码的应用
    五、Base64具体实现
      1. 对字符串进行Base64编码
      2. 对字符串进行Base64解码
      3. 对文件进行Base64编码
      4. 对文件进行Base64编码
      5. 针对Base64.DEFAULT参数说明

    注意:BASE64Encoder每编码76个字符后,都会在后面加上一个回车换行。所以当超过76个字符的字符串被编码后,再解码时会提示RuntimeError;如果除去编码后出现的所有换行符,如使用Base64.NO_WRAP,则字符串能正常解码。
    `

回复 hi 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注