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 ...
随机推荐
- 《CSS世界》读书笔记(二)
<!-- <CSS世界> 张鑫旭著 --> 块级元素:水平流上只能单独显示一个元素 <li>元素默认的display值是list-item,是块级元素 块级盒子( ...
- GoldenGate抽取Informix数据库安装及配置
GoldenGate抽取Informix数据库安装及配置 本次测试架构 l 在中间机上安装informix csdk4.10版本,并编译配置unixODBC; l 在中间机上安装ogg for I ...
- web前端开发常用组件
web前端开发常用组件 1. 对话框(dialog):jbox(适合对话框等其它功能).colorbox(也很强大,可以弥补jbox图片轮播的落点), 这二者基本能搞定所有对话框的情况 2. ...
- php 禁止屏蔽类
1.禁止右键 <body oncontextmenu=return(false)> 2.禁止选择 <body onselectstart="return false&quo ...
- 剑指offer(46)孩子们的游戏
题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...
- Python3 tkinter基础 Menubutton 点击按钮出现下拉菜单
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Vue:(五)axios
Axios是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.axios主要是用于向后台发起请求的,还有在请求中做更多可控功能.官方不再维护vue-resource,推 ...
- python检测服务器端口
import socket sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(10) try: sk.conne ...
- 【linux总结】zsl
extundelete [root@xuegod63 ~]# lsof -p 43641 #一般用于查看木马进程,在读哪些文件 [root@xuegod63 ~]# lsof -i :22 #用于查看 ...
- mysql 常用sql语句
权限 撤销权限revoke all on *.* from 'root'@'192.168.0.197' ; 撤销权限revoke all on *.* from 'xx_db' @'%'; 给指定用 ...