Windows10 VS2017 C++使用crypto++库加密解密(AES)
参考文章:
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)的更多相关文章
- 使用python进行加密解密AES算法
使用python进行加密解密AES算法-代码分享-PYTHON开发者社区-pythoner.org 使用python进行加密解密AES算法 TY 发布于 2011-09-26 21:36:53,分类: ...
- AES加密解密——AES在JavaWeb项目中前台JS加密,后台Java解密的使用
一:前言 在软件开发中,经常要对数据进行传输,数据在传输的过程中可能被拦截,被监听,所以在传输数据的时候使用数据的原始内容进行传输的话,安全隐患是非常大的.因此就要对需要传输的数据进行在客户端进行加密 ...
- Java对称与非对称加密解密,AES与RSA
加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...
- 加密解密 AES RSA MD5 SHA
加密解密: 对称加密:加密和解密相同秘钥.常见算法:AES, XTEA, 3DES. 非对称加密: 公钥加密 私钥加密. 加密和解密秘钥不同.常见算法:RSA OpenSSL> genrsa - ...
- 使用java实现对称加密解密(AES),非对称加密解密(RSA)
对称加密:双方采用同样的秘钥进行加密和解密.特点是速度快,但是安全性没有非对称加密高 非对称加密:接收方生成的公有秘钥公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解 ...
- C#实现DES加密解密,AES加密解密
DES算法描述简介: DES是Data Encryption Standard(数据加密标准)的缩写.它是由IBM公司研制的一种加密算法,美国国家标准局于1977年公布把它作为非机要部门使用的数据加密 ...
- C#加密解密(AES)
using System; namespace Encrypt { public class AESHelper { /// <summary> /// 默认密钥-密钥的长度必须是32 / ...
- C#加密解密(AES)-AESHelper
原文地址:https://ken.io/note/csharp-aesencrypt using System; namespace Encrypt { public class AESHelper ...
- C#调用Crypto++库AES ECB CBC加解密
本文章使用上一篇<C#调用C++类库例子>的项目代码作为Demo.本文中,C#将调用C++的Crypto++库,实现AES的ECB和CBC加解密. 一.下载Crypto 1.进入Crypt ...
随机推荐
- 创建react项目的几种方法
前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...
- LSTMs 长短期记忆网络系列
RNN的长期依赖问题 什么是长期依赖? 长期依赖是指当前系统的状态,可能受很长时间之前系统状态的影响,是RNN中无法解决的一个问题. 如果从(1) “ 这块冰糖味道真?”来预测下一个词,是很容易得出“ ...
- P2053 [SCOI2007]修车(费用流)
P2053 [SCOI2007]修车 顾客平均等待的最小时间$=$等待总时间$/n$ 考虑只有1个技术人员时,$n$辆车等待总时间 $A_1+(A_1+A_2)+(A_1+A_2+A_3)+...+\ ...
- 常见的python的unittest用法
python的unittest好处是通过python脚本编写用例,每个用例可以单独调试初始化和清理动作,因为都是用例都是代码所以调试起来也很方便:它的缺点是得先学会python,难易程度见仁见智吧,对 ...
- Linux相关代码
Linux ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---- ...
- vi删除当前行的字符
x 删除当前光标下的字符dw 删除光标之后的单词剩余部分.d$ 删除光标之后的该行剩余部分.dd 删除当前行
- esay-ui学习笔记(一)
JavaScript prototype用法 prototype 属性使您有能力向对象添加属性和方法. object.prototype.name=value <script type=&quo ...
- P1908 逆序对
传送门 这题似乎不应该出现在这里.. 日常做法(归并): #include<iostream> #include<cstdio> #include<algorithm&g ...
- excel加密文件破解代码
1. AIT+F11 2. 代码 3. F5 Public Sub AllInternalPasswords()' Breaks worksheet and workbook structure ...
- 【linux总结】zsl
extundelete [root@xuegod63 ~]# lsof -p 43641 #一般用于查看木马进程,在读哪些文件 [root@xuegod63 ~]# lsof -i :22 #用于查看 ...