OpenSSL3.0是在去年升级的,而且还改的很狠,不怎么兼容之前的版本,但是网上能找到的教程又比较早,就很痛苦,好歹摸索出来能跑不报错了,先记一下,可能有多余操作。
1 先安装visual studio 2019和OpenSSL3.0
OpenSSL3.0能正常在控制台进行加解密。
我自己安装时候随手记的内容
https://blog.csdn.net/mandiheyanyu/article/details/122982302
2 新建一个c++项目
3 更改项目属性
VC++ 目录
更改“包含目录”,添加OpenSSL的include路径;
更改“库目录”,添加OpenSSL的lib路径。
修改C/C++→常规→附加包含目录
修改 链接器→常规→附加库目录
链接器→输入→附加依赖项
加入
libssl.lib;libcrypto.lib;
将这俩dll拷入到项目中
这里我实在不清楚到底应该拷到哪里
E:\vcsave\OpenSSLaes\Debug
E:\vcsave\OpenSSLaes\OpenSSLaes
E:\vcsave\OpenSSLaes\OpenSSLaes\Debug
这仨路径下我都放了一份
安装参考的链接
Visual Studio使用openssl库环境搭建
https://blog.csdn.net/qq_19734597/article/details/100563055
关于“在Visual studio 2017中出现:无法打开源文件”的问题!
https://blog.csdn.net/LKR0325/article/details/80144149
其实我不止看了这两份,但是别的我给关掉了,打开过的网页太多了就不想再去找了。
4 运行测试
这份代码也忘了是从哪里找的了,我是真的从网上找不到直接就能跑的完整代码,我也看不懂这是哪种加密,反正它用了openssl的文件,而且能运行不报错
extern "C"
{
#include <openssl/applink.c>
};
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
int main(void)
{
EVP_MD_CTX* ctx = NULL;
EVP_MD* sha256 = NULL;
const unsigned char msg[] = {
0x00, 0x01, 0x02, 0x03
};
unsigned int len = 0;
unsigned char* outdigest = NULL;
/* Create a context for the digest operation */
ctx = EVP_MD_CTX_new();
if (ctx == NULL)
goto err;
/*
* Fetch the SHA256 algorithm implementation for doing the digest. We're
* using the "default" library context here (first NULL parameter), and
* we're not supplying any particular search criteria for our SHA256
* implementation (second NULL parameter). Any SHA256 implementation will
* do.
*/
sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
if (sha256 == NULL)
goto err;
/* Initialise the digest operation */
if (!EVP_DigestInit_ex(ctx, sha256, NULL))
goto err;
/*
* Pass the message to be digested. This can be passed in over multiple
* EVP_DigestUpdate calls if necessary
*/
if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
goto err;
/* Allocate the output buffer */
outdigest = (unsigned char*)OPENSSL_malloc(EVP_MD_get_size(sha256));
if (outdigest == NULL)
goto err;
/* Now calculate the digest itself */
if (!EVP_DigestFinal_ex(ctx, outdigest, &len))
goto err;
/* Print out the digest result */
BIO_dump_fp(stdout, outdigest, len);
err:
/* Clean up all the resources we allocated */
OPENSSL_free(outdigest);
EVP_MD_free(sha256);
EVP_MD_CTX_free(ctx);
}
5 相关教程链接
OpenSSL3.0深入学习
https://blog.csdn.net/include_he/category_11612114.html
openssl3.0 加密算法库编程精要
https://www.cnblogs.com/huowenjie/