//*****************************************************************************
//@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++接口实现文件

使用Crypto++库的CBC模式实现加密的更多相关文章

  1. 使用Crypto++库的CBC模式实现加密(二)

    前面已经有一篇介绍使用Crypto++库实现的加密的文章了,但是代码中考虑的不完全,所以就重新发了个二 C++封装: #include "zyaes.h" #include < ...

  2. C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解

    之前工作上需要用C++把软件生成的用户序列号用des加密cbc的模式,加密后为二进制,转化为十六进制,然后提供给java写的授权码管理平台. java平台会根据用户序列号,生成一个授权码,授权码是用r ...

  3. AES采用CBC模式128bit加密工具类

    写在前面 安全测试ECB模式过于简单需要改为CBC模式加密以下为工具类及测试 AESUtils.java package com.sgcc.mobile.utils; import sun.misc. ...

  4. C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式

    ============================================== des   cbc  加密 zeropadding填充方式 ======================= ...

  5. DES加解密 cbc模式 的简单讲解 && C++用openssl库来实现的注意事项

    DES cbc是基于数据块加密的.数据块的长度为8字节64bit.以数据块为单位循环加密,再拼接.每个数据块加密的秘钥一样,IV向量不同.第一个数据快所需的IV向量,需要我们提供,从第二个数据块开始, ...

  6. Python实现AES的CBC模式加密和解密过程详解 和 chr() 函数 和 s[a:b:c] 和函数lambda

    1.chr()函数 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 2.s[a:b:c] s=(1,2,3,4,5) 1>. s[a]下标访 ...

  7. 使用Crypto++库编译出错 解决办法

    错误信息: >------ 已启动生成: 项目: testCrypto++, 配置: Debug Win32 ------ >正在编译... >main.cpp >正在链接.. ...

  8. 解决AES算法CBC模式加密字符串后再解密出现乱码问题

    问题 在使用 AES CBC 模式加密字符串后,再进行解密,解密得到的字符串出现乱码情况,通常都是前几十个字节乱码: 复现 因为是使用部门 cgi AESEncryptUtil 库,找到问题后,在这里 ...

  9. C#调用Crypto++库AES ECB CBC加解密

    本文章使用上一篇<C#调用C++类库例子>的项目代码作为Demo.本文中,C#将调用C++的Crypto++库,实现AES的ECB和CBC加解密. 一.下载Crypto 1.进入Crypt ...

随机推荐

  1. js创建json对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Android UI Material Design

    Material Design 中文版: http://wiki.jikexueyuan.com/project/material-design/ Material Design开发文章系列1:App ...

  3. Android介绍

    Android系统的底层建立在Linux系统之上,该平台有操作系统,中间件,用户界面和应用软件4层组成,它采用一种被称为软件叠层(Software Stack)的方式进行构建. 1.应用程序层:And ...

  4. 网络采集软件核心技术剖析系列(5)---将任意博主的全部博文下载到内存中并通过Webbrower显示(将之前的内容综合到一起)

    一 本系列随笔概览及产生的背景 自己开发的豆约翰博客备份专家软件工具问世3年多以来,深受广大博客写作和阅读爱好者的喜爱.同时也不乏一些技术爱好者咨询我,这个软件里面各种实用的功能是如何实现的. 该软件 ...

  5. "__doPostBack”未定义的解决办法(转载)

    "__doPostBack”未定义,在IE下调试错误: 问题是服务器版的.Net40的补丁没有打上,ASP.NET 可能无法辨识出一些浏览器的最新版本,还会经常把它们看做是低级的浏览器,不支 ...

  6. 【POI】对于POI无法处理超大xls等文件,官方解决方法【已解决】【多线程提升速率待定】

    本次使用POI处理xlsx文件,莫名的遇到了一个无法逾越的问题. 总共71个xlsx文件,单个文件最大达到50M以上,71个xls文件摆在那里就有3-4G的大小. 在起始处理的时候,发现原本适用于正常 ...

  7. 【Git】GitHub for Windows使用(3) GitHub Flow的使用

    第三章了,关于GitHub上有一个Pull Request,是展示本项目或资源所有的Pull 请求的. 而这个开发流程是基于GitHub Flow的开发模式. 网上关于GitHub Flow简单的介绍 ...

  8. fl2440字符设备led驱动

    首先要明白字符设备驱动注册的基本流程 当我们调用insomd命令加载驱动后,驱动程序从module_init函数开始执行:硬件初始化 -> 申请主次设备号 -> 定义fops(file_o ...

  9. 自己做的roguelike+恶魔城游戏《魔塔猎人》已发布。

    游戏仍然是标准的roguelike,死亡后回到出生点重新开始,宏观架构上参考了<死亡细胞>,战斗设计上更加强调轻重攻击的组合,再配合236和28系列的搓招技.空中的突进飞腿.副武器等等. ...

  10. List<实体>与List<String>数据互转

    1.List<实体>数据: public List<Device> queryOSDevice(String cpu,String ip,String name){ Strin ...