系统相关
首页 > 系统相关> > 实验一 密码引擎-2-OpenEuler-OpenSSL测试(Linux与OpenEuler)

实验一 密码引擎-2-OpenEuler-OpenSSL测试(Linux与OpenEuler)

作者:互联网

实验一 密码引擎-2-OpenEuler-OpenSSL测试(Linux与OpenEuler)

目录

在Ubuntu编写代码测试OpenSSL功能,包含Base64,SM2,SM3,SM4算法的调用,然后在OpenEuler中重现
提交代码链接和运行结果截图

加分项:在Windows中重现

Base64调用

Linux

编写代码base64.cpp

#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")

#include  <cstring>
#include <openssl/evp.h>  
#include <openssl/bio.h>  
#include <openssl/buffer.h>    
#include <string>  
#include <iostream> 

using namespace std;

char* base64Encode(const char* buffer, int length, bool newLine);
char* base64Decode(char* input, int length, bool newLine);

int main(int argc, char* argv[])
{
    bool newLine = false;
    string input = "20191227!";

    char* encode = base64Encode(input.c_str(), input.length(), newLine);
    char* decode = base64Decode(encode, strlen(encode), newLine);

    cout << "Base64 Encoded : " << encode << endl;
    cout << "Base64 Decoded : " << decode << endl;

    cin.get();
}

// base64 编码
char* base64Encode(const char* buffer, int length, bool newLine)
{
    BIO* bmem = NULL;
    BIO* b64 = NULL;
    BUF_MEM* bptr;

    b64 = BIO_new(BIO_f_base64());
    if (!newLine) {
        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    }
    bmem = BIO_new(BIO_s_mem());
    b64 = BIO_push(b64, bmem);
    BIO_write(b64, buffer, length);
    BIO_flush(b64);
    BIO_get_mem_ptr(b64, &bptr);
    BIO_set_close(b64, BIO_NOCLOSE);

    char* buff = (char*)malloc(bptr->length + 1);
    memcpy(buff, bptr->data, bptr->length);
    buff[bptr->length] = 0;
    BIO_free_all(b64);

    return buff;
}

// base64 解码
char* base64Decode(char* input, int length, bool newLine)
{
    BIO* b64 = NULL;
    BIO* bmem = NULL;
    char* buffer = (char*)malloc(length);
    memset(buffer, 0, length);
    b64 = BIO_new(BIO_f_base64());
    if (!newLine) {
        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    }
    bmem = BIO_new_mem_buf(input, length);
    bmem = BIO_push(b64, bmem);
    BIO_read(bmem, buffer, length);
    BIO_free_all(bmem);

    return buffer;
}

使用g++编译运行,调用openssl,需要加上-lcrypto

g++ base64.cpp -o 20191227base64 -lcrypto

成功输出“hello world!”的base64编码

在openeuler中复现

购买云服务器
yum安装gcc g++

sudo yum install gcc-c++

open自带的openssl会报错

参考https://www.cnblogs.com/rocedu/p/14617763.html安装最新的

编译运行

g++ -o 20191227base64 base64.cpp -I /root/rocopenssl/include -L /root/rocopenssl/lib -lcrypto -lpthread
./20191227base64

成功输出

SM2调用

Linux

编译运行

gcc *.c -o 20191227sm2 -lcrypto

在openeuler中复现

编译运行

gcc -o 20191227sm2 *.c -I /root/rocopenssl/include -L /root/rocopenssl/lib -lcrypto -lpthread

SM3调用

Linux

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
void tDigest()
{
	unsigned char sm3_value[EVP_MAX_MD_SIZE];	//保存输出的摘要值的数组
	unsigned 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);	//设置摘要算法和密码算法引擎,这里密码算法使用MD5,算法引擎使用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;
}

编译运行

 gcc mysm3.c -lcrypto

在openeuler中复现

gcc mysm3.c -o 20191227sm3 -I /root/rocopenssl/include -L /root/rocopenssl/lib -lcrypto -lpthread

SM4调用

gcc mysm4.c -o 20191227sm4 -lcrypto
#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();
    return 0;
}

编译运行:

在openeuler中复现

gcc mysm4.c -o 20191227sm4 -I /root/rocopenssl/include -L /root/rocopenssl/lib -lcrypto -lpthread

标签:BIO,int,OpenSSL,char,length,Linux,EVP,OpenEuler,include
来源: https://www.cnblogs.com/GanNy/p/16140041.html