OpenSSL测试-Base64

  1. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
  2. 使用工具(如bc,计算机器等)把自己学号转化为16进制,提交转化过程和结果截图(2‘)
  3. 使用工具(如echo -e, ultraedit等)把上面转化的结果写入二进制文件“你的学号.dat”中,并用工具od -tx1 你的学号.dat,提交命令运行(3’)
  4. 使用OpenSSL的base64命令对"你的学号.dat"进行编码解码,提交过程截图(5')
  5. 使用OpenSSL编程对"你的学号.dat"进行编码解码,提交代码和运行结果截图。(10’)
    本次测试我选择在Ubuntu中完成下面任务

1、使用工具(如bc,计算机器等)把自己学号转化为16进制,提交转化过程和结果截图
由此可以我学号的16进制为1343EF5

2、使用工具(如echo -e, ultraedit等)把上面转化的结果写入二进制文件“你的学号.dat”中,并用工具od -tx1 你的学号.dat,提交命令运行

3、使用OpenSSL的base64命令对"你的学号.dat"进行编码解码,提交过程截图

4、使用OpenSSL编程对"你的学号.dat"进行编码解码,提交代码和运行结果截图。
代码:

#include <stdio.h>

#include <string.h>

#include <openssl/evp.h>

#include <openssl/x509.h>

//Base64编码

void tEVP_Encode()

{

​ EVP_ENCODE_CTX *ctx;

​ ctx = EVP_ENCODE_CTX_new(); //EVP编码结构体unsigned char in[1024]; //输入数据缓冲区int inl; //输入数据长度char out[2048]={0}; //输出数据缓冲区int outl; //输出数据长度

​ FILE *infp; //输入文件句柄

​ FILE *outfp; //输出文件句柄

​ infp = fopen("20181309.bat","rb");//打开待编码的文件if(infp == NULL)

​ {

​ printf("Open File "20181309.bat" 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("文件"20181309.bat" 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("对文件"20181309.bat" Base64编码完成,保存到"test.txt"文件.\n\n\n");

}

//Base64解码

void tEVP_Decode()

{

​ EVP_ENCODE_CTX *ctx;

​ ctx = EVP_ENCODE_CTX_new(); //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\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\n\n");

​

}

int main()

{

​ tEVP_Encode();

​ tEVP_Decode();

​

​ return 0;

}


posted @   西宁西  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示