参考文章:

https://blog.csdn.net/tangcaijun/article/details/42110319

首先下载库:

https://www.cryptopp.com/#download

使用vs2017打开cryptest.sln文件,解决方案选择“重订解决方案目标”,升级sdk。

编译库和dll文件

将生成的cryptopp.lib和cryptopp.dll放到项目文件夹,如果单独运行需要将dll文件拷贝到debug文件夹和生成的exe文件放在一起使用。

新建win32 c++控制台程序,工程->配置属性->vc++目录->包含目录,填写cryptopp的目录,需要使用其中的头文件.

编码:

#include "pch.h"
#include <iostream>
#include <fstream>
#include <aes.h>
#include <filters.h>
#include <modes.h>
#include <Windows.h> #pragma comment(lib, "cryptopp.lib") using namespace std; byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE]; void initKV()
{
memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE); // 或者也可以
/*
char tmpK[] = "1234567890123456";
char tmpIV[] = "1234567890123456";
for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)
{
key[j] = tmpK[j];
}
for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)
{
iv[i] = tmpIV[i];
}
*/
} string encrypt(string plainText)
{
string cipherText; //
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(cipherText));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plainText.c_str()), plainText.length() + 1);
stfEncryptor.MessageEnd(); string cipherTextHex;
for (int i = 0; i < cipherText.size(); i++)
{
char ch[3] = { 0 };
sprintf_s(ch, "%02x", static_cast<byte>(cipherText[i]));
cipherTextHex += ch;
} return cipherTextHex;
} void writeCipher(string output)
{
ofstream out("cipher.data");
out.write(output.c_str(), output.length());
out.close(); cout << "writeCipher finish " << endl << endl;
} string decrypt(string cipherTextHex)
{
string cipherText;
string decryptedText;
int i = 0;
while (true)
{
char c;
int x;
stringstream ss;
ss << hex << cipherTextHex.substr(i, 2).c_str();
ss >> x;
c = (char)x;
cipherText += c;
if (i >= cipherTextHex.length() - 2)break;
i += 2;
} //
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedText));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipherText.c_str()), cipherText.size()); stfDecryptor.MessageEnd(); return decryptedText;
} string readCipher()
{
ifstream in("cipher.data"); string line;
string decryptedText;
while (getline(in, line))
{
if (line.length() > 1)
{
decryptedText += decrypt(line) + "\n";
}
line.clear();
} cout << "readCipher finish " << endl;
in.close();
return decryptedText;
} int main()
{
string text = "What's up dude!";
cout << "text : " << text << endl; initKV();
string cipherHex = encrypt(text);
cout << "cipher : " << cipherHex << endl;
writeCipher(cipherHex); string decrpt_text = readCipher();
cout << "text : " << decrpt_text << endl; return 0;
}

运行结果:

Windows10 VS2017 C++使用crypto++库加密解密(AES)的更多相关文章

  1. 使用python进行加密解密AES算法

    使用python进行加密解密AES算法-代码分享-PYTHON开发者社区-pythoner.org 使用python进行加密解密AES算法 TY 发布于 2011-09-26 21:36:53,分类: ...

  2. AES加密解密——AES在JavaWeb项目中前台JS加密,后台Java解密的使用

    一:前言 在软件开发中,经常要对数据进行传输,数据在传输的过程中可能被拦截,被监听,所以在传输数据的时候使用数据的原始内容进行传输的话,安全隐患是非常大的.因此就要对需要传输的数据进行在客户端进行加密 ...

  3. Java对称与非对称加密解密,AES与RSA

    加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...

  4. 加密解密 AES RSA MD5 SHA

    加密解密: 对称加密:加密和解密相同秘钥.常见算法:AES, XTEA, 3DES. 非对称加密: 公钥加密 私钥加密. 加密和解密秘钥不同.常见算法:RSA OpenSSL> genrsa - ...

  5. 使用java实现对称加密解密(AES),非对称加密解密(RSA)

    对称加密:双方采用同样的秘钥进行加密和解密.特点是速度快,但是安全性没有非对称加密高 非对称加密:接收方生成的公有秘钥公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解 ...

  6. C#实现DES加密解密,AES加密解密

    DES算法描述简介: DES是Data Encryption Standard(数据加密标准)的缩写.它是由IBM公司研制的一种加密算法,美国国家标准局于1977年公布把它作为非机要部门使用的数据加密 ...

  7. C#加密解密(AES)

    using System; namespace Encrypt { public class AESHelper { /// <summary> /// 默认密钥-密钥的长度必须是32 / ...

  8. C#加密解密(AES)-AESHelper

    原文地址:https://ken.io/note/csharp-aesencrypt using System; namespace Encrypt { public class AESHelper ...

  9. C#调用Crypto++库AES ECB CBC加解密

    本文章使用上一篇<C#调用C++类库例子>的项目代码作为Demo.本文中,C#将调用C++的Crypto++库,实现AES的ECB和CBC加解密. 一.下载Crypto 1.进入Crypt ...

随机推荐

  1. Redis学习-sorted set数据类型

    sorted set 是有序集合,它在 set 的基础上增加了一个顺序属性,这一属性在添加修 改元素的时候可以指定,每次指定后,会自动重新按新的值调整顺序. zadd key score member ...

  2. Java笔记 #05# Java Native Interface

    参考资料:http://www.ntu.edu.sg/home/ehchua/programming/java/javanativeinterface.html(很详细) 看jdk源代码时,经常会看到 ...

  3. MySQL8.0 优化

    参考 https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html https://dev.mysql.com/doc/refman ...

  4. Docker 介绍及安装

    Docker介绍 Docker采用 C/S架构 Docker daemon 作为服务端接受来自客户的请求,并处理这些请求(创建.运行.分发容器). Docker基于go语言并遵从Apache2.0协议 ...

  5. spring boot 2整合mybatis

    mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解,一种是使用XML. 参考这篇文章动手跑了一个例子,稍微不同之处,原文是spring boot,这里改成了spr ...

  6. 剑指offer 02:替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题代码 public cla ...

  7. PHP深入浅出之命名空间(Namespace)的使用详解

    对于命名空间,官方文档已经说得很详细[查看],我在这里做了一下实践和总结. 命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只 ...

  8. MapReduce 踩坑 :Aggregation is not enabled. Try the nodemanager at IP:HOST

    原因:yarn-site.xml 中,有关mapreduce日志查看的aggregation未配置启用 解决:在yarn-site.xml 中加入以下配置 <property> <n ...

  9. VIP系统

    不同等级的VIP可以被_req调用,以实现分级控制 不同的VIP等级可以增加装备升级.强化成功的几率,掉率增加,VIP泡点等 VIP系统可以通过制作多功能Item.Creature及Gameobjec ...

  10. 在input中输入需要的数据,使用qrcode,点击生成二维码

    话不多说直接上代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...