解析OpenSSL1.1.1?centos7安裝編譯aes的c++調(diào)用
裝這個主要是拿來和我自己寫的aes代碼做驗(yàn)證的,但是其實(shí)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
在該文檔內(nèi)加入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); //把一個字符轉(zhuǎn)變成整型 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; // 設(shè)置加密密鑰 AES_set_encrypt_key(aes_keybuf, 128, &aeskey); // 加密 AES_encrypt(strr,buf2,&aeskey); printf("輸出加密結(jié)果:\n"); printf("%x ",buf2[i]); printf("\n"); //設(shè)置解密密鑰 AES_set_decrypt_key(aes_keybuf, 128, &aeskey); //解密 AES_decrypt(buf2, buf3, &aeskey); buf3[16]='\0'; printf("輸出解密結(jié)果:\n"); printf("%s\n",buf3); return 0;
g++ test.cpp -o test -L/usr/local/openssl/lib -lcrypto ./test
運(yùn)行效果如圖
到此這篇關(guān)于OpenSSL1.1.1 centos7安裝編譯aes的c++調(diào)用的文章就介紹到這了,更多相關(guān)centos7 安裝編譯OpenSSL1.1.1內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++ 原生API實(shí)現(xiàn)線程池的方法
線程池,簡單來說就是有一堆已經(jīng)創(chuàng)建好的線程,接下來通過本文給大家介紹C/C++ 原生API實(shí)現(xiàn)線程池的方法,感興趣的朋友跟隨小編一起看看吧2021-11-11解析bitmap處理海量數(shù)據(jù)及其實(shí)現(xiàn)方法分析
本篇文章是對bitmap處理海量數(shù)據(jù)及其實(shí)現(xiàn)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++利用MySQL API連接和操作數(shù)據(jù)庫實(shí)例詳解
這篇文章主要介紹了C++利用MySQL API連接和操作數(shù)據(jù)庫實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01