windows下安装openssl
下载地址 http://slproweb.com/products/Win32OpenSSL.html
win32版本
最后捐款可以自行决定
然后配置环境变量就可以使用了】
Linux下安装
下载地址https://www.openssl.org/source/
tar zxvf 万能解压
进入解压目录
$ ./config
$ make
$ make install (使用管理员)
验证是否安装成功
$ openssl version -a
VS中如果出以下错误
配置环境变量即可
测试代码
#include <openssl/md5.h> // md5 头文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void getMD5(const char* str, char* result)
{
MD5_CTX ctx;
// 初始化
MD5_Init(&ctx);
// 添加数据
MD5_Update(&ctx, str, strlen(str));
// 计算结果
unsigned char md[16] = { 0 };
MD5_Final(md, &ctx);
for (int i = 0; i < 16; ++i)
{
sprintf(&result[i * 2], "%02x", md[i]);
}
}
int main()
{
char result[33] = { 0 };
getMD5("hello, md5", result);
printf("md5: %s\n", result);
return 0;
}
输出
md5 value: 33b3bc8e05b4fcc16bd531dd9adac166
Linux下的使用和测试
代码同上
表示l链接错误
gcc md5_test.c -o md5 -lcrypto
执行时报错
原因:是找不到动态库
./a.out: error while loading shared libraries: libcrypto.so.1.1: cannot open shared object file: No such file or directory
解决方案
查找库
find /usr/local/ -name libcrypto.so
将上面的路径添加到配置文件
vim /etc/ld.so.conf
执行命令(使配置文件生效)
ldconfig
OPENSSL_Uplink no OPENSSL_Applink 错误
写出密钥时的错误及解决方案
Applink()函数不属于openssl的dll内部函数的一部分(通过dll分析器看出这个函数不存在), 所以必须把applink.c文件应用程序的一部分编译.
解决方案
extern “C”
{
#include <openssl/applink.c> // 编译时,将applink.c包含到项目中,参与编译。
};