Visual Studio配置OpenSSL
Posted oilover
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Visual Studio配置OpenSSL相关的知识,希望对你有一定的参考价值。
OpenSSL 是一个密码学算法库。
我的安装在F:\\Program Files\\OpenSSL-Win64\\
VS新建工程,菜单Project->Properties,
Configuration Manager,这样设置:
在C/C++的General->Additional Include Directories设置为:F:\\Program Files\\OpenSSL-Win64\\include
在Linker的All Options->Additional Dependencies设置为libeay32.lib;ssleay32.lib
Additional Library Directories设置为F:\\Program Files\\OpenSSL-Win64\\lib
实现SHA256算法的代码如下:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <openssl/hmac.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <assert.h>
typedef unsigned long long uint;
#define prt(k) cout<<#k" = "<<k<<endl
typedef long long LL;
using namespace std;
typedef unsigned long long uint;
int HmacEncode(const char * key, unsigned int key_length,
const char * input, unsigned int input_length,
unsigned char * &output, unsigned int &output_length)
const EVP_MD * engine = NULL;
engine = EVP_sha256();
output = (unsigned char*)malloc(EVP_MAX_MD_SIZE);
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, strlen(key), engine, NULL);
HMAC_Update(&ctx, (unsigned char*)input, strlen(input)); // input is OK; &input is WRONG !!!
HMAC_Final(&ctx, output, &output_length);
HMAC_CTX_cleanup(&ctx);
return 0;
void HMAC(string message, string key) //len = 32 * 8 = 256
unsigned char *mac = NULL;; unsigned int out_len;
HmacEncode(key.c_str(), key.length(), message.c_str(), message.length(), mac, out_len);
for (int i = 0; i < out_len; i++)
printf("%5d", (int)mac[i]);
cout << endl;
int main()
///std::string str, key;
char str[555], key[888];
while (scanf("%s%s", str, key) == 2)
HMAC(string(str), string(key));
return 0;
参考:
http://blog.csdn.net/yasi_xi/article/details/9066003
以上是关于Visual Studio配置OpenSSL的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Visual Studio 2017 在 Windows 上构建 OpenSSL?
openssl 包含 Visual Studio 2012 无法识别的路径
在 Windows下用 Visual Studio 编译 OpenSSL 1.1.0
如何使用 Visual Studio 2019 和 c++ 链接 OpenSSL 库?