Windows编译OpenSSL

29 阅读3分钟

安装Perl

进入Perl官网,点击登录,然后我们选择以github登录

image.png

image.png

image.png

步骤一

下载命令行工具后,双击运行安装。安装过程如需确认根据命令行提示进行输入。

步骤二

接着我们按照步骤二执行命令执行state checkout postkarte/Perl-5.38.2-Windows .

PS C:\Users\gillbert> state checkout postkarte/Perl-5.38.2-Windows .
Checking out project: postkarte/Perl-5.38.2-Windows

• Resolving Dependencies ✔ Done

  Setting up the following dependencies:
  ├─ Utilities@1.00 (93 sub-dependencies)
  └─ perl@5.38.2

█ Sourcing Runtime

Downloading                                95/95
Unpacking                                  95/95
Installing                                 95/95
✔ All dependencies have been installed and verified

Checked out project postkarte/Perl-5.38.2-Windows, located at C:\Users\gillbert.
For editors and other tooling use the executables at: C:\Users\gillbert\AppData\Local\activestate\cache\3e75a2d8\exec.

步骤三

接着我们按照步骤三执行命令执行state shell Perl-5.38.2-Windows

PS C:\Users\gillbert> state shell Perl-5.38.2-Windows
Dependencies for your project are already configured and installed.
Opening shell for project postkarte/Perl-5.38.2-Windows, located at C:\Users\gillbert.
This shell is operating inside a virtual environment.
For editors and other tooling use the executables at: C:\Users\gillbert\AppData\Local\activestate\cache\3e75a2d8\exec.

✔ Project "postkarte/Perl-5.38.2-Windows" Has Been Activated
Warning: PowerShell is not yet officially supported.
Locale 'Chinese (Simplified)_China.936' is unsupported, and may crash the interpreter.
Quick Start
鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€
鈥?To add a package to your runtime, type "state install <package name>"

鈥?Learn more about how to use the State Tool, type "state learn"

测试查看Perl版本

PS C:\Users\gillbert> perl.exe --version
Locale 'Chinese (Simplified)_China.936' is unsupported, and may crash the interpreter.

This is perl 5, version 38, subversion 2 (v5.38.2) built for MSWin32-x64-multi-thread

Copyright 1987-2023, Larry Wall

Binary build 38.2r4 provided by ActiveState http://www.ActiveState.com
Built Wed Mar 27 06:25:33 2024

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.

安装NASM

下载地址

www.nasm.us/pub/nasm/re…

下载后解压并把目录配置到环境变量中

image.png

编译OpenSSL

OpenSSL源码下载

github.com/openssl/ope…

打开VS工具命令行窗口

我们以管理员身份打开,因为后面编译完成后需要进行安装需要权限。

image.png

进入OpenSSL目录

cd C:\Users\gillbert\code\openssl-3.0.16

image.png

生成Makefile文件

# 加载perl环境
state shell Perl-5.38.2-Windows

# 生成Makefile文件,这里我们生成了64位的,如果要生成32位,需要写VC-WIN32
perl Configure VC-WIN64A

配置成功后提示如下

[postkarte/Perl-5.38.2-Windows] C:\Users\gillbert\code\openssl-3.0.16>perl Configure VC-WIN64A
Locale 'Chinese (Simplified)_China.936' is unsupported, and may crash the interpreter.
Configuring OpenSSL version 3.0.16 for target VC-WIN64A
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Locale 'Chinese (Simplified)_China.936' is unsupported, and may crash the interpreter.
Created makefile.in
Created makefile
Created include\openssl\configuration.h

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL.md file first)      ***
***                                                                ***
**********************************************************************

编译

执行nmake进行编译

nmake

安装

在这一步骤可能会出错,需要我们以管理员权限执行

nmake install

安装成功后我们看到目录下有生成的文件

image.png

image.png

代码测试

创建项目

image.png

在我们的项目中的bin目录下创建一个x64目录,然后将bin目录原来的文件都拷贝进去

image.png

image.png

在我们的项目中的lib目录也是创建一个x64目录,然后将lib目录原来的文件都拷贝进去

image.png

image.png

在我们的项目中创建src目录,然后使用vs studio在其中创建一个项目

image.png

image.png

项目配置

我们指定下项目的头文件和库文件路径

image.png

image.png

image.png

把项目输出目录在修改一下

image.png

image.png

测试代码

#include <iostream>
#include <openssl/rand.h>
#include <time.h>

int main(int argc, char* argv[]) {

    time_t t = time(0);

    unsigned char buf[16] = { 0 };

    int re = RAND_bytes(buf, sizeof(buf));  // 生成16个随机数

    for (int i = 0; i < sizeof(buf); i++)
    {
        std::cout << (int)buf[i] << std::endl;
    }

    return 0;
}

运行结果

image.png

遇到的错误

NASM没有找到

image.png

解决办法

NASM环境变量没配置,需要配置下环境变量