安装OpenSSL
检查原装OpenSSL

发现已安装好的OpenEuler系统里已经配好了OpenSSL工具,但版本不是最新的,尝试安装最新版本的OpenSSL
准备工作
环境配置
树莓派版本的OpenEuler操作系统中很多东西没有,需要自己用sudo dnf install command命令一个个下载。
首先用sudo dnf install wget和sudo dnf install yum下载wget和yum。
再用wget命令从OpenSSL官网下载最新版本tar文件
创建tqhopenssl和tqhopensslsrc两个文件夹

为解压压缩包,需要安装tar命令

将压缩包解压到tqhopensslsrc文件夹

解压完成

用make编译发现报错,安装make命令

此外还需要附带安装gcc

编译安装
先在当前目录下make

等待时间较长,约需要一小时

make编译完成,进入下一步make test和make install

其中make test花费时间会稍长一点。全部完成后检查OpenSSL版本

发现已经是1.1.1k(2021-5-21)最新版,但library却还是1.1.1f(2020-5-31)
修改库路径
下面解决library版本仍未更新的问题
首先尝试运用在之前配置动态库中所学方法,使用export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/home/tqh/tqhopenssl/lib添加一条环境变量,设置为默认的库。但此种方法只能临时生效。
参考博客Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH方法二,修改根目录下.bashrc文件,使路径更改永久生效

退出终端后再次登录仍有效

版本成功更新
OpenSSL测试和使用
简单测试
查看帮助

使用openssl help xxsubcmd或openssl xxsubcmd --help查看子命令的帮助(注意在当前路径下openssl前要加./)
查看MD5的帮助

使用管道进行摘要计算
sm3

md5

sha256

计算文件摘要

生成随机数

OpenSSL编程
测试代码
#include <stdio.h>
#include <openssl/evp.h>
int main(){
OpenSSL_add_all_algorithms();
return 0;
}

其中函数OpenSSL_add_all_algorithms()的作用是将所有算法载入。使用./openssl_test;echo $?是打印出主函数的返回值。
老师给出的base64代码进行编译测试
#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("test.dat","rb");//打开待编码的文件
if(infp == NULL)
{
printf("Open File "Test.dat" for Read Err.
");
return;
}
outfp = fopen("test.txt","w");//打开编码后保存的文件
if(outfp == NULL)
{
printf("Open File "test.txt" For Write Err.
");
return;
}
EVP_EncodeInit(ctx);//Base64编码初始化
printf("文件"Test.dat" Base64编码后为:
");
//循环读取原文,并调用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"文件.
");
}
//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.
");
return;
}
outfp = fopen("test-1.dat","wb");//打开解码后保存的文件
if(outfp == NULL)
{
printf("Open File "test-1.txt" For Write Err.
");
return;
}
EVP_DecodeInit(ctx);//Base64解码初始化
printf("开始对文件"Test.txt" Base64解码...
");
//循环读取原文,并调用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"
");
}
int main()
{
tEVP_Encode();
tEVP_Decode();
return 0;
}
测试成功,运行结果
