//*****************************************************************************
//@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 <cryptopp/aes.h>
#include <string> typedef unsigned char BYTE //@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(byte *p_byteKey, byte *p_byteIv, int nKeyLen);
private:
byte *m_pByteKey;
byte *m_pByteIv;
int m_nKeyLen;
}; //@Usage Start
//unsigned char key[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};//AES::DEFAULT_KEYLENGTH
//unsigned char iv[] = {0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03, 0x03,0x03, 0x01,0x02,0x03,0x03,0x03,0x03};
//int keysize = 16;
//CSCSAes aes;
//aes.SetKey(key, iv, keysize);
//string strCipher = aes.Encrypt(strText);
//string strText = aes.Encrypt(strCipher);
//@Usage End #endif // __CSCSAES_H__
 CSCSAes::CSCSAes(string strKey, string strIv)
{
SetKey(strKey, strIv);
} CSCSAes::~CSCSAes()
{ } void CSCSAes::SetKey(byte *p_byteKey, byte *p_byteIv, int nKeyLen)
{
m_pByteKey = p_byteKey;
m_pByteIv = p_byteIv;
m_nKeyLen = nKeyLen;
} // 加密
std::string CSCSAes::Encrypt(const std::string &strText)
{
string strCipher;
CBC_Mode<AES>::Encryption aesEncryptor(m_pByteKey, m_nKeyLen, m_pByteIv);
StringSource(strText, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strCipher))); return strCipher;
} // 解密
std::string CSCSAes::Decrypt(const std::string &strCipher)
{
string strText;
CBC_Mode<AES>::Decryption aesEncryptor(m_pByteKey, m_nKeyLen, m_pByteIv);
StringSource(strCipher, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strText))); return strText;
}

++++++++++++++++++++++++++++++++++++++++++升级 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 = ;
CSCSAes aes; if (strKey != NULL)
{
aes.SetKey(strKey, "");
}
strCipher = aes.Encrypt(strText);
nLen = strCipher.length(); pCipher = (char *)malloc(nLen + );
memset(pCipher, , nLen + );
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 = ;
CSCSAes aes; if (strKey != NULL)
{
aes.SetKey(strKey, "");
}
strText = aes.Decrypt(strCipher);
nLen = strText.length(); pText = (char *)malloc(nLen + );
memset(pText, , nLen + );
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[];
byte m_arrByteIv[];
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) * );
memcpy(m_arrByteIv, byteIv, sizeof(byte) * );
m_nKeyLen = ;
} CSCSAes::~CSCSAes()
{
} // 设置密钥和初始向量
void CSCSAes::SetKey(std::string strKey, std::string strIv)
{
int nKeyLen = ;
int nIvLen = ;
memset(m_arrByteKey, , sizeof(byte) * );
memset(m_arrByteIv, , sizeof(byte) * ); if (strKey.length() >= )
{
nKeyLen = ;
}
else
{
nKeyLen = strKey.length();
}
memcpy(m_arrByteKey, strKey.c_str(), sizeof(byte) * nKeyLen); if (!strIv.empty())
{
if (strIv.length() >= )
{
nIvLen = ;
}
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++接口实现文件

最新文章

  1. iOS-钥匙串中证书全部失效(证书的签发者无效)的解决办法
  2. SimpleHttpServer的学习之UML
  3. MVC in Javascript
  4. Android 富文本框实现 RichEditText
  5. C. Captain Marmot (Codeforces Round #271)
  6. 浅谈Android五大布局
  7. webpack使用
  8. 【ES6】变量的解构赋值
  9. 创建Maven web工程不能解析EL表达式的解决办法
  10. linux仅修改文件夹权限 分别批量修改文件和文件夹权限
  11. RelativeLayout设置wrap_content无效
  12. H5 实现图片上传预览
  13. laraval migration 新增字段或者修改字段的方法
  14. 用二分法定义平方根函数(Bisection method Square Root Python)
  15. 基于ip的虚拟主机配置——在一台服务器上绑定多个 IP 地址
  16. sql server 作业收缩数据库
  17. R语言csv与txt文本读入区分(sep参数)
  18. multer详解
  19. 币安Binance API Websocket
  20. 小白第一次使用Git随笔

热门文章

  1. linux安装dubbo
  2. 使用DFS求任意两点的所有路径
  3. OC语言基础之NSArray
  4. javascript 定时器 笔记
  5. 连接zookeeper集群
  6. js判断上传文件的大小、类型、修改日期等信息
  7. 渗透笔记0x00
  8. vim使用指北 ---- Multiple Windows in Vim
  9. 表单提交时上传图片 表单ajax提交
  10. 小米6安装google play