//*****************************************************************************
//@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. 【2-SAT】URAL - 2089 - Experienced coach

    题意:给出n对点a,b  要求从没对点中选出一个,且最终选出的点n个数不能存在相同的.输入数据满足每种数最多出现3次,最少出现1次 思路:第i对点的编号2*i, 2*i+1,   因为每个数最多出现3 ...

  2. python3开发进阶-Django框架中的ORM的常用(增,删,改,查)操作

    阅读目录 如何在Django终端打印SQL语句 如何在Python脚本中调用Django环境 操作方法 单表查询之神奇的下划线 ForeignKey操作 ManyToManyField 聚合查询和分组 ...

  3. 1.3(java学习笔记)构造方法及重载

    构造方法,用于对象的初始化,在创建对象时被自动调用的特殊方法.构造方法名称与类名一致,通过new调用. 下面通过代码来详细讲解 public class Point { int x, y; publi ...

  4. Centos 6.5 优化 一些基础优化和安全设置

    原文链接:http://www.centoscn.com/CentosSecurity/CentosSafe/2015/0424/5263.html 本文 centos 6.5 优化 的项有18处: ...

  5. 误删除Ubuntu 14桌面文件夹“~/桌面”,如何恢复?

    一不小心把当前用户的桌面文件夹“/home/wenjianbao/桌面”删了,导致系统把“/home/wenjianbao”当成桌面文件夹.结果,桌面上全是乱七八糟的文件/文件夹. 查看了下网络资料, ...

  6. wpf datagrid performance

    http://stackoverflow.com/questions/1704512/wpf-toolkit-datagrid-scrolling-performance-problems-why h ...

  7. 理解JS里的偏函数与柯里化

    联系到上篇博客讲的bind完整的语法为: let bound = func.bind(context, arg1, arg2, ...); 可以绑定上下文this和函数的初始参数.举例,我们有个乘法函 ...

  8. 【React Native开发】React Native控件之Image组件解说与美团首页顶部效果实例(10)

    ),React Native技术交流4群(458982758),欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送! Im ...

  9. IOS高级面试题

    1.写一下UIButton与UITableView的层级结构  2.Cocoa的Foundation对象与Core Foundation对象通过什么keyword进行转换?这些keyword有什么差别 ...

  10. react-native-storage + AsyncStorage 实现数据存储

    1.组件封装 import Storage from 'react-native-storage'; import { AsyncStorage } from 'react-native'; cons ...