分享

Vc6.0下搭建 Crypto 环境

 londonKu 2012-05-05

   首先创建Crypto__test 工程。


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


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



  

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


将如下代码放入  StdAfx.h 文件中 (主要是编译时用到得头文件和类库)

  1. #include <iostream>  
  2.   
  3. // Crypto++ Library  
  4. #ifdef _DEBUG  
  5. #  pragma comment( lib, "cryptlibd" )  
  6. #else  
  7. #  pragma comment( lib, "cryptlib" )  
  8. #endif  


 最后在Crypto__test.cpp 中贴入如下源码:

  1. // Crypto__test.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. #include "rsa.h"  
  7. #include "osrng.h"  
  8. #include "integer.h"  
  9. #include "sha.h"  
  10. #include "hex.h"  
  11. #include "filters.h"  
  12.   
  13. int main(int argc, char* argv[])  
  14. {  
  15.     ///////////////////////////////////////  
  16.     // Quote of the Day  
  17.     //   Stephen Hawkins  
  18.     std::string message( "I think computer viruses should count as life. I think it\n" \  
  19.         " says something about human nature that the only form of\n" \  
  20.         " life we have created so far is purely destructive. We've\n" \  
  21.         " created life in our own image." );  
  22.   
  23.     ///////////////////////////////////////  
  24.     // Pseudo Random Number Generator  
  25.     CryptoPP::AutoSeededRandomPool rng;  
  26.   
  27.     ///////////////////////////////////////  
  28.     // Key Generation  
  29.     CryptoPP::InvertibleRSAFunction keys;  
  30.     keys.GenerateRandomWithKeySize( rng, 384 );  
  31.   
  32.     ///////////////////////////////////////  
  33.     // Generated Parameters  
  34.     CryptoPP::Integer n = keys.GetModulus();  
  35.     CryptoPP::Integer p = keys.GetPrime1();  
  36.     CryptoPP::Integer q = keys.GetPrime2();  
  37.     CryptoPP::Integer d = keys.GetPrivateExponent();  
  38.     CryptoPP::Integer e = keys.GetPublicExponent();  
  39.   
  40.     ///////////////////////////////////////  
  41.     // Dump  
  42.     std::cout << "RSA Parameters:" << std::endl;  
  43.     std::cout << " n: " << n << std::endl;  
  44.     std::cout << " p: " << p << std::endl;  
  45.     std::cout << " q: " << q << std::endl;  
  46.     std::cout << " d: " << d << std::endl;  
  47.     std::cout << " e: " << e << std::endl;  
  48.     std::cout << std::endl;  
  49.   
  50.     ///////////////////////////////////////  
  51.     // Signature  
  52.     CryptoPP::RSASS<  
  53.         CryptoPP::PKCS1v15, CryptoPP::SHA  
  54.     >::Signer signer( keys );  
  55.   
  56.     ///////////////////////////////////////  
  57.     // Dump  
  58.     std::cout << "Message:" << std::endl;  
  59.     std::cout << " " << message << std::endl;  
  60.     std::cout << std::endl;  
  61.   
  62.     // Set up for SignMessage()  
  63.     byte* signature = new byte[ signer.MaxSignatureLength() ];  
  64.     if( NULL == signature ) { return -1; }  
  65.   
  66.     // Sign...  
  67.     size_t length = signer.SignMessage( rng, (const byte*) message.c_str(),  
  68.         message.length(), signature );  
  69.   
  70.     ///////////////////////////////////////  
  71.     // Signature Hex Encoding  
  72.     std::string encoded;  
  73.     CryptoPP::HexEncoder encoder( new CryptoPP::StringSink( encoded ),  
  74.         true /* Uppercase */, 2 /* Grouping */":" /* Separator */ );  
  75.     encoder.Put( signature, length );  
  76.     encoder.MessageEnd();  
  77.   
  78.     ///////////////////////////////////////  
  79.     // Dump  
  80.     std::cout << "Signature:" << std::endl;  
  81.     std::cout << " " << encoded << std::endl;  
  82.     std::cout << std::endl;  
  83.   
  84.     ///////////////////////////////////////  
  85.     // Verification  
  86.     CryptoPP::RSASS<  
  87.         CryptoPP::PKCS1v15, CryptoPP::SHA  
  88.     >::Verifier verifier( signer );  
  89.   
  90.     bool result = verifier.VerifyMessage( (const byte*)message.c_str(),  
  91.         message.length(), signature, length );  
  92.   
  93.     ///////////////////////////////////////  
  94.     // Verify Result  
  95.     iftrue == result )  
  96.     {  
  97.         std::cout << "Message Verified" << std::endl;  
  98.     }  
  99.     else  
  100.     {  
  101.         std::cout << "Message Verification Failed" << std::endl;  
  102.     }  
  103.   
  104.     ///////////////////////////////////////  
  105.     // Cleanup  
  106.     if( NULL != signature ) { delete[] signature; }  
  107.   
  108.     return 0;  
  109. }  

运行结果如下:


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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多