centos7 安装编译OpenSSL1.1.1

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

centos7 安装编译OpenSSL1.1.1

禾烟雨   2022-05-28 我要评论

装这个主要是拿来和我自己写的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

运行效果如图

在这里插入图片描述

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们