首先创建Crypto__test 工程。

在已编译好的Cryptopp561中找到Debug文件夹 下的cryptlib.lib 将其改名为cryptlibd.lib后放到上级目录中。在Release文件夹下找到cryptlib.lib 将其放到上级目录中(不改名)。目的是为以后引用方便。

在 工具——选项 ——目录中 选择 Include--files 导入CRYPTOPP561 (方便以后程序中引用) 接着选择Library files 导入CRYPTOPP561 (这就是问什么刚才需要把两个库文件放在上级目录的原因----方便引用)


在工程———设置 ——调试 下 分类中选择 Code Generation 然后选择Multithreaded (多线程) 此步非常重要,若不设置将会出现2005错误。

将如下代码放入 StdAfx.h 文件中 (主要是编译时用到得头文件和类库)
- #include <iostream>
-
-
- #ifdef _DEBUG
- # pragma comment( lib, "cryptlibd" )
- #else
- # pragma comment( lib, "cryptlib" )
- #endif

最后在Crypto__test.cpp 中贴入如下源码:
-
-
-
- #include "stdafx.h"
-
- #include "rsa.h"
- #include "osrng.h"
- #include "integer.h"
- #include "sha.h"
- #include "hex.h"
- #include "filters.h"
-
- int main(int argc, char* argv[])
- {
-
-
-
- std::string message( "I think computer viruses should count as life. I think it\n" \
- " says something about human nature that the only form of\n" \
- " life we have created so far is purely destructive. We've\n" \
- " created life in our own image." );
-
-
-
- CryptoPP::AutoSeededRandomPool rng;
-
-
-
- CryptoPP::InvertibleRSAFunction keys;
- keys.GenerateRandomWithKeySize( rng, 384 );
-
-
-
- CryptoPP::Integer n = keys.GetModulus();
- CryptoPP::Integer p = keys.GetPrime1();
- CryptoPP::Integer q = keys.GetPrime2();
- CryptoPP::Integer d = keys.GetPrivateExponent();
- CryptoPP::Integer e = keys.GetPublicExponent();
-
-
-
- std::cout << "RSA Parameters:" << std::endl;
- std::cout << " n: " << n << std::endl;
- std::cout << " p: " << p << std::endl;
- std::cout << " q: " << q << std::endl;
- std::cout << " d: " << d << std::endl;
- std::cout << " e: " << e << std::endl;
- std::cout << std::endl;
-
-
-
- CryptoPP::RSASS<
- CryptoPP::PKCS1v15, CryptoPP::SHA
- >::Signer signer( keys );
-
-
-
- std::cout << "Message:" << std::endl;
- std::cout << " " << message << std::endl;
- std::cout << std::endl;
-
-
- byte* signature = new byte[ signer.MaxSignatureLength() ];
- if( NULL == signature ) { return -1; }
-
-
- size_t length = signer.SignMessage( rng, (const byte*) message.c_str(),
- message.length(), signature );
-
-
-
- std::string encoded;
- CryptoPP::HexEncoder encoder( new CryptoPP::StringSink( encoded ),
- true , 2 , ":" );
- encoder.Put( signature, length );
- encoder.MessageEnd();
-
-
-
- std::cout << "Signature:" << std::endl;
- std::cout << " " << encoded << std::endl;
- std::cout << std::endl;
-
-
-
- CryptoPP::RSASS<
- CryptoPP::PKCS1v15, CryptoPP::SHA
- >::Verifier verifier( signer );
-
- bool result = verifier.VerifyMessage( (const byte*)message.c_str(),
- message.length(), signature, length );
-
-
-
- if( true == result )
- {
- std::cout << "Message Verified" << std::endl;
- }
- else
- {
- std::cout << "Message Verification Failed" << std::endl;
- }
-
-
-
- if( NULL != signature ) { delete[] signature; }
-
- return 0;
- }
运行结果如下:

到此有关crypto++环境搭建工作基本上已经完成。若遇到此文中没有提及的问题时可百度查阅相关资料。
|