1. 程式人生 > 其它 >樹莓派openEuler安裝使用openSSL

樹莓派openEuler安裝使用openSSL

openssl和環境安裝配置

安裝openssl

首先檢視是否已經安裝openssl

已經安裝了ssl,但還是自己重新安裝編譯最新版吧。

編譯安裝

要下載網上資源使用wget,先進行安裝。

在openssl官網上看到最新版本openssl已經是1.1.1l,進行下載和安裝。

解壓包用tar -zxvf openssl-1.1.1k.tar.gz -C rocopensslsrc

開始make


隨後make testmake install。完成後,整個原始碼就編譯完成了。


configmakemake testmake install的理解:
對於網上開源專案,我們可以自己編譯下載好的原始碼。
config

就是對於現在本機的系統進行判斷,設定安裝路徑等安裝引數,同時檢查編譯環境是否齊全。最後生成makefile檔案來進行後續編譯過程。
make就是進行編譯構建的操作,將原始碼編譯成為可執行檔案。
make test就是測試上一步make構建的可執行檔案和功能是否有問題。
make install則是將編譯好的檔案進行本地化的安裝。
編譯安裝完成後檢視版本

出現問題:庫的版本還是舊版。
我猜測出現問題的原因是設定好的預設庫的路徑應該是linux預設的庫的路徑,而openEuler預設安裝的openssl的版本就是舊版的,所以庫也就是舊版的,應該要修改安裝好的新openssl的庫路徑。

修改庫路徑

嘗試使用環境變數的方法進行設定。
使用以前學習到的動態庫的知識,使用export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/home/lzh/lzhopenssl/lib新增一條快捷的變數,設定為預設的庫。

成功更改了庫,現在openssl本身和庫都是新的了。
但這樣存在重啟後失效的問題,我參考了這篇部落格:
https://blog.csdn.net/qq_19004627/article/details/79090052
新增到.bashrc檔案中,實現啟動時自動設定,就完成了。從此lzhopenssl中的ssl就使用的都是新的庫了。

同時,將PATH變數也修改一下,將自己安裝的openssl的bin路徑加在前面,自動執行的openssl的版本就是新版本了,這樣如果要使用系統自帶舊版本的話,刪除這兩個變數就可以。

重啟後:

openssl測試與使用

測試openssl

使用openssl help檢視幫助

使用openssl help xxsubcmdopenssl xxsubcmd --help檢視子命令的幫助
下圖為檢視rsa的幫助

使用管道計算摘要:如計算besti20191320的sm3摘要就可以使用命令echo "besti20191320" | openssl sm3來完成。
效果如圖:

計算某檔案的摘要:使用openssl sm3 filename完成。

通過使用help的方法,可以學習和實現很多功能,如
生成隨機數作為金鑰

sm4使用方法

程式設計測試openssl

使用程式碼進行測試

#include <stdio.h>
#include <openssl/evp.h>

int main(){
	
    OpenSSL_add_all_algorithms();
	
    return 0;
}


函式OpenSSL_add_all_algorithms()的作用是將所有演算法載入。使用./to;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.\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("檔案\"Test.dat\" 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("對檔案\"Test.dat\" 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;
}
測試成功,截圖如下: