使用Crypto++库的CBC模式实现加密
//*****************************************************************************
//@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模式实现加密的更多相关文章
- 使用Crypto++库的CBC模式实现加密(二)
前面已经有一篇介绍使用Crypto++库实现的加密的文章了,但是代码中考虑的不完全,所以就重新发了个二 C++封装: #include "zyaes.h" #include < ...
- C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解
之前工作上需要用C++把软件生成的用户序列号用des加密cbc的模式,加密后为二进制,转化为十六进制,然后提供给java写的授权码管理平台. java平台会根据用户序列号,生成一个授权码,授权码是用r ...
- AES采用CBC模式128bit加密工具类
写在前面 安全测试ECB模式过于简单需要改为CBC模式加密以下为工具类及测试 AESUtils.java package com.sgcc.mobile.utils; import sun.misc. ...
- C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式
============================================== des cbc 加密 zeropadding填充方式 ======================= ...
- DES加解密 cbc模式 的简单讲解 && C++用openssl库来实现的注意事项
DES cbc是基于数据块加密的.数据块的长度为8字节64bit.以数据块为单位循环加密,再拼接.每个数据块加密的秘钥一样,IV向量不同.第一个数据快所需的IV向量,需要我们提供,从第二个数据块开始, ...
- 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]下标访 ...
- 使用Crypto++库编译出错 解决办法
错误信息: >------ 已启动生成: 项目: testCrypto++, 配置: Debug Win32 ------ >正在编译... >main.cpp >正在链接.. ...
- 解决AES算法CBC模式加密字符串后再解密出现乱码问题
问题 在使用 AES CBC 模式加密字符串后,再进行解密,解密得到的字符串出现乱码情况,通常都是前几十个字节乱码: 复现 因为是使用部门 cgi AESEncryptUtil 库,找到问题后,在这里 ...
- C#调用Crypto++库AES ECB CBC加解密
本文章使用上一篇<C#调用C++类库例子>的项目代码作为Demo.本文中,C#将调用C++的Crypto++库,实现AES的ECB和CBC加解密. 一.下载Crypto 1.进入Crypt ...
随机推荐
- 【字符串哈希】【莫队算法】bzoj3207 花神的嘲讽计划Ⅰ
既然询问的长度是确定的,那么我们可以将所有长度为K的字串弄个哈希值出来,这样字串存在性=>哈希值存在性. 自然上溢哈希,base=107比较不错. 序列长度n=>n-K+1 询问区间[x, ...
- 1.5(学习笔记)Cookie
一.Cookie简介 Cookie是网站发送的一小段数据,在用户访问浏览网站时通过浏览器存储在用户的计算机上. 主要用于记录一些用户状态信息,例如记录用户的账号,当前所在地等,根据这些信息网站 可以提 ...
- java 面向接口编程的理解
初学者可能在学习中会有很多疑惑,为什么要这样,明明可以那样实现,这样做的好处又是什么? 可能会的人觉得很简单很容易理解,甚至可能觉得问的问题很智障,但对于小白来说可能是苦思冥想都不得其解的. 自己身为 ...
- Java高级架构师(一)第28节:Index、商品详细页和购物车
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- Intellij IDEA光标保持自动缩进,设置下次不放在行首
- mysql-connector-java-6日期存储时差的问题解决方法
在my.ini文件中的[mysqld]下面加入 default-time_zone = '+8:00' 重启mysql 修改driver连接路径 这样日期保存到mysql就是正确的了,但是mysql- ...
- 【js】js中const,var,let区别
在node.js使用例子中,第一次看到const的声明,查询了一下,可以看得出来: http://www.cnblogs.com/ksl666/p/5944718.html 参考 主要内容是:js中三 ...
- easyui dialog 按钮动态命名
1.方法一: /** * grid新增 * 弹框并且获取支付类型 */ function gridAdd() { var dlg = $('#mydialog').dialog({ title : & ...
- javascript快速入门18--样式
修改元素外观方式 修改元素外观主要有下面3种方法:修改ID,修改className,修改元素的style属性 修改ID?会造成多么混乱的结果可想而知! 修改className确实是非常好的方法,我们甚 ...
- Windows如何定时关机
方法一:首先在"开始"菜单点击"运行",输入"at xx:xx shoutdown -s" 可以实现定时关机,xx:xx指的是具体关机时间. ...