Cryptopp库的使用.doc

上传人:人*** 文档编号:542213299 上传时间:2024-02-27 格式:DOC 页数:5 大小:39.50KB
返回 下载 相关 举报
Cryptopp库的使用.doc_第1页
第1页 / 共5页
Cryptopp库的使用.doc_第2页
第2页 / 共5页
Cryptopp库的使用.doc_第3页
第3页 / 共5页
Cryptopp库的使用.doc_第4页
第4页 / 共5页
Cryptopp库的使用.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《Cryptopp库的使用.doc》由会员分享,可在线阅读,更多相关《Cryptopp库的使用.doc(5页珍藏版)》请在金锄头文库上搜索。

1、Cryptopp库的使用 1. 安装及配置1.1. 编译下载下来,将工程导入到vs中,打开,有4个工程。全部编译一遍。如果编译有问题的话,也可以只编译cryptlib工程。至于另外几个工程,不是很清楚。我试了只编译cryptlib的工程,貌似也可以。编译完了后,在“.Win32OutputDebug”目录下可以找到cryptlib.lib文件。1.2. 编译选项问题这里有一点值得提一下的是,编译选项的问题。Cryptopp工程默认的编译选项是/MTd。所以,如果你没有改它的话,把这样编译后的lib用到你自己的工程中去的话,你的工程的编译选项也必须调成/MTd,否则链接的时候可能会报错。或者,你

2、可以选择,按照你工程的编译选项的需要(如/MDd),在编译Cryptlib的时候,先调整编译选项,再把编译后的lib引入到工程中,这样就没问题了。调整编译选项:工程-右键-属性-Configuration Properties-C/C+-Code Generation : Runtime Library。1.3. 引入工程1. 引入相关头文件 在你的工程目录,右键 - 属性 - Configuration Properties - C/C+ - General :Additional Include Directories,填入Cryptopp库的源代码所在目录。如:C:DevCommoncr

3、yptopp5611. 引入lib库 1).在你的工程目录,右键 - 属性 - Configuration Properties - Linker - General :Additional Library Directories,填入cryptlib.lib所在的目录。2).在下面的 Linker - Input : Additional Dependencies,填入:cryptlib.lib。3).在Linker - Command Line : Additional options中填入:cryptlib.lib。上面的 2) 或者 3) 任选一个都可以。2. Hello World配

4、置完毕后,写个Hello World测试一下,看能不能编译通过。#include #include using namespace std;using namespace CryptoPP;int main() cout hello crypto+ endl; cout Aes block size is AES:BLOCKSIZE endl; return 0;3. 二进制数组编码3.1. 将一个byte数组按16进制编码后输出 string encoded; StringSource ss(m, sizeof(m), true, new HexEncoder(new StringSink(

5、encoded) ); cout encoded endl;3.2. 将一个byte数组按base64编码后输出 string encoded; StringSource s(m, sizeof(m), true, new Base64Encoder(new StringSink(encoded) ); cout encoded endl;4. 哈稀算法签名4.1. MD54.1.1. MD5对字符串签名bool md5(const string &src, string &digest) byte m16; int len = src.length(); byte *msg = new by

6、telen + 1; std:stringstream stream; stream msg; Weak:MD5 md5; md5.CalculateDigest(m, msg, len); /此一句等于下面两句 /md5.Update(msg, len); /md5.Final(m); StringSource ss(m, sizeof(m), true, new HexEncoder(new StringSink(digest) ); delete msg; return true;注:1、CalculateDigest(m, msg, len) 方法等效于下面两个语句md5.Update

7、(msg, len);md5.Final(m);2、关于数组byte m ,用于接收数字签名数组m的长度16,这个是怎么来的呢?查看API帮助文档可以发现,其实这个是由方法DigestSize()定义的,同时一个静态的常量DIGESTSIZE也定义了该长度。使用的时候,可以将该值打印出来:cout Weak:MD5:DIGESTSIZE;或者:bool md5(const string &src, string &digest) Weak:MD5 md5; StringSource(src, true, new HashFilter(md5, new HexEncoder(new String

8、Sink(digest) ) ); return true;4.1.2. MD5对文件产生签名bool md5file(const string &filePath, string &digest) std:locale loc1 = std:locale:global(std:locale(.936); /解决中文文件名问题 Weak:MD5 md5; FileSource f(filePath.c_str(), true, new HashFilter( md5, new HexEncoder(new StringSink(digest) ) ); std:locale:global(std:locale(loc1); return true;4.2. SHA-14.2.1. SHA-1对字符串签名bool sha1(const string &src, string &digest) SHA1 sha1; StringSource(src, true, new HashFilter(sha1, new HexEncoder(new StringSink(digest) ) ); return true;参考:http:/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号