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; }
运行结果:
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Windows10 VS2017 C++ Json解析(使用jsoncpp库)
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/85259297 1.项目必须是win32 2.生成的lib_json.lib放到工程目录下 3.incldue的头文件放到工程目录,然后设置工程->属性->配置属性->vc++目录->包含目录 4.工程->属性->配置属性->c/c+±>代码生成->运行库,设置运行库为多线程调试 (/MTd) 首先从github下载最新版jsoncpp,0.10.7https://github.com/open-source-parsers/jsoncpp/releases 在makefile目录的msvc2010打开工程进行编译,将库文件makefiles\msvc2010\Debug\lib_json.lib添加到工程目录,如上步骤2,然后将include目录拷贝到工程目录,按照上边步骤3设置,然后如上步骤4调整设置 解析字符串json,写码: #include "pch.h" #include...
- 下一篇
Java 8 开发的 4 大顶级技巧
我使用Java 8编码已经有些年头,既用于新的应用程序,也用来迁移现有的应用,感觉是时候写一些我发现的非常有用的“最佳实践”。 我个人并不喜欢“最佳实践”这个说法,因为它意味着“一刀切”的解决方案,而编码不可能是这样的工作方式——我们需要亲自去发现什么样的解决方案才是有效的。 但是我发现了一些Java 8代码中可以帮助我们的一些选择,让我们一起来看看吧。 1.Optional Optional是一个严重被低估的功能,并且有潜力删除很多困扰我们的NullPointerExceptions。这在代码边界中(要么是正在使用的API,要么是正在暴露的API)特别有用,因为它允许你和你的调用代码来推理所期待的东西。 然而,不加思考和设计就应用Optional可能会导致影响大量的类,并可能导致可读性更差。下面是一些关于如何高效使用Optional的技巧。 Optional应该只用于返回类型 ……不是参数,也不是字段。幸运的是,IntelliJ IDEA的让你打开检查来查看是是否遵循这些建议。 Optional值应在遇到它们的地方中处理。IntelliJ IDEA的建议会防止代码Optional泄漏...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Mario游戏-低调大师作品
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS7,CentOS8安装Elasticsearch6.8.6
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS6,CentOS7官方镜像安装Oracle11G
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2配置默认Tomcat设置,开启更多高级功能