OpenSSL Base64编码与解码
来源:互联网 发布:ftp防火墙端口设置 编辑:程序博客网 时间:2024/05/21 01:56
#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;}
0 0
- OpenSSL Base64编码与解码
- OPENSSL Base64编码和解码
- OpenSSL 使用 base64 编码/解码
- 利用openssl进行base64的编码与解码
- 利用OpenSSL进行BASE64的编码与解码
- Base64编码与解码
- BASE64编码与解码
- BASE64编码与解码
- Base64编码与解码
- base64编码与解码
- base64编码与解码
- Base64 编码与解码
- Base64编码与解码
- Base64编码与解码
- Base64编码与解码
- base64编码与解码
- BASE64编码与解码
- Base64编码与解码
- OpenSSL RSA 消息签名与验证
- First Missing Positive
- as3中为什么要用各种Listener实现?
- java读取web页面信息
- 伙伴算法
- OpenSSL Base64编码与解码
- 解释与编译
- Golang第二天
- 【Android界面实现】整合了刷新、加载更多、滑动删除功能的XListview
- 【无限互联】ios开发之AFNetWorking
- 关于Animation State could not be played的问题
- 关于Httpclient的使用总结
- jQuery选择器:input实现—全选,反选,取消(匹配所有 input, textarea, select 和 button 元素)
- java学习二十三 java循环语句while