CryptoPP DES 加解密

DesUtil.h
[cpp]  view plain copy
  1. #ifndef DESUTIL_H_  
  2. #define DESUTIL_H_  
  3.   
  4. #include <string>  
  5.   
  6. class DesUtil {  
  7. private:  
  8.     const char *keystr;  
  9.     void Base64en(const unsigned char *ming, unsigned char* enstr);  
  10.     void Base64de(const unsigned char* mi, unsigned char* destr);  
  11. public:  
  12.     DesUtil(const char *key);  
  13.     std::string Encrypt(const std::string &content);  
  14.     std::string Decrypt(const std::string &content);  
  15. };  
  16.   
  17. #endif /* DESUTIL_H_ */  

DesUtil.cpp

[cpp]  view plain copy
  1. #include "DesUtil.h"  
  2. #include <iostream>  
  3. #include <des.h>  
  4. #include <base64.h>  
  5. #include <osrng.h>  
  6. #include <string>  
  7. #include <cstdlib>  
  8. #include <cryptlib.h>  
  9. #include <hex.h>  
  10. #include <filters.h>  
  11. #include <modes.h>  
  12. #include <secblock.h>  
  13.   
  14. DesUtil::DesUtil(const char *key) : keystr(key) {  
  15.     // TODO Auto-generated constructor stub  
  16.   
  17. }  
  18.   
  19. void DesUtil::Base64en(const unsigned char* ming, unsigned char* enstr) {  
  20.     using CryptoPP::Base64Encoder;  
  21.     Base64Encoder base64en;  
  22.     base64en.Put(ming, strlen((const char *) ming));  
  23.   
  24.     base64en.MessageEnd();  
  25.     //  base64en.MessageSeriesEnd();  
  26.   
  27.     unsigned int size = base64en.MaxRetrievable();  
  28.     byte* temp = new byte[size + 1];  
  29.     temp[size] = '\0';  
  30.   
  31.     base64en.Get(temp, size);  
  32.   
  33.     //  cout << "Base64en: " << temp << endl;  
  34.     memcpy(enstr, temp, size + 1);  
  35.     delete temp;  
  36. }  
  37.   
  38. void DesUtil::Base64de(const unsigned char* mi, unsigned char* destr) {  
  39.     using CryptoPP::Base64Decoder;  
  40.     Base64Decoder base64de;  
  41.     base64de.Put(mi, strlen((const char *) mi));  
  42.     base64de.MessageEnd();  
  43.     unsigned int size = base64de.MaxRetrievable();  
  44.     byte* temp = new byte[size + 1];  
  45.     temp[size] = '\0';  
  46.     base64de.Get(temp, size);  
  47.     //  cout << "Base64de: " << temp << endl;  
  48.     memcpy(destr, temp, size + 1);  
  49.     delete temp;  
  50. }  
  51.   
  52. std::string DesUtil::Encrypt(const std::string & content) {  
  53.     using namespace CryptoPP;  
  54.     std::string cipher;  
  55.     try {  
  56.         ECB_Mode<DES>::Encryption e;  
  57.         e.SetKey((const unsigned char *) keystr, DES::DEFAULT_KEYLENGTH);  
  58.   
  59.         // The StreamTransformationFilter adds padding  
  60.         //  as required. ECB and CBC Mode must be padded  
  61.         //  to the block size of the cipher.  
  62.         StringSource(content, true,  
  63.                 new StreamTransformationFilter(e, new StringSink(cipher)) // StreamTransformationFilter  
  64.                         );// StringSource  
  65.         byte * mi = new byte[strlen(content.c_str())*8];  
  66.         Base64en((unsigned char*) cipher.c_str(), mi);  
  67.         std::string result((char*) mi);  
  68.         delete mi;  
  69.         return result;  
  70.     } catch (const CryptoPP::Exception& e) {  
  71.         std::cerr << e.what() << std::endl;  
  72.         exit(1);  
  73.     }  
  74.     return NULL;  
  75. }  
  76.   
  77. std::string DesUtil::Decrypt(const std::string & content) {  
  78.     using namespace CryptoPP;  
  79.     std::string recovered;  
  80.     byte * ming = new byte[strlen(content.c_str())*8];  
  81.     Base64de((unsigned char*) content.c_str(), ming);  
  82.     std::string temp((char *) ming);  
  83.     delete ming;  
  84.     try {  
  85.         ECB_Mode<DES>::Decryption d;  
  86.         d.SetKey((const unsigned char *) keystr, DES::DEFAULT_KEYLENGTH);  
  87.   
  88.         // The StreamTransformationFilter removes  
  89.         //  padding as required.  
  90.         StringSource s(temp, true,  
  91.                 new StreamTransformationFilter(d, new StringSink(recovered)) // StreamTransformationFilter  
  92.                         );// StringSource  
  93.         return recovered;  
  94. //          std::cout << "recovered text: " << recovered << std::endl;  
  95.     } catch (const CryptoPP::Exception& e) {  
  96.         std::cerr << e.what() << std::endl;  
  97.         exit(1);  
  98.     }  
  99.     return NULL;  
  100. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值