码迷,mamicode.com
首页 > 其他好文 > 详细

使用Crypto++库的CBC模式实现加密

时间:2014-11-14 19:43:25      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   使用   sp   for   

 1 //*****************************************************************************
 2 //@File Name    : scsaes.h: the interface of crypto++ library
 3 //@Version        : V1.0.0
 4 //@Author        : xiaoc
 5 //@Date            : 2014/11/11
 6 //*****************************************************************************
 7 
 8 #ifndef __CSCSAES_H__
 9 #define __CSCSAES_H__
10 
11 #include <cryptopp/aes.h>
12 #include <string>
13 
14 typedef unsigned char BYTE
15 
16 //@Description
17 //This class encapsulate the aes library‘s encryption method and decryption method.
18 class CSCSAes
19 {
20 public:
21     CSCSAes();
22     virtual ~CSCSAes();
23 
24 public:
25     // encrypt plainText
26     std::string Encrypt(const std::string &strText);
27     // decrypt plainText
28     std::string Decrypt(const std::string &strText);
29     
30     void SetKey(byte *p_byteKey, byte *p_byteIv, int nKeyLen);
31 private:
32     byte *m_pByteKey;
33     byte *m_pByteIv;
34     int m_nKeyLen;
35 };
36 
37 //@Usage Start
38 //unsigned char key[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};//AES::DEFAULT_KEYLENGTH  
39 //unsigned char iv[]  = {0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03, 0x03,0x03, 0x01,0x02,0x03,0x03,0x03,0x03};
40 //int keysize = 16; 
41 //CSCSAes aes;
42 //aes.SetKey(key, iv, keysize); 
43 //string strCipher = aes.Encrypt(strText);
44 //string strText = aes.Encrypt(strCipher);
45 //@Usage End
46 
47 #endif // __CSCSAES_H__
 1 CSCSAes::CSCSAes(string strKey, string strIv)
 2 {
 3     SetKey(strKey, strIv);
 4 }
 5 
 6 CSCSAes::~CSCSAes()
 7 {
 8     
 9 }
10 
11 void CSCSAes::SetKey(byte *p_byteKey, byte *p_byteIv, int nKeyLen)
12 {
13     m_pByteKey = p_byteKey;
14     m_pByteIv = p_byteIv;
15     m_nKeyLen = nKeyLen;
16 }
17 
18 // 加密
19 std::string CSCSAes::Encrypt(const std::string &strText)
20 {
21     string strCipher;
22     CBC_Mode<AES>::Encryption aesEncryptor(m_pByteKey, m_nKeyLen, m_pByteIv);
23     StringSource(strText, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strCipher)));
24     
25     return strCipher;
26 }
27 
28 // 解密
29 std::string CSCSAes::Decrypt(const std::string &strCipher)
30 {
31     string strText;
32     CBC_Mode<AES>::Decryption aesEncryptor(m_pByteKey, m_nKeyLen, m_pByteIv);
33     StringSource(strCipher, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strText)));
34     
35     return strText;
36 }

 

使用Crypto++库的CBC模式实现加密

标签:des   style   blog   io   color   ar   使用   sp   for   

原文地址:http://www.cnblogs.com/lit10050528/p/4097987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!