//*****************************************************************************
//@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. 【CCpp程序设计2017】迷宫游戏

    大一寒假作业!写了第一个小游戏! //maze_test By lizitong #include<stdio.h> #include<time.h> #include< ...

  2. 【费用流】bzoj3280 小R的烦恼

    类似bzoj1221 http://www.cnblogs.com/autsky-jadek/p/4174087.html 只不过大学有多个,所以我们另开一个节点汇总所有'S->大学'的边,然后 ...

  3. Spring IOC 中三种注入方式

    项目错误知识点记录 正文 最近在项目的时候,用到Spring框架,Spring框架提供了一种IOC的自动注入功能,可以很轻松的帮助我们创建一个Bean,这样就省的我们四处写new Object()这样 ...

  4. Git学习笔记(一) 安装及版本库介绍

    安装Git 最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和 ...

  5. windbg学习—-.ecxr

      .ecxr 命令定位当前异常的上下文信息,并显示指定记录中的重要寄存器 0:000> .ecxr eax=10000000 ebx=7ffd9000 ecx=77386500 edx=002 ...

  6. LLBLGen Pro ORM 生成器

    LLBLGen Pro  ORM 生成器:         http://www.llblgen.com/default.aspx 支持多种 框架,多种数据库.

  7. web UIproject师必读的一篇文档,写的很好

    今天在花瓣网上看到的一片文章.写的是web UIproject师的工作应该是如何的,内容非常精彩,分享给小伙伴.希望web UIproject师看了之后有所启示. 我不是一个简单的web UI htt ...

  8. 表格中的IE BUG

    在表格应用了跨列单元格后,在IE6/7下当跨列单元格中的元素长度超过其跨列单元格中第一个单元格的宽度时会产生换行,如下所示: 解决方法: 1. 设置 table 的 'table-layout' 特性 ...

  9. Vue组件进阶知识总结

    上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子组件中定义props,可以让父组件的数据传递下来,这就好比子组件告诉父组件:“嘿,老哥,我开通了一个驿站,你 ...

  10. Tempter of the Bone——DFS(王道)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...