解析OpenSSL1.1.1 centos7安装编译aes的c++调用
作者:禾烟雨
这篇文章主要介绍了OpenSSL1.1.1 centos7安装编译aes的c++调用,实现方法也很简单,主要是在该文档内加入openssl的lib路径,感兴趣的朋友跟随小编一起看看吧
装这个主要是拿来和我自己写的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
运行效果如图

到此这篇关于OpenSSL1.1.1 centos7安装编译aes的c++调用的文章就介绍到这了,更多相关centos7 安装编译OpenSSL1.1.1内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
