首页 > 代码库 > 使用Crypto++库的CBC模式实现加密
使用Crypto++库的CBC模式实现加密
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 BYTE15 16 //@Description17 //This class encapsulate the aes library‘s encryption method and decryption method.18 class CSCSAes19 {20 public:21 CSCSAes();22 virtual ~CSCSAes();23 24 public:25 // encrypt plainText26 std::string Encrypt(const std::string &strText);27 // decrypt plainText28 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 Start38 //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 End46 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模式实现加密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。