前面已经有一篇介绍使用Crypto++库实现的加密的文章了,但是代码中考虑的不完全,所以就重新发了个二

C++封装:

#include "zyaes.h"
#include <string.h>
#include <stdio.h> using namespace CryptoPP; CZYAes::CZYAes()
{
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 = ;
} CZYAes::~CZYAes()
{
} // set key and iv
void CZYAes::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);
}
} // encrypt
std::string CZYAes::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))); std::string strEncoded;
StringSource s2(strCipher, true,
new HexEncoder(
new StringSink(strEncoded)
) // HexEncoder
); // StringSource
return strEncoded;
} // decrypt
std::string CZYAes::Decrypt(const std::string &strCipher)
{
std::string strDecoded;
StringSource s2(strCipher, true,
new HexDecoder(
new StringSink(strDecoded)
) // HexEncoder
); // StringSource std::string strText;
CBC_Mode<AES>::Decryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
StringSource(strDecoded, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strText))); return strText;
}

实现文件

头文件

C封装:

//*****************************************************************************
//@FileName : aes256.cpp
//@Version : v1.0.0
//@Author : xiaoc
//@Date : 2015/03/13
//*****************************************************************************
#include "zyaes.h"
#include "zyaes256.h"
using namespace std; string Encrypt(const string &strText, const string &strKey/* = "" */)
{
std::string strCipher;
int nLen = ;
CZYAes aes; if (!strKey.empty())
{
aes.SetKey(strKey, "");
}
strCipher = aes.Encrypt(strText);
return strCipher;
} string Decrypt(const string &strCipher, const string &strKey/* = "" */)
{
std::string strText;
int nLen = ;
CZYAes aes; if (!strKey.empty())
{
aes.SetKey(strKey, "");
}
strText = aes.Decrypt(strCipher);
return strText;
}

实现文件

//*****************************************************************************
//@FileName : zyaes256.h : the c interface of class zyaes
//@Version : v1.0.0
//@Author : xiaoc
//@Date : 2015/03/13
//*****************************************************************************
#ifndef _ZYAES256_H_
#define _ZYAES256_H_
#include <string> //*****************************************************************************
//@strText : 需要加密的数据
//@strKey : 加密用的密钥
//@return : 返回加密之后的数据
//*****************************************************************************
std::string Encrypt(const std::string &strText, const std::string &strKey = ""); //*****************************************************************************
//@strText : 需要解密的数据
//@strKey : 解密用的密钥
//@return : 返回解密之后的数据
//*****************************************************************************
std::string Decrypt(const std::string &strCipher, const std::string &strKey = "");
#endif // _ZYAES256_H_

头文件

此次修改主要是上一个版本中没有考虑到加密数据在网络中的传输问题,因为加密数据不再是简单的ASCII字符,所以不能存在char数组中,上一版本中就因为这个问题导致了数据的丢失,因为加密数据中的0导致字符串被截断了。

因此现在的做法是,先对加密数据进行转换,转换为16进制。然后传输,解密端做相反处理即可。

其中有个疑问是,cryptopp加密库将加密数据存在string中,但是string为什么能够存储非ASCII字符呢?

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

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

    //***************************************************************************** //@File Name : scsae ...

  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. 1644 免费馅饼 题解(c++)

    1644 免费馅饼(巴蜀oj上的编号) 题面:          SERKOI最新推出了一种叫做"免费馅饼"的游戏.         游戏在一个舞台上进行.舞台的宽度为W格,天幕的 ...

  2. 集成禅道和svn

    转载:http://www.zentao.net/book/zentaopmshelp/137.html 说明:svn集成功能配置会比较复杂,我们会尽量通过文档来帮助大家配置成功!如果实在配置不成功的 ...

  3. go语言基础之defer延迟调用

    1.defer作用 关键字 defer ⽤于延迟一个函数或者方法(或者当前所创建的匿名函数)的执行.注意,defer语句只能出现在函数或方法的内部. 运行场景: defer语句经常被用于处理成对的操作 ...

  4. SQL Server快速部署作业到多台服务器

    问题: 需要在很多的SQL Server服务器上创建相同的作业.我们可以一台一台的运行相同的脚本创建作业,但是有没有什么简便的做法呢? 解决方法: 可能很多人都没有注意到可以用多服务器环境管理SQL ...

  5. Chrome 制作绿色便携版

    1.建立一个新的文件夹命名为Chrome 2.将电脑上默认的Chrome文件复制到新的文件夹Chrome里包含安装文件和Chrome数据文件     Chrome数据文件一般在"C:\Use ...

  6. 同步网络时间到linux服务器(先修改时区再进行同步网络时间)

    查看时区:date -R 修改整个系统时区: rm -f /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 网 ...

  7. Cocos2d-x -- 如何让背景从上到下滚动

    1. 首先,声明一个2个大小的sprite数组 class GameScreen : public cocos2d::Layer { public: ... cocos2d::Sprite *back ...

  8. MP3文件头格式

    MP3文件结构及编解码流程 http://blog.sina.com.cn/s/blog_67b7cb7b01018i2l.html http://blog.csdn.net/liuyan4794/a ...

  9. 算法笔记_026:折半查找(Java)

    目录 1 问题描述 2 解决方案 2.1 递归法 2.2 迭代法 1 问题描述 首先,了解一下何为折半查找?此处,借用<算法设计与分析基础>第三版上一段文字介绍: 2 解决方案 2.1 递 ...

  10. Python list替换元素

    替换直接对应位置赋值 假设现在班里仍然是3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 现在,Bart同学要转学走了,碰巧来了一个Paul同学,要更新班 ...