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

C++封装:

  1. #include "zyaes.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. using namespace CryptoPP;
  6.  
  7. CZYAes::CZYAes()
  8. {
  9. byte byteKey[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08,
  10. 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};
  11. byte byteIv[] = {0x01,0x02,0x03,0x04,0x01,0x02,0x03,0x04, 0x01,0x02, 0x03,0x04,0x01,0x02,0x03,0x04};
  12. memcpy(m_arrByteKey, byteKey, sizeof(byte) * );
  13. memcpy(m_arrByteIv, byteIv, sizeof(byte) * );
  14. m_nKeyLen = ;
  15. }
  16.  
  17. CZYAes::~CZYAes()
  18. {
  19. }
  20.  
  21. // set key and iv
  22. void CZYAes::SetKey(std::string strKey, std::string strIv)
  23. {
  24. int nKeyLen = ;
  25. int nIvLen = ;
  26. memset(m_arrByteKey, , sizeof(byte) * );
  27. memset(m_arrByteIv, , sizeof(byte) * );
  28.  
  29. if (strKey.length() >= )
  30. {
  31. nKeyLen = ;
  32. }
  33. else
  34. {
  35. nKeyLen = strKey.length();
  36. }
  37. memcpy(m_arrByteKey, strKey.c_str(), sizeof(byte) * nKeyLen);
  38.  
  39. if (!strIv.empty())
  40. {
  41. if (strIv.length() >= )
  42. {
  43. nIvLen = ;
  44. }
  45. else
  46. {
  47. nIvLen = strIv.length();
  48. }
  49. memcpy(m_arrByteIv, strIv.c_str(), sizeof(byte) * nIvLen);
  50. }
  51. }
  52.  
  53. // encrypt
  54. std::string CZYAes::Encrypt(const std::string &strText)
  55. {
  56. std::string strCipher;
  57. CBC_Mode<AES>::Encryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
  58. StringSource(strText, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strCipher)));
  59.  
  60. std::string strEncoded;
  61. StringSource s2(strCipher, true,
  62. new HexEncoder(
  63. new StringSink(strEncoded)
  64. ) // HexEncoder
  65. ); // StringSource
  66. return strEncoded;
  67. }
  68.  
  69. // decrypt
  70. std::string CZYAes::Decrypt(const std::string &strCipher)
  71. {
  72. std::string strDecoded;
  73. StringSource s2(strCipher, true,
  74. new HexDecoder(
  75. new StringSink(strDecoded)
  76. ) // HexEncoder
  77. ); // StringSource
  78.  
  79. std::string strText;
  80. CBC_Mode<AES>::Decryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
  81. StringSource(strDecoded, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strText)));
  82.  
  83. return strText;
  84. }

实现文件

头文件

C封装:

  1. //*****************************************************************************
  2. //@FileName : aes256.cpp
  3. //@Version : v1.0.0
  4. //@Author : xiaoc
  5. //@Date : 2015/03/13
  6. //*****************************************************************************
  7. #include "zyaes.h"
  8. #include "zyaes256.h"
  9. using namespace std;
  10.  
  11. string Encrypt(const string &strText, const string &strKey/* = "" */)
  12. {
  13. std::string strCipher;
  14. int nLen = ;
  15. CZYAes aes;
  16.  
  17. if (!strKey.empty())
  18. {
  19. aes.SetKey(strKey, "");
  20. }
  21. strCipher = aes.Encrypt(strText);
  22. return strCipher;
  23. }
  24.  
  25. string Decrypt(const string &strCipher, const string &strKey/* = "" */)
  26. {
  27. std::string strText;
  28. int nLen = ;
  29. CZYAes aes;
  30.  
  31. if (!strKey.empty())
  32. {
  33. aes.SetKey(strKey, "");
  34. }
  35. strText = aes.Decrypt(strCipher);
  36. return strText;
  37. }

实现文件

  1. //*****************************************************************************
  2. //@FileName : zyaes256.h : the c interface of class zyaes
  3. //@Version : v1.0.0
  4. //@Author : xiaoc
  5. //@Date : 2015/03/13
  6. //*****************************************************************************
  7. #ifndef _ZYAES256_H_
  8. #define _ZYAES256_H_
  9. #include <string>
  10.  
  11. //*****************************************************************************
  12. //@strText : 需要加密的数据
  13. //@strKey : 加密用的密钥
  14. //@return : 返回加密之后的数据
  15. //*****************************************************************************
  16. std::string Encrypt(const std::string &strText, const std::string &strKey = "");
  17.  
  18. //*****************************************************************************
  19. //@strText : 需要解密的数据
  20. //@strKey : 解密用的密钥
  21. //@return : 返回解密之后的数据
  22. //*****************************************************************************
  23. std::string Decrypt(const std::string &strCipher, const std::string &strKey = "");
  24. #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. C# SMTP邮件发送程序

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  2. 关于substring的char[]共享

    我们知道,对于一个较大的String对象假设从中获取一个子串.jdk默认子串的char[]是共享原串的char[].即子串的char[]是原串的char[]中的一部分, 这样对于一个原串多个子串的情况 ...

  3. 修复错误配置/etc/fstab文件导致系统无法正常启动

      1.文件介绍 /etc/fstab这个文件描述系统中各种文件系统的信息,应用程序读取这个文件,然后根据其内容进行自动挂载的工作.作为系统配置文件,fstab通常都位于/etc目录下,它包括了所有分 ...

  4. 设计模式之Programming to an Interface, not anImplementation 程序指向接口,而不是实现

    Class inheritance is basically just a mechanism for extending an application's functionality by reus ...

  5. JSTL核心标签

    JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...

  6. webpack 通用模块(每个页面都用到的js)编译

    1.项目目录 2.配置文件:webpack.config.js var htmlWebpackPlugin = require('html-webpack-plugin'); var webpack ...

  7. MS SQL得到指定日期的当月月末

    MS SQL得到指定日期的当月月末 declare @ddate date ,,)) select @ddate --2016-01-31 declare @ddatetime datetime ,, ...

  8. Linux下统计当前文件夹下的文件个数、目录个数(转)

    1) 统计当前文件夹下文件的个数 代码如下: ls -l |grep "^-"|wc -l 2) 统计当前文件夹下目录的个数 代码如下: ls -l |grep "^d& ...

  9. UI_storyboard实现页面回调

    新建类 注意继承关系 #import <UIKit/UIKit.h> @interface CustomPopIt : UIStoryboardSegue @end #import &qu ...

  10. Android中对Handle机制的理解

    一.重要參考资料  [參考资料]     眼下来看,以下的几个网址中的内容质量比較不错.基本不须要再读别的网址了. 1.android消息机制一     http://xtfncel.javaeye. ...