實驗一 密碼引擎-2-OpenEuler-OpenSSL測試(Linux與OpenEuler)
阿新 • • 發佈:2022-04-13
實驗一 密碼引擎-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