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 ...
随机推荐
- Uncaught SyntaxError: Unexpected token <解决方法
最近剥离基础框架的公共部分,早上有个页面部分流程未加载出来,报了Uncaught SyntaxError: Unexpected token <,网上搜了下 错误原因:js脚本中非正常引用外部的 ...
- 分类统计的controller和service
SpringMVC框架下的 部分代码: Controller控制器: @Resource ReviewTitleService reviewTitleService;//调用ReviewTitleSe ...
- oracle 查询表结构
SELECT t1.Table_Name AS "表名称", t3.comments AS "表说明", t1.Column_Name AS "字段名 ...
- Learning-Python【12】:装饰器
一.什么是装饰器 器:工具 装饰:为被装饰对象添加新功能 装饰器本身可以是任意可调用的对象,即函数 被装饰的对象也可以是任意可调用的对象,也是函数 目标:写一个函数来为另外一个函数添加新功能 二.为何 ...
- JVM运行时内存模型
JDK1.7版本图 一,栈 基本数据类型的局部变量是直接保存在栈中. 栈帧:一个栈里面会包含多个栈帧,每一个栈帧代表一个方法的开始到结束,它涵盖了整个方法运行期间所有的操作和数据 栈帧 1:局部变 ...
- 微信小程序计算器后后续
改的眼睛都要瞎了,总算是知道问题出哪了 最后一段 在等号里面计算输入的数组,这个判断的主要操作是将输入的数据的数组进行数和符号的拆分然后再计算,把数按字符串输入数组,然后将数和符号进行拆分 ,最后通过 ...
- Fatal error: ENOSPC: System limit for number of file watchers reached
参考https://www.jianshu.com/p/4d2edd55b471 echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/ ...
- linux存储管理之逻辑卷
LVM管理 ====================================================================================创建LVMVG扩展/ ...
- Pycharm:设置Tab键一次性为4个空格
File > Settings > Editor > Code Style > Python
- WEB UI做TREE
效果图: 原本的普通搜索帮助,改成上面这样层级的搜索帮助.这里只做了两级. 一,新建一个TREE节点 1.新建tree结构:ZGRTEXT 2.新建树叶节点处理类: 修改超类为CL_BSP_WD_TR ...