Visual C++ 2017 集成 OpenSSL(1.1.0h)
下载安装环境
- 安装 Visual Studio 2017(自行解决)
- 下载并安装 ActivePerl (https://www.activestate.com,需要VPN,Orz)
- 下载 OpenSSL v1.1.0,Stable版本(https://www.openssl.org)
- 安装 dmake(ppm install dmake,需要VPN,Orz,否则会报 ppm install failed: Can't uncompress)
编译 OpenSSL(32位)静态库
- 进入“适用于 VS 2017 的 x86 本机工具命令提示”
- cd /d D:\SDK\openssl-1.1.0h
- perl Configure VC-WIN32 no-shared no-asm --prefix="D:\SDK\openssl-1.1.0h\build\x86" --openssldir="D:\SDK\openssl-1.1.0h\build\x86\ssl"
- nmake(编译)
- nmake install(生成目录、文件)
- 可选,nmake test(测试,应看到“Result: PASS”)
- nmake clean(清理)
编译 OpenSSL(64位)静态库
- 进入“适用于 VS 2017 的 x64 本机工具命令提示”(注意:如果链接用32位命令行工具编译的lib会报:库计算机类型“x86”与目标计算机类型“x64”冲突)
- cd /d D:\SDK\openssl-1.1.0h
- perl Configure VC-WIN64A no-shared no-asm --prefix="D:\SDK\openssl-1.1.0h\build\x64" --openssldir="D:\SDK\openssl-1.1.0h\build\x64\ssl"
- nmake(编译)
- nmake install(生成目录、文件)
- 可选,nmake test(测试,应看到“Result: PASS”)
- nmake clean(清理)
Visual C++ 2017 集成
-
包含目录:D:\SDK\openssl-1.1.0h\build\x86\include
- 注意:不要使用D:\SDK\openssl-1.1.0h\include,否则会报 “openssl/opensslconf.h”: No such file or directory
-
库目录:D:\SDK\openssl-1.1.0h\build\x86\lib
-
附加依赖项:libcrypto.lib;libssl.lib;
- 注意:1.1.0版本,Windows下还需要附加2个lib:crypt32.lib;ws2_32.lib; 否则会报链接错误(Windows版OpenSSL可能使用了Windows的库)
-
示例代码(注意:1.1.0相比0.9.8有一些区别):
#include <openssl/rsa.h> int main() { printf("TEST OpenSSL\n"); unsigned char plain[4] = { 0xaa,0xbb,0xcc,0xdd }; unsigned char cipher[128] = { 0 }; unsigned char plain_2[4] = { 0 }; int ret; RSA * rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL); RSA_print_fp(stdout, rsa, 8); ret = RSA_public_encrypt(sizeof(plain), plain, cipher, rsa, RSA_PKCS1_PADDING); printf("RSA_public_encrypt = %d\n", ret); printf("cipher:\n"); for (int i = 0; i < ret; i++) printf("%02x ", cipher[i]); printf("\n"); ret = RSA_private_decrypt(ret, cipher, plain_2, rsa, RSA_PKCS1_PADDING); printf("RSA_private_decrypt = %d\n", ret); printf("plain:\n"); for (int i = 0; i < ret; i++) printf("%02x ", plain[i]); printf("\n"); RSA_free(rsa); printf("\n"); return 0; }
网友评论