CryptoPP使用介绍
来源:互联网 发布:php网页编辑软件有哪些 编辑:程序博客网 时间:2024/05/01 08:51
Crypto++是个免费的C++加解密类库,由于资格太老、持续更新,最新版本到了CryptoPP 5.6,对天缘而言,第一眼看到CryptoPP就感觉头大,根目录下放置大量单源文件、编译文件、项目文件,再加上多平台和多编译器支持,文件几乎又多了一倍,而且还是都混到一起,直接就让人望而却步。毕竟Crypto是个功能完整,且经过大量用户使用考验的开源库。所以,皱眉学习汇总一下,遂成此文。
官方网址:http://www.cryptopp.com/
本文测试环境:Windows7 SP1+VC6,测试工程名为Test。用VC打开CryptoPP工程文件,会发现有四个子工程:
- cryptdll - 生成cryptopp.dll动态库
- dlltest - 用来测试cryptopp.dll,依赖cryptdll工程
- cryptlib - 生成cryptlib.lib静态库
- cryptest - 用来测试cryptopp,依赖cryptlib工程
所以,我们有两种使用CryptoPP方法,一种是静态链接,还有一种是动态链接,使用对应的工程编译即可,区别就不说了,我们下文以静态链接为例,介绍几种常用加解密算法使用。
一、编译cryptlib
首先需要编译cryptlib,最后得到cryptlib.lib文件。
- 先在测试工程Test下创建目录cryptopp\lib和cryptopp\include,并把Project Settings-C/C++-Processor下Include目录增加“cryptopp\include”。
- 把cryptlib.lib拷贝到Test\cryptopp\lib下。
- 把cryptoPP根目录下所有的*.h文件都拷贝到Test\cryptopp\include下。当然实际上用不了那么多,后续用到哪个include哪个。
下文开始测试使用CryptoPP,实际使用中,如果功能上逻辑上需要修改,可以参考上文测试工程中的示例程序,以及官方文档。下文编译如果报告XX重复定义等错误,请检查LIB库工程和本测试工程的:Project Setting-C/C++-Code Generation User run-time library是否统一。
二、测试cryptlib
1、测试MD5
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1#include "md5.h"using namespace CryptoPP;#pragma comment(lib, "cryptopp\\lib\\cryptlib.lib") using namespace std;void main() {byte message[]="HelloWorld!"; byte mres[16];//MD5 128 bits=16bytesWeak::MD5 md5; md5.Update(message,11);//strlen=11md5.Final(mres); for(int i=0;i<16;i++)printf("%02X",mres[i]); printf("\n");}
2、测试AES
//For AES encrypt#include "default.h" #include "cryptlib.h"#include "filters.h"#include "bench.h"#include "osrng.h"#include "hex.h"#include "modes.h"#include "files.h"using namespace CryptoPP;#pragma comment(lib, "cryptopp\\lib\\cryptlib.lib") using namespace std;void main() {unsigned char key[]= {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};//AES::DEFAULT_KEYLENGTHunsigned char iv[]= {0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03, 0x01,0x02,0x03,0x03,0x03,0x03};int keysize = 16;stringmessage = "Hello World!";stringstrEncTxt;stringstrDecTxt;//CBC - PADDING//AES-CBC Encrypt(ONE_AND_ZEROS_PADDING)CBC_Mode<AES>::Encryption Encryptor1(key,keysize,iv); StringSource(message,true,new StreamTransformationFilter(Encryptor1,new StringSink( strEncTxt ),BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING,true));//AES-CBC DecryptCBC_Mode<AES>::Decryption Decryptor1(key,keysize,iv); StringSource(strEncTxt, true,new StreamTransformationFilter( Decryptor1,new StringSink( strDecTxt ),BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING,true));//CTR - NO_PADDING//AES-CTR EncryptCTR_Mode<AES>::Encryption Encryptor2(key,keysize,iv); StringSource(message, true,new StreamTransformationFilter( Encryptor2,new StringSink( strEncTxt ),BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING,true)); //AES-CTR DecryptCTR_Mode<AES>::Decryption Decryptor2(key,keysize,iv); StringSource(strEncTxt, true,new StreamTransformationFilter( Decryptor2,new StringSink( strDecTxt ),BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING,true)); }
上文是使用StringSource方式加密字符串,还可以使用FileSource方式直接对文件进行加解密操作。示例如下:
SecByteBlock HexDecodeString(const char *hex) {StringSource ss(hex, true, new HexDecoder);SecByteBlock result((size_t)ss.MaxRetrievable());ss.Get(result, result.size());return result;}void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile) {SecByteBlock key = HexDecodeString(hexKey);SecByteBlock iv = HexDecodeString(hexIV);CTR_Mode<AES>::Encryption aes(key, key.size(), iv);FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));}
直接调用AES_CTR_Encrypt函数即可,CBC函数需对应修改。
四、使用提示
1、StringSource类定义filters.h
//! zero terminated string as sourceStringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULL): SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}//! binary byte array as sourceStringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULL): SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}//! std::string as sourceStringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULL): SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
2、RSA有关的加解密、签名函数
更多请参考工程cryptest工程下test.cpp文件内函数
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);string RSADecryptString(const char *privFilename, const char *ciphertext);void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename);bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char
本文链接:http://www.metsky.com/archives/584.html
版权声明:天缘原创博客,转载和使用请遵守署名、非商业用途和保持一致,转载请注明出处: 天缘博客。
- CryptoPP使用介绍
- CryptoPP使用介绍
- Cryptopp使用
- Cryptopp使用
- 使用CryptoPP实现DES加密
- Crypto++(CryptoPP)库初始化以及使用注意事项
- Cryptopp密码库在Linux下的安装与使用
- CryptoPP 使用方法
- Cryptopp学习笔记(一)
- CryptoPP::HashModule例子纠正
- [CryptoPP userguide]BufferedTransformation示例
- [CryptoPP::userguide]Encryptionwithrng
- Cryptopp安装说明
- cryptopp++的配置
- CryptoPP DES 加解密
- Cryptopp安装配置
- CryptoPP学习笔记(二)
- 再看cryptopp, 版本5.6.1
- 【刷题之路】二叉树先序序列化
- linux定时器
- Python SMTP发送邮件
- 给exe程序 制作数字签名
- Libgcrypt 代码实例
- CryptoPP使用介绍
- 谈谈对APC的一点理解
- Android----让你的状态栏变色-----沉浸式状态栏解决方案
- 该伙伴事务管理器已经禁止了它对远程/网络事务的支持
- JAVA 加载文件的三种方式的区别
- 特殊质数js实现
- ImageLoader的使用
- jQuery kxbdMarquee 无缝滚动
- Linux下tomcat JVM内存 配置