OpenSSL Base64编码与解码

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/x509.h>

//Base64编码
void tEVP_Encode()
{
	EVP_ENCODE_CTX ctx;	//EVP编码结构体
	unsigned char in[1024];	//输入数据缓冲区
	int inl;		//输入数据长度
	char out[2048]={0};	//输出数据缓冲区
	int outl;		//输出数据长度
	FILE *infp;		//输入文件句柄
	FILE *outfp;		//输出文件句柄

	infp = fopen("test.dat","rb");//打开待编码的文件
	if(infp == NULL)
	{
		printf("Open File \"Test.dat\"  for Read Err.\n");
		return;
	}
	
	outfp = fopen("test.txt","w");//打开编码后保存的文件
	if(outfp == NULL)
	{
		printf("Open File \"test.txt\" For Write Err.\n");
		return;
	}
	EVP_EncodeInit(&ctx);//Base64编码初始化
	printf("文件\"Test.dat\" Base64编码后为:\n");
	//循环读取原文,并调用EVP_EncodeUpdate计算Base64编码
	while(1)
	{
		inl = fread(in,1,1024,infp);
		if(inl <= 0)
			break;
		EVP_EncodeUpdate(&ctx,out,&outl,in,inl);//编码
		fwrite(out,1,outl,outfp);//输出编码结果到文件
		printf("%s",out);
	} 
	EVP_EncodeFinal(&ctx,out,&outl);//完成编码,输出最后的数据。
	fwrite(out,1,outl,outfp);
	printf("%s",out);
	fclose(infp);
	fclose(outfp);	
	printf("对文件\"Test.dat\" Base64编码完成,保存到\"test.txt\"文件.\n\n");
}

//Base64解码
void tEVP_Decode()
{
	EVP_ENCODE_CTX ctx;	//EVP编码结构体
	char in[1024];		//输入数据缓冲区
	int inl;		//输入数据长度
	unsigned char out[1024];//输出数据缓冲区
	int outl;		//输出数据长度
	FILE *infp;		//输入文件句柄
	FILE *outfp;		//输出文件句柄
	
	infp = fopen("test.txt","r");//打开待解码的文件
	if(infp == NULL)
	{
		printf("Open File \"Test.txt\"  for Read Err.\n");
		return;
	}
	outfp = fopen("test-1.dat","wb");//打开解码后保存的文件
	if(outfp == NULL)
	{
		printf("Open File \"test-1.txt\" For Write Err.\n");
		return;
	}
	EVP_DecodeInit(&ctx);//Base64解码初始化
	printf("开始对文件\"Test.txt\" Base64解码...\n");
	//循环读取原文,并调用EVP_DecodeUpdate进行Base64解码
	while(1)
	{
		inl = fread(in,1,1024,infp);
		if(inl <= 0)
			break;
		EVP_DecodeUpdate(&ctx,out,&outl,in,inl);//Base64解码
		fwrite(out,1,outl,outfp);//输出到文件
	} 
	EVP_DecodeFinal(&ctx,out,&outl);//完成解码,输出最后的数据。
	fwrite(out,1,outl,outfp);
	fclose(infp);
	fclose(outfp);	
	printf("对文件\"Test.txt\" Base64解码完成,保存为\"test-1.dat\"\n");
	
}
 
int main()
{
 
	tEVP_Encode();
	tEVP_Decode();
	
	return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于Base64编码解码涉及到字符编码和位运算等细节,实现起来比较复杂,因此我们可以使用现成的库函数来实现。以下是使用OpenSSL库函数实现Base64编码解码的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> #define MAX_BUF_LEN 1024 int base64_encode(const char *src, int src_len, char *dst, int dst_len) { BIO *b64 = NULL; BIO *bio = NULL; BUF_MEM *bptr = NULL; int ret = -1; if (src == NULL || dst == NULL) { return -1; } b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bio = BIO_new(BIO_s_mem()); if (bio == NULL) { BIO_free_all(b64); return -1; } bio = BIO_push(b64, bio); BIO_write(bio, src, src_len); BIO_flush(bio); BIO_get_mem_ptr(bio, &bptr); if (bptr == NULL) { BIO_free_all(bio); return -1; } if (bptr->length > dst_len) { BIO_free_all(bio); return -1; } memcpy(dst, bptr->data, bptr->length); dst[bptr->length] = '\0'; ret = bptr->length; BIO_free_all(bio); return ret; } int base64_decode(const char *src, int src_len, char *dst, int dst_len) { BIO *b64 = NULL; BIO *bio = NULL; int ret = -1; if (src == NULL || dst == NULL) { return -1; } b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bio = BIO_new_mem_buf(src, src_len); if (bio == NULL) { BIO_free_all(b64); return -1; } bio = BIO_push(b64, bio); ret = BIO_read(bio, dst, dst_len); BIO_free_all(bio); return ret; } int main() { char src[MAX_BUF_LEN] = "Hello, world!"; char dst[MAX_BUF_LEN] = {0}; char out[MAX_BUF_LEN] = {0}; int src_len = strlen(src); int dst_len = MAX_BUF_LEN; int out_len = MAX_BUF_LEN; // Base64编码 int len = base64_encode(src, src_len, dst, dst_len); if (len >= 0) { printf("Base64 encoded: %s\n", dst); } else { printf("Base64 encode failed.\n"); return -1; } // Base64解码 len = base64_decode(dst, len, out, out_len); if (len >= 0) { printf("Base64 decoded: %s\n", out); } else { printf("Base64 decode failed.\n"); return -1; } return 0; } ``` 在上面的示例程序中,我们使用了OpenSSL库中的BIO和EVP模块来实现Base64编码解码。其中,`base64_encode`函数接受原始数据和长度,输出Base64编码后的数据和长度。`base64_decode`函数接受Base64编码后的数据和长度,输出解码后的原始数据和长度。在主函数中,我们先对原始数据进行Base64编码,然后再将编码后的数据解码回原始数据。如果编码解码都成功,则输出编码后的数据和解码后的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值