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 ...
随机推荐
- 浅析Delphi Container库(有开源的DCLX)
与Java和C++相比,Delphi对容器的支持实在少得可怜.Java有强大的集合框架,C++更有STL,Delphi有什么呢,不就是TList几个小巧的列表类,而TCollection系列的类更多只 ...
- bunoj 34990(hash)
传送门:Justice String 题意:有两个串A,B,问是否存在A的一个子串S,S和B的长度相等,最多有2个字符不同.如果有多个,输出其实下标最小S的下标,没有输出-1. 分析:从A每个位置开始 ...
- C#之自己定义的implicit和explicit转换
在类型转换时常会遇到隐式转换和显式转换.那我们自己定义的类型要怎样去定义隐式转换和显式转换?我们来看一段代码 public class Rational { private Int32 _inner_ ...
- Struts 2.x仍然明显落后于时代。 Struts 2.x这一类老牌Web MVC开发框架仅能用于开发瘦客户端应用,无法用来开发对于交互体验要求更高的应用。
后来我在工作中陆续使用过Struts 1.x和Struts 2.x.我曾经把一个开源的基于Struts 1.x的自助式广告联盟应用移植到Spring MVC,还基于Struts 2.x做过网站开发.S ...
- 【UVA】10012 - How Big Is It?(暴力)
使用DFS枚举所有的安排.每次加入后,当一个圆.他的立场是最大的,并已加入了圆环中的所有切线位置前面. 14383635 10012 option=com_onlinejudge&Itemid ...
- hdu4223(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4223 由于n范围较小,完全可暴力... #include <cstdio> #includ ...
- 2014 CSDN博文大赛终于获奖名单发布
博文大赛第二阶段(2014年7月15日-2014年8月10日)已经结束,决赛获奖名单已在8月11日出炉. 现将获奖名单发布: 移动开发 NO.1 罗升阳 Luoshengyang S ...
- [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
XTools Pro 是一套为ArcGIS平台设计的矢量空间分析. 形状转换和表管理扩展工具,大大增强了 ArcGIS 的功能,使用该工具能够提高 ArcGIS 用户的效率和性能. XTools Pr ...
- red hat Linux 使用CentOS yum源更新
red hat linux是商业版软件,没有经过注册是无法使用红帽 yum源更新软件的,使用CentOS源更新操作如下: 1.删除red hat linux 原有的yum 源 rpm -aq | gr ...
- A Game of Thrones(8) - Bran
The hunt left at dawn. The king wanted wild boar at the feast tonight. Prince Joffrey rode with his ...