#include<stdio.h> #include<evp.h> #include<string.h> #include<windows.h> #include<x509.h> //Base64 编码 void tEVP_Encode() { EVP_ENCODE_CTX ctx; 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); printf("文件/"Test.dat/" Base64编码后为: /n"); //循环读取原文,并调用EVP_EncodeUpdate 计算Base64编码 while(1) { inl=fread(in,1,1024,infp); if (inl<=0) break; EVP_EncodeUpdate(&ctx,(unsigned char *)out,&outl,in,inl); fwrite(out,1,outl,outfp); printf("%s",out); } EVP_EncodeFinal(&ctx,(unsigned char *)out,&outl); fwrite(out,1,outl,outfp); printf("%s",out); fclose(infp); fclose(outfp); printf("对文件/"Test.dat/" Base64 编码完成,保存到/"test.txt/"文件,/n/n/n/n"); return; } //Base64 解码 void tEVP_Decode() { EVP_ENCODE_CTX ctx; 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.dat/" 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,(unsigned char *)in,inl); 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"); return; } int main(){ OpenSSL_add_all_algorithms(); tEVP_Encode(); tEVP_Decode(); return 0; }