这阵子写了一些数据加密的小程序,对照了好几种算法后,选择了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++加密库 样例的更多相关文章

  1. PHPCMS中GET标签概述、 get 标签语法、get 标签创建工具、get 调用本系统演示样例、get 调用其它系统演示样例

    一.get 标签概述 通俗来讲,get 标签是Phpcms定义的能直接调用数据库里面内容的简单化.友好化代码,她可调用本系统和外部数据,仅仅有你对SQL有一定的了解,她就是你的绝世好剑!也就是适合熟悉 ...

  2. 数据源加密-JDBC调用方式加密示例

    package test; import org.gjt.mm.mysql.Driver; import java.sql.*;import java.util.Properties;import j ...

  3. boost.python编译及演示样例

    欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581 linux编译boost的链接:http://bl ...

  4. fabric默认样例的分析

    参考资料 http://www.bubuko.com/infodetail-2092748.html http://www.ithao123.cn/content-11117437.html http ...

  5. 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密

    东篱 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密 2013/06/05 · tech PyCrypto 是流行的 Python 加密/解密库.但是其 AES 的 ECB 模 ...

  6. Crypto加密解密

    crypto 模块提供了加密功能,包含对 OpenSSL 的哈希.HMAC.加密.解密.签名.以及验证功能的一整套封装.我们这里讲crypto AES算法加密 一.使用步骤 1.引入Crypto 1. ...

  7. AES Java加密 C#解密 (128-ECB加密模式)

    在项目中遇到这么一个问题: java端需要把一些数据AES加密后传给C#端,找了好多资料,算是解决了,分享一下: import sun.misc.BASE64Decoder; import sun.m ...

  8. Qt使用AES加密算法对字符串进行加密

          因工作需要,需要对字符串进行加密处理,在网上找了很长时间,终于找到了一个可以使用的aes加密算法.其源代码采用c++编写而成,但其头文件引用windows.h,经过修改部分代码,将#inc ...

  9. [转]java利用AES实现URL的参数加密

    原文地址:http://h5566h.iteye.com/blog/1465426 很多时候需要在URL传参,希望URL参数能够加密,这里我结合了文章http://www.2cto.com/kf/20 ...

随机推荐

  1. OpenCv调用摄像头拍照代码

    近期在研究OpenCv对摄像头的调用.现将代码贴出,供大家批评指正. 1.申明 #include"./opencv2/opencv.hpp" #ifdef _DEBUG #prag ...

  2. ExtJs4 笔记(5) Ext.Button 按钮

    id="li2"></li> <li id="li3"></li> </ul> </div> ...

  3. Swift - 操作SQLite数据库(引用SQLite3库)

    SQLite轻量级数据库在移动应用中使用非常普遍,但是目前的库是C编写的,为了方便使用,对SQLite相关的操作用Swift进行了封装.这个封装代码使用了一个开源项目SQLiteDB,地址是:http ...

  4. 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)

    原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...

  5. windows程序员进阶系列:《软件调试》之Win32堆

     win32堆及内部结构 Windows在创建一个新的进程时会为该进程创建第一个堆,被称为进程的默认堆.默认堆的句柄会被保存在进程环境块_PEB的ProcessHeap字段中. 要获得_PEB的地址, ...

  6. 300M无线路由器 TL-WR842N - TP-LINK官方网站

    300M无线路由器 TL-WR842N - TP-LINK官方网站 300M无线路由器TL-WR842N 11N无线技术.300Mbps无线速率 2x2MIMO架构.CCA技术,提升无线稳定性.扩大无 ...

  7. Python3.2官方文档翻译--实例对象和方法对象

    6.3.3 实例对象 如今我们用实例对象做什么呢?实例对象唯一可用的操作就是属性引用.如今有两种合法的属性名称:数据属性和方法. 数据属性相当于smallTalk中的实例变量,C++中的数据成员.数据 ...

  8. [C++]C++中的运行时类型检测

    Date:2014-1-3 Summary: 使用C++中的运行时类型检测.(文章重点在于记录本人的使用情况,并非深层讨论RTTI) Contents:写习惯C#的我,在C++依然存在哪些.NET的惯 ...

  9. Urxvt - awesome

    Urxvt - awesome Urxvt From awesome Jump to: navigation, search rxvt-unicode (urxvt for short) is a c ...

  10. C++&&Mysql&&codeblocks

    #include <iostream> #include <stdio.h> #include <winsock2.h> #include <mysql.h& ...