OpenSSL的VC编程 - MD5
来源:互联网 发布:linux log4j日志乱码 编辑:程序博客网 时间:2024/06/05 00:38
由于诸多原因,前几天抽了点时间在网上找了一些OpenSSL的资料。网上的资料鱼龙混杂,我看原创的并不多。部分资料是讲OpenSSL命令行的操作,另一部分(基本上转载的那样一两篇)是Linux下的OpenSSL的API。
忍不住吐槽几句了。我见的90%的文章,都在说怎么怎么编译安装OpenSSL,然后使用的话就没下文了。难道你们的水平就仅限于安装它?安装软件不是小学初中就应该知道的东西吗?我看大部分人根本就是转载,就是充斥着转载的垃圾站。转载就算了,转载完了也不看看是否完整,是否可行。源码缺胳膊少腿,你特么这就是在祸害下一代。
好吧,回到正题。OpenSSL是一个开源的库,封装了常用的(基本上是所有的)加密、解密的算法,让使用者不再苦恼于算法的编写(不是每个人都有数学家的大脑),直接运用库里的函数,就能简单地对数据进行加密、解密。
我一直是喜欢Windows系统的,最重要的原因就是Visual Studio。它让程序的编写变的直观,容易百倍。OpenSSL的编译网上文章比较多,不过我今天用到的是一个叫“Win32 OpenSSL”的第三方版本。它就是一个编译好的OpenSSL(无命令行功能),而且其中有专为VC编译的库(Lib以及Dll),省去了我们很多麻烦。
于是结合他们,我们将可以在5分钟的时间里,写出一个MD5加密的程序。
1.下载并安装Win32 OpenSSL。安装目录中有以下几个文件夹:bin、exp、include、lib,大家应该不陌生,猜都知道该怎么用。不过还是先看看文档,打开目录下的“OpenSSLhelp.chm”。 其中有这么一段可以参考:
OpenSSL is designed to build easily under Microsoft Visual C++. However, the requirement that you go and obtain the 25MB Win98 DDK and have the latest service pack (SP5 is 120MB) installed can prove to be a hinderance to some people (particularly those with modems).So, the installation of the Win32 OpenSSL binaries is a fairly simple process (similar to the Borland C++ Builder process).
The first thing to do (assuming a default installation of 'C:\OpenSSL') is to go to 'C:\OpenSSL\lib\VC' and copy all of the files to your Visual C++ 'lib' directory. This directory is sometimes located in a somewhat cryptic location such as 'C:\Program Files\Microsoft Visual Studio\VC98\lib' or 'C:\Program Files\Microsoft Visual C++\lib'.
Next, copy everything in the 'C:\OpenSSL\include' directory to your Visual C++ 'include' directory.
That's it! You are ready to go write OpenSSL-capable code!
不过我没按它的来做。
2.新建工程,选择工程 - 属性 - 配置属性 - VC++目录:
3.开始写代码。首先
1
#include <openssl/md5.h>
2
3
#pragma comment(lib, "ssleay32MDd.lib")
4
#pragma comment(lib, "libeay32MDd.lib")
md5的头文件,以及OpenSSL的静态库。这里说明一下,什么编译模式包含什么静态库。比如默认的“多线程调试DLL”,就是ssleay32MDd.lib。写过Windows程序的都应该清楚。编译模式在这里改:
4.封装函数:
01
int
MD5(
const
char
* data,
char
* buf)
02
{
03
MD5_CTX ctx;
04
unsigned
char
md[16];
05
char
tmp[3]={
'\0'
};
06
int
i;
07
08
MD5_Init(&ctx);
09
MD5_Update(&ctx,data,
strlen
(data));
10
MD5_Final(md,&ctx);
11
12
for
( i=0; i<16; i++ ){
13
sprintf
(tmp,
"%02x"
,md[i]);
14
strcat
(buf,tmp);
15
}
16
return
0;
17
}
MD5_Final函数,用来取加密好的MD5散列。第一个参数是散列存放的缓冲区,第二个参数是MD5_CTX结构。取到MD5散列以后,最后一个for循环将它转换成十六进制字符串,这里就不多说了。
最后得到一个长度为32的字符串,保存在buf中。
5.写一个main函数测试结果:
1
int
_tmain(
int
argc, _TCHAR* argv[])
2
{
3
char
szBuf[1024] = {0}, szMd5[50] = {0};
4
gets_s(szBuf, _countof(szBuf));
5
MD5(szBuf, szMd5);
6
printf
(
"%s\n"
,szMd5);
7
return
0;
8
}
成功将https://www.leavesongs.com转换成了MD5散列。
- OpenSSL的VC编程 - MD5
- openssl speed md5 测试md5的performance
- VC++6.0环境下利用openssl编程入门(以计算md5值为例)的全解析------openssl库直接给大家弄好, 省得麻烦大家去编译
- 使用openssl的md5库
- 使用openssl的md5库
- 使用openssl的md5库
- 使用openssl的md5库
- 使用openssl的md5库
- openssl的Md5计算笔记
- 使用openssl的md5库
- 使用openssl的md5库
- openssl md5
- Openssl-MD5
- Openssl-MD5
- Linux下使用openssl的MD5加密
- OPENSSL产生字符串的MD5码
- VC环境OpenSSL安装以及编程过程
- VC环境OpenSSL安装以及编程过程
- gitHub关联本地
- 结合码率和pcr来发送组播数据
- android开发环境搭建 安装说明 mac和windwos Eclipse adt
- 前端页面提交中文到java时,乱码解决
- MD5 消息摘要算法
- OpenSSL的VC编程 - MD5
- linux内核中内存分配调用API-引起的一些思考
- 清除svn 脚本
- iOS接口回调模式
- nodejs ssh2基本功能封装,实现上传、下载文件以及文件夹
- jqgrid常用格式
- Java集合Map接口与Map.Entry学习
- Mybatis之XML配置文件之properties,settings,typeAliases,typeHandlers
- 【31.58%】【codeforces 682D】Alyona and Strings