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; }
编译:g++ main.cpp -o main -lcryptopp
(以上内容仅供学习參考。若发现有误。请留言告知,谢谢。)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2015-01-16 关于C++ const 的全面总结
2015-01-16 error C3130: 内部编译器错误: 未能将插入的代码块写入PDB
2015-01-16 Android研究之游戏开发处理按键的响应
2015-01-16 C语言指针的初始化和赋值
2015-01-16 Cloudera CDH 5集群搭建(yum 方式)
2015-01-16 未将对象引用设置到对象的实例--可能出现的问题总结
2015-01-16 内存泄漏以及常见的解决方法