这篇文章主要介绍了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内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:解析OpenSSL1.1.1 centos7安装编译aes的c++调用
基础教程推荐
猜你喜欢
- C++实战之二进制数据处理与封装 2023-05-29
- [c语言-函数]不定量参数 2023-09-08
- 全面了解C语言 static 关键字 2023-03-26
- centos 7 vscode cmake 编译c++工程 2023-09-17
- C语言实现宾馆管理系统课程设计 2023-03-13
- [C语言]二叉搜索树 2023-09-07
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- C++实现ETW进行进程变动监控详解 2023-05-15
- C语言 详解字符串基础 2023-03-27
- 带你深度走入C语言取整以及4种函数 2022-09-17
