博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows10 VS2017 C++使用crypto++库加密解密(AES)
阅读量:7057 次
发布时间:2019-06-28

本文共 2974 字,大约阅读时间需要 9 分钟。

版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/85262234

参考文章:

首先下载库:

使用vs2017打开cryptest.sln文件,解决方案选择“重订解决方案目标”,升级sdk。
编译库和dll文件
将生成的cryptopp.lib和cryptopp.dll放到项目文件夹,如果单独运行需要将dll文件拷贝到debug文件夹和生成的exe文件放在一起使用。
新建win32 c++控制台程序,工程->配置属性->vc++目录->包含目录,填写cryptopp的目录,需要使用其中的头文件.
编码:

#include "pch.h"#include 
#include
#include
#include
#include
#include
#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
(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
(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
(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;}

运行结果:

在这里插入图片描述

你可能感兴趣的文章
我的友情链接
查看>>
SLG手游Java服务器的设计与开发——网络通信
查看>>
spring-xml版本AspectJ环绕通知
查看>>
bootstrap-导航(垂直堆叠带分隔线的导航)
查看>>
安装tomcat-7.0.61图文
查看>>
游戏程序员的学习指南(必看)(二)
查看>>
手把手教你如何建立自己的Linux系统(LFS速成手册)
查看>>
初识 sqlite 与 content provider 学习笔记
查看>>
java--ftp的断点上传和断点下载
查看>>
11.SSH整合
查看>>
PowerShell记录脚本运行过程
查看>>
OpenSUSE下启动ssh和samba服务以及防火墙设置
查看>>
linux nethogs查看进程流量
查看>>
pip 安装报utf-8错解决办法
查看>>
django 中form在html中的简单使用
查看>>
lync 2013标准版安装
查看>>
【C#】在主线程中取消任务的运行方式
查看>>
POJ-2715(Water)
查看>>
防止集群多节点存储访问方法
查看>>
菜鸟学习Linux集群之概念篇
查看>>