首页 > 其他分享 >openssl 静态编译 win7+vs2010

openssl 静态编译 win7+vs2010

时间:2023-01-07 14:58:23浏览次数:45  
标签:NULL lib openssl vs2010 win7 char include

本文写于2013年1月,此时距离win7停止延长支持已经3年整了。

1. 所以我准备了虚拟机Win7,win7的iso下载地址 :

注册一个微软账号登录到  《登录到我的订阅》

 visualstudio.microsoft.com/zh-hans/msdn-platforms/

 

 

 

 

然后搜索win7,和VS2010并下载安装。

2. 在win7里安装perl:https://strawberryperl.com/ 

3. 下载openssl1.1.1s:  https://github.com/openssl/openssl/tags

4. 接下来 参考 blog.csdn.net/greless/article/details/115657319 

   a. 用管理员模式 打开vs2010 command prompt, 并进入openssl的源码目录
   b. perl configure VC-WIN32 no-asm --prefix="openssl的安装目录,比如c:/openssl"
   c. nmake
   d. nmake test
   e. nmake install
   f. 在openssl的源码目录里找到 libcrypto_static.lib   libssl_static.lib 并copy到  "openssl的安装目录,比如c:/openssl" 的lib文件夹里,原本的libcrypto.lib   libssl.lib只是动态符号库。

5. 调用openssl的verify代码示例:

#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/sha.h> 
#include <openssl/crypto.h>

using namespace std;

#pragma comment(lib, "libcrypto_static.lib")
#pragma comment(lib, "libssl_static.lib")
#pragma comment(lib, "ws2_32.lib")

int main()
{
    FILE *fp = NULL;
    RSA *pubRsa = NULL;
    unsigned char buf[128] = { 0 };
    int nOutLen = sizeof(buf);

    //char * p_KeyPath = "D:/PublicKey.pem";
    char * p_KeyPath = "C:/Users/win7/Desktop/PublicKey.pem";
    printf("PublicKeyPath[%s]\n", p_KeyPath);

    fp = fopen(p_KeyPath, "r");

    /*  open pem file */
    if (NULL == fp)
    {
        printf("fopen[%s] \n", p_KeyPath);
        return NULL;
    }
    
    // read public key
    pubRsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
    if (NULL == pubRsa)
    {
        printf("PEM_read_RSA_PUBKEY error\n");
        fclose(fp);
        return NULL;
    }
    fclose(fp);

    FILE *fpkey = NULL;
    fpkey = fopen("C:/Users/win7/Desktop/aaaaaaaaaaaaaaaaa.key", "rb");

    fread(buf, nOutLen, 1, fpkey);
    if(NULL != fpkey)
        fclose(fpkey);

    char * message = "aaaaaaaaaaaaaaaaa";
    unsigned char md[SHA256_DIGEST_LENGTH];
    SHA256((unsigned char *)message, strlen(message), md);

    int nRet = RSA_verify(NID_sha256, md, SHA256_DIGEST_LENGTH, buf, nOutLen, pubRsa);
    if (nRet != 1)
    {
        printf("RSA_verify err !!! \n");
    }
    printf("RSA_verify Success !!! \n");


    return 0;
}

编译的时候会报链接错误: 要忽略libcmt.lib, libcmtd.lib,  libcmtd.lib是debug模式用的。

 

 当然了,也要配置 include directory,library directory

 

 最后的忠告: 有时候浪费点时间去下载ISO,安装虚拟机可能会更节约时间。

 

   

 

标签:NULL,lib,openssl,vs2010,win7,char,include
From: https://www.cnblogs.com/crazyghostvon/p/17032610.html

相关文章