使用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 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 }

 ++++++++++++++++++++++++++++++++++++++++++升级 2015/02/06+++++++++++++++++++++++++++++++++++++++++++++++

封装了一层C的接口

//*****************************************************************************
//@FileName    : scsaes256.h : the c interface of class scsaes
//@Version    : v1.0.0
//@Author    : xiaoc
//@Date        : 2014/11/18
//*****************************************************************************

//*****************************************************************************
//@strText    : 需要加密的数据
//@strKey    : 加密用的密钥    
//@return      : 返回加密之后的数据
//*****************************************************************************
const char *Encrypt(const char *strText, const char *strKey = NULL);

//*****************************************************************************
//@strText    : 需要解密的数据
//@strKey    : 解密用的密钥    
//@return      : 返回解密之后的数据
//*****************************************************************************
const char *Decrypt(const char *strCipher, const char *strKey = NULL);
C接口头文件
//*****************************************************************************
//@FileName    : scsaes256.cpp
//@Version    : v1.0.0
//@Author    : xiaoc
//@Date        : 2014/11/18
//*****************************************************************************

#include <string>
#include "scsaes.h"

using namespace std;

const char *Encrypt(const char *strText, const char *strKey = NULL)
{
    char *pCipher = NULL;
    std::string strCipher;
    int nLen = 0;
    CSCSAes aes;
    
    if (strKey != NULL)
    {
        aes.SetKey(strKey, "");
    }
    strCipher = aes.Encrypt(strText);
    nLen = strCipher.length();
    
    pCipher = (char *)malloc(nLen + 1);
    memset(pCipher, 0, nLen + 1);
    memcpy(pCipher, strCipher.c_str(), nLen);
    return pCipher;
}

const char *Decrypt(const char *strCipher, const char *strKey = NULL)
{
    char *pText = NULL;
    std::string strText;
    int nLen = 0;
    CSCSAes aes;
    
    if (strKey != NULL)
    {
        aes.SetKey(strKey, "");
    }
    strText = aes.Decrypt(strCipher);
    nLen = strText.length();
    
    pText = (char *)malloc(nLen + 1);
    memset(pText, 0, nLen + 1);
    memcpy(pText, strText.c_str(), nLen);
    return pText;
}
C接口实现文件
//*****************************************************************************
//@File Name    : scsaes.h: the interface of crypto++ library
//@Version        : V1.0.0
//@Author        : xiaoc
//@Date            : 2014/11/11
//*****************************************************************************

#ifndef __CSCSAES_H__
#define __CSCSAES_H__

#include <string>
#include "aes.h"
#include "default.h"
#include "filters.h"
#include "modes.h"

using namespace CryptoPP;
//@Description
//This class encapsulate the aes library's encryption method and decryption method.
class CSCSAes
{
public:
    CSCSAes();
    virtual ~CSCSAes();

public:
    // encrypt plainText
    std::string Encrypt(const std::string &strText);
    // decrypt plainText
    std::string Decrypt(const std::string &strText);
    
    void SetKey(std::string strKey, std::string strIv);
private:
    byte m_arrByteKey[32];
    byte m_arrByteIv[16];
    int m_nKeyLen;
};

//@Usage Start
//CSCSAes aes;
//string strCipher = aes.Encrypt(strText);
//string strText = aes.Encrypt(strCipher);
//@Usage End

#endif // __CSCSAES_H__
C++接口头文件
#include "scsaes.h"
#include <string.h>

using namespace CryptoPP;

CSCSAes::CSCSAes()
{
    byte byteKey[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08, 
                0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};
    byte byteIv[] = {0x01,0x02,0x03,0x04,0x01,0x02,0x03,0x04, 0x01,0x02, 0x03,0x04,0x01,0x02,0x03,0x04};
    memcpy(m_arrByteKey, byteKey, sizeof(byte) * 32);
    memcpy(m_arrByteIv, byteIv, sizeof(byte) * 16);
    m_nKeyLen = 32;
}

CSCSAes::~CSCSAes()
{
}

// 设置密钥和初始向量
void CSCSAes::SetKey(std::string strKey, std::string strIv)
{
    int nKeyLen = 0;
    int nIvLen = 0;
    memset(m_arrByteKey, 0, sizeof(byte) * 32);
    memset(m_arrByteIv, 0, sizeof(byte) * 16);
    
    if (strKey.length() >= 32)
    {
        nKeyLen = 32;
    }
    else
    {
        nKeyLen = strKey.length();
    }
    memcpy(m_arrByteKey, strKey.c_str(), sizeof(byte) * nKeyLen);
    
    if (!strIv.empty())
    {
        if (strIv.length() >= 16)
        {
            nIvLen = 16;
        }    
        else
        {
            nIvLen = strIv.length();
        }
        memcpy(m_arrByteIv, strIv.c_str(), sizeof(byte) * nIvLen);
    }
}

// 加密
std::string CSCSAes::Encrypt(const std::string &strText)
{
    std::string strCipher;
    CBC_Mode<AES>::Encryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
    StringSource(strText, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strCipher)));
    
    return strCipher;
}

// 解密
std::string CSCSAes::Decrypt(const std::string &strCipher)
{
    std::string strText;
    CBC_Mode<AES>::Decryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
    StringSource(strCipher, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strText)));
    
    return strText;
}
C++接口实现文件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值