AES加密 C++调用Crypto++加密库 样例
这阵子写了一些数据加密的小程序,对照了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES)。听这名字就非常厉害的样子
预计会搜索到这文章的。对AES算法已经有了些基本了解了吧。以下先简介一下AES加密算法吧
(1)AES在password学中又称Rijndael加密法。是美国联邦政府採用的一种区块加密标准。2006年。高级加密标准已然成为对称密钥加密中最流行的算法之中的一个。
(2)AES加密数据块分组长度必须为128比特。密钥长度能够是128比特、192比特、256比特中的随意一个。(8比特 == 1字节)
(3)在CBC、CFB、OFB、CTR模式下除了密钥外,还须要一个初始化向IV。
(ECB模式不用IV)
关于AES很多其它的介绍,http://zh.wikipedia.org/wiki/AES。或者是百度百科吧
AES可使用的加密模式的介绍,http://blog.csdn.net/aaaaatiger/article/details/2525561
我使用的是Crypto++库,开发人员是Wei Dai,使用C++写的加密库。实现了非常多的加密算法,基本能满足我们的加密需求。使用起来也非常easy方便。这是官方站点http://www.cryptopp.com/
写这文章目的不是介绍AES算法,仅仅是想给一个小样例让大家參考一下而已,避免大家在查了大半天加密算法,看了老久AES原理,可就是就不知道怎么使用
(基本加解密过程是stackoverflow的一个小demo,我将它改动一下,实现了一个在两个程序之间,以文件做为介质的加解密的过程)
这里选的是CBC模式(其他模式调用也一样)
1、程序一:加密
#include <stdio.h> #include <iostream>
#include <fstream>
#include <sstream> #include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/modes.h> 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(ch, "%02x", static_cast<byte>(cipherText[i]));
cipherTextHex += ch;
} return cipherTextHex;
} void writeCipher(string output)
{
ofstream out("/tmp/cipher.data");
out.write(output.c_str(), output.length());
out.close(); cout<<"writeCipher finish "<<endl<<endl;
} int main()
{
string text = "hello zhuzhu dashen !";
cout<<"text : "<<text<<endl; initKV();
string cipherHex = encrypt(text);
cout<<"cipher : "<<cipherHex<<endl;
writeCipher(cipherHex); return 0;
}
程序二:解密
#include <stdio.h> #include <iostream>
#include <fstream>
#include <sstream> #include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/modes.h> 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 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("/tmp/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()
{
initKV();
string text = readCipher();
cout<<"text : "<<text<<endl;
return 0;
}
安装cryptopp: sudo apt-get install libcrypto++-dev
编译:g++ main.cpp -o main -lcryptopp
(以上内容仅供学习參考。若发现有误。请留言告知,谢谢。)
AES加密 C++调用Crypto++加密库 样例的更多相关文章
- PHPCMS中GET标签概述、 get 标签语法、get 标签创建工具、get 调用本系统演示样例、get 调用其它系统演示样例
一.get 标签概述 通俗来讲,get 标签是Phpcms定义的能直接调用数据库里面内容的简单化.友好化代码,她可调用本系统和外部数据,仅仅有你对SQL有一定的了解,她就是你的绝世好剑!也就是适合熟悉 ...
- 数据源加密-JDBC调用方式加密示例
package test; import org.gjt.mm.mysql.Driver; import java.sql.*;import java.util.Properties;import j ...
- boost.python编译及演示样例
欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581 linux编译boost的链接:http://bl ...
- fabric默认样例的分析
参考资料 http://www.bubuko.com/infodetail-2092748.html http://www.ithao123.cn/content-11117437.html http ...
- 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密
东篱 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密 2013/06/05 · tech PyCrypto 是流行的 Python 加密/解密库.但是其 AES 的 ECB 模 ...
- Crypto加密解密
crypto 模块提供了加密功能,包含对 OpenSSL 的哈希.HMAC.加密.解密.签名.以及验证功能的一整套封装.我们这里讲crypto AES算法加密 一.使用步骤 1.引入Crypto 1. ...
- AES Java加密 C#解密 (128-ECB加密模式)
在项目中遇到这么一个问题: java端需要把一些数据AES加密后传给C#端,找了好多资料,算是解决了,分享一下: import sun.misc.BASE64Decoder; import sun.m ...
- Qt使用AES加密算法对字符串进行加密
因工作需要,需要对字符串进行加密处理,在网上找了很长时间,终于找到了一个可以使用的aes加密算法.其源代码采用c++编写而成,但其头文件引用windows.h,经过修改部分代码,将#inc ...
- [转]java利用AES实现URL的参数加密
原文地址:http://h5566h.iteye.com/blog/1465426 很多时候需要在URL传参,希望URL参数能够加密,这里我结合了文章http://www.2cto.com/kf/20 ...
随机推荐
- 居然还有FindFirstChangeNotification函数
http://download.csdn.net/download/sololie/5966243
- C#写的客户端连接 php的服务器端的小例子
C#写的客户端连接 php的服务器端的小例子 php的server 端 <?php // server.php set_time_limit( 0 ); ob_implicit_flush(); ...
- ISO-8859-1乱码恢复
为兼容较旧的jQuery插件,我把jQuery版本号从2.1.x降到了1.8.x,这比加入jquery-migration插件要来得干脆. 我没把这两个版本号的的区别一一记下来,而是直接改个版本号号. ...
- 《转》python 网络编程
原地址:http://blog.163.com/benben_long/blog/static/19945824320121225918434/ 网络客户端: 1. 理解socket: socket是 ...
- 使用ffmpeg 对视频截图,和视频转换格式
//执行CMD命令方法 public static void CmdProcess(string command)//调用CMD { //实例化一个进程类 ...
- Android中的跨进程通信方法实例及特点分析(二):ContentProvider
1.ContentProvider简单介绍 在Android中有些数据(如通讯录.音频.视频文件等)是要供非常多应用程序使用的.为了更好地对外提供数据.Android系统给我们提供了Content P ...
- Linux Shell 之 我的第一个Shell程序
这里我首先会介绍一个Shell是什么,再介绍我的第一个Shell程序和从中总结的经验. 一.Shell是什么 在说我的这个Shell程序之前,还是先跟大家说说什么是Shell吧,相信Shell这个 ...
- OpenRisc-32-ORPSoC烧写外部spi flash
引言 经过前面的分析和介绍,我们对ORPSoC的启动过程(http://blog.csdn.net/rill_zhen/article/details/8855743)和 ORpSoC的debug子系 ...
- OSG+VS2010+win7环境搭建
Win7下 osg+vs2010环境搭建 一.相关准备 a) Osg源代码 当前最新版:OpenSceneGraph的3.0.0.zip 下载链接: http://www.openscenegraph ...
- Andriod中绘(画)图----Canvas的使用具体解释
转载请注明出处:http://blog.csdn.net/qinjuning 因为在网络上找到关于Canvas的使用都比較抽象,或许是我的逻辑思维不太好吧,总是感觉理解起来比較困难, 尤其是对 ...