实验一 密码引擎-2-OpenEuler-OpenSSL测试

在Ubuntu编写代码测试OpenSSL功能,包含Base64,SM2,SM3,SM4算法的调用,然后在OpenEuler中重现
提交代码链接和运行结果截图
加分项:在Windows中重现(已重现)所有代码均在链接里——已完成
代码链接:https://gitee.com/csq200215/csq/tree/master/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8%E7%B3%BB%E7%BB%9F%EF%BC%88%E4%B8%8B%EF%BC%89%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%AE%9E%E9%AA%8C

一、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.\n");
return;
}

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

}

//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();
 
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;

}

一、执行命令将Base64.c编译成可执行文件Base64
gcc -o Base64 Base64.c -I /usr/local/ssl/inlcude -L /usr/local/ssl/lib -ldl -lpthread -lcrypto
gcc -o Base64 Base64.c -lpthread -lcrypto

 OpenEuler下的编译:

 Windows下实现:

结果均是正确的。

 

二、实现SM2

Ubuntu下编译:

 在OpenEuler下编译:

 在Windows下重现:

 结果均是正确的。

 

三、SM3

代码:

mysm3.c

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
void tDigest()
{
    unsigned char sm3_value[EVP_MAX_MD_SIZE];   //保存输出的摘要值的数组
    int sm3_len, i;
    EVP_MD_CTX *sm3ctx;                         //EVP消息摘要结构体
    sm3ctx = EVP_MD_CTX_new();//调用函数初始化
    char msg1[] = "Test Message1";              //待计算摘要的消息1
    char msg2[] = "Test Message2";              //待计算摘要的消息2
     
    EVP_MD_CTX_init(sm3ctx);                    //初始化摘要结构体
    EVP_DigestInit_ex(sm3ctx, EVP_sm3(), NULL); //设置摘要算法和密码算法引擎,这里密码算法使用sm3,算法引擎使用OpenSSL默认引擎即软算法
    EVP_DigestUpdate(sm3ctx, msg1, strlen(msg1));//调用摘要UpDate计算msg1的摘要
    EVP_DigestUpdate(sm3ctx, msg2, strlen(msg2));//调用摘要UpDate计算msg2的摘要 
    EVP_DigestFinal_ex(sm3ctx, sm3_value, &sm3_len);//摘要结束,输出摘要值   
    EVP_MD_CTX_reset(sm3ctx);                       //释放内存
     
    printf("原始数据%s和%s的摘要值为:\n",msg1,msg2);
    for(i = 0; i < sm3_len; i++)
    {
        printf("0x%02x ", sm3_value[i]);
    }
    printf("\n");
}
int main()
{
    OpenSSL_add_all_algorithms();
    tDigest();
    return 0;
}

初始化函数EVP_MD_CTX_init
函数功能:初始化一个 EVP_MD_CTX 结构体。只有调用该函数初始化后,EVP_MD_CTX结构体才能在其他函数中调用。
函数定义:
void EVP_MD_CTxinit(EVP MD CTX *ctx):

在ubuntu下实现:

执行命令:

gcc -o mysm3 mysm3.c -lpthread -lcrypto

 在OpenEuler下编译:

在Windows下重现:

 结果均是正确的。

 

四、SM4

代码:

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

void tEVP_Encrypt()
{
unsigned
char key[EVP_MAX_KEY_LENGTH]; //密钥
unsigned char iv[EVP_MAX_KEY_LENGTH];//初始化向量
EVP_CIPHER_CTX* ctx;//EVP算法上下文
unsigned char out[1024];//输出密文缓冲区
int outl;//密文长度
int outltmp;
const char msg="Hello OpenSSL";//待加密的数据
int rv;
int i;
//初始化函数才能用!
ctx = EVP_CIPHER_CTX_new();
//设置key和iv(可以采用随机数和可以是用户输入)
for(i=0;i<24;i++)
{
key[i]
=i;
}
for(i=0;i<8;i++)
{
iv[i]
=i;
}
//初始化密码算法结构体
EVP_CIPHER_CTX_init(ctx);
//设置算法和密钥以
rv = EVP_EncryptInit_ex(ctx,EVP_sm4_cbc(),NULL,key,iv);
if(rv!=1)
{
printf(
"Err\n");
return;
}
//数据加密
rv = EVP_EncryptUpdate(ctx,out,&outl,(const unsigned char
)msg,strlen(msg));
if(rv!=1)
{
printf(
"Err\n");
return;
}
//结束数据加密,把剩余数据输出。
rv = EVP_EncryptFinal_ex(ctx,out+outl,&outltmp);
if(rv!=1)
{
printf(
"Err\n");
return;
}
outl
= outl +outltmp;
printf(
"原文为:%s\n",msg);
//打印输出密文
printf("密文长度:%d\n密文数据:\n",outl);
for(i=0;i<outl;i++)
{
printf(
"0x%02x ",out[i]);
}
printf(
"\n");

}

int main()
{

OpenSSL_add_all_algorithms();
tEVP_Encrypt();
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;

}

编译命令:

gcc -o mysm4 mysm4.c -lpthread -lcrypto
./mysm4

Ubuntu下编译:

 在OpenEuler下编译:

在Windows下重现:

 结果均是正确的。

posted @ 2022-04-07 18:33  张灯结彩,潇潇暮雨  阅读(134)  评论(0编辑  收藏  举报