日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

解析OpenSSL1.1.1?centos7安裝編譯aes的c++調用_C 語言

作者:禾煙雨 ? 更新時間: 2022-05-27 編程語言

裝這個主要是拿來和我自己寫的aes代碼做驗證的,但是其實OpenSSL能干的事情挺多的。

下載地址

https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz?

tar -zxvf openssl-OpenSSL_1_1_1d.tar.gz
cd openssl-OpenSSL_1_1_1d
sudo mkdir /usr/local/openssl
./config --prefix=/usr/local/openssl
make
sudo make install
sudo mv /usr/bin/openssl /usr/bin/openssl.old
sudo mv /usr/include/openssl /usr/include/openssl.old
sudo ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
sudo ln -s /usr/local/openssl/include/openssl /usr/include/openssl
sudo vim /etc/ld.so.conf

在該文檔內加入openssl的lib路徑

/usr/local/openssl/lib

:wq保存

sudo ldconfig -v
openssl version

測試代碼如下:

//test.cpp
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <cstring>
#include <openssl/aes.h>
using namespace std;

static int getIntFromChar(char c);
//把一個字符轉變成整型
static int getIntFromChar(char c) {
	int result = (int)c;
	return result & 0x000000ff;
}
int main(int argc, char *argv[]){
    unsigned char buf2[16];
    unsigned char buf3[16];
	char str[16];
	unsigned char strr[16];
	int len;
	printf("輸入明文:\n");
	scanf("%s",str);
	len=strlen(str);
	printf("len=%d\n",len);
	
	for(int i=0;i<len;i++){
		strr[i]=getIntFromChar(str[i]);
	}
	unsigned char aes_keybuf[16]; 
	char key[16];
	getchar();
	printf("輸入密鑰:\n");
	scanf("%s",key);
	for(int i=0;i<16;i++){
		aes_keybuf[i]=getIntFromChar(key[i]);
    AES_KEY aeskey;
    // 設置加密密鑰 
	AES_set_encrypt_key(aes_keybuf, 128, &aeskey);
    // 加密
	AES_encrypt(strr,buf2,&aeskey);
	printf("輸出加密結果:\n");
		printf("%x ",buf2[i]);
	printf("\n");	
    //設置解密密鑰
	AES_set_decrypt_key(aes_keybuf, 128, &aeskey);
    //解密
    AES_decrypt(buf2, buf3, &aeskey);
    
    buf3[16]='\0';
    printf("輸出解密結果:\n");
    printf("%s\n",buf3);
    return 0;
g++ test.cpp -o test -L/usr/local/openssl/lib -lcrypto
./test

運行效果如圖

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/mandiheyanyu/article/details/123680012

欄目分類
最近更新