前面已经有一篇介绍使用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字符呢?

最新文章

  1. 跟我一起云计算(3)——hbase
  2. 简短的几句js实现css压缩和反压缩功能
  3. 改变HTML
  4. AMQ学习笔记 - 05. 客户端模板化
  5. flash图标插件
  6. matlab 中保存某几个变量
  7. [置顶] 让导入的Android项目,运行起来的方法。
  8. 在C#中实现串口通信的方法
  9. Ubuntu16.04安装mongodb
  10. asp.net core系列 53 IdentityServer4 (IS4)介绍
  11. 《通过C#学Proto.Actor模型》之Spawning
  12. IBGP规划
  13. 进度条(progress_bar)
  14. 实验1--用C语言编程四则运算
  15. dcoker 安装mysql和数据持久化
  16. 758B Blown Garland
  17. C# 数组深拷贝
  18. C# Http方式下载文件到本地类改进版
  19. spring 核心思想:AOP 理解
  20. geoserver 源码介绍

热门文章

  1. poj 3264 Balanced Lineup 题解
  2. 关闭使用ShellExecute打开的进程!!!!!
  3. 安装Office2007时出现1706错误的解决方案
  4. boost.asio学习笔记一、linux下boost库的安装
  5. Netbeans配合xdebug调试
  6. 【pyhon】nvshens图片批量下载爬虫1.01
  7. C语言 域名通配符实现
  8. css 小问题解决方法整理
  9. AndroidStudio简单的apk混淆
  10. iOS7 UIKit动力学-重力特性UIGravityBehavior