AES ECB模式加解密
来源:互联网 发布:南京众东项目数据分析 编辑:程序博客网 时间:2024/04/29 04:15
AES ECB模式加解密
使用cryptopp完成AES的ECB模式进行加解密。
分组密码有五种工作体制:1. 电码本模式(Electronic Codebook Book (ECB));2.密码分组链接模式(Cipher Block Chaining (CBC));3.计算器模式(Counter (CTR));4.密码反馈模式(Cipher FeedBack (CFB));5.输出反馈模式(Output FeedBack (OFB))。
AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个。(8比特 == 1字节)
在CBC、CFB、OFB、CTR模式下除了密钥外,还需要一个初始化向IV。(ECB模式不用IV)
程序 aes_ecb.cpp
// g++ -g3 -ggdb -O0 -Wall -Wextra -Wno-unused -o aes_ecb aes_ecb.cpp /usr/local/lib/libcryptopp.a -I/usr/local/include/cryptopp/#include "osrng.h"using CryptoPP::AutoSeededRandomPool;#include <iostream>using std::cin;using std::cout;using std::cerr;using std::endl;#include <string>using std::string;#include <cstdlib>using std::exit;#include "cryptlib.h"using CryptoPP::Exception;#include "hex.h"using CryptoPP::HexEncoder;using CryptoPP::HexDecoder;#include "filters.h"using CryptoPP::StringSink;using CryptoPP::StringSource;using CryptoPP::StreamTransformationFilter;#include "aes.h"using CryptoPP::AES;#include "modes.h"using CryptoPP::ECB_Mode;int main(int argc, char* argv[]){ AutoSeededRandomPool prng; byte key[AES::DEFAULT_KEYLENGTH]; prng.GenerateBlock(key, sizeof(key)); string plain; string cipher, encoded, recovered; /*********************************\ \*********************************/ // Pretty print key encoded.clear(); StringSource(key, sizeof(key), true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "key: " << encoded << endl; /*********************************\ \*********************************/ try { cout << "input plain text: "; cin >> plain; ECB_Mode< AES >::Encryption e; e.SetKey(key, sizeof(key)); // The StreamTransformationFilter adds padding // as required. ECB and CBC Mode must be padded // to the block size of the cipher. StringSource(plain, true, new StreamTransformationFilter(e, new StringSink(cipher) ) // StreamTransformationFilter ); // StringSource } catch(const CryptoPP::Exception& e) { cerr << e.what() << endl; exit(1); } /*********************************\ \*********************************/ // Pretty print encoded.clear(); StringSource(cipher, true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "cipher text: " << encoded << endl; /*********************************\ \*********************************/ try { ECB_Mode< AES >::Decryption d; d.SetKey(key, sizeof(key)); // The StreamTransformationFilter removes // padding as required. StringSource s(cipher, true, new StreamTransformationFilter(d, new StringSink(recovered) ) // StreamTransformationFilter ); // StringSource cout << "recovered text: " << recovered << endl; } catch(const CryptoPP::Exception& e) { cerr << e.what() << endl; exit(1); } /*********************************\ \*********************************/ return 0;}
运行
# ./aes_ecb
key: 24CB312D72C57B9EE9494B3E754C972B
input plain text: 1234567890
cipher text: D529A3245B1547375F1F01B33BF35738
recovered text: 1234567890
参考网址:http://cryptopp.com/wiki/Advanced_Encryption_Standard
0 0
- AES ECB模式加解密
- AES加密解密(ECB模式)
- Java利用 AES/ECB/PKCS5Padding 算法加解密
- Java利用 AES/ECB/PKCS5Padding 算法加解密
- PHP和Java AES 128 ECB 加解密(PKCS5Padding)
- golang实现AES ECB模式的加密和解密
- AES Java加密 C#解密 (128-ECB加密模式)
- AES128-ecb加解密
- AES加解密算法的模式介绍
- AES加解密算法的模式介绍
- 3DES加解密中的ECB模式-wp8开发
- JavaScript的CryptoJS AES/ECB/NoPadding加密,可与JAVA objective-c 进行同步加解密
- javascript实现AES加密解密(ECB&CBC)
- PHP--JAVA AES(ECB)加密解密
- AES加解密算法
- aes加解密
- iOS-AES加解密
- erlang aes 加解密
- HDFS NodeLabel特性
- Anddroid 适配多种屏幕
- 第五周项目1-三角形类雏形(3)
- OC-UIScrollView&UIPageControl
- 题目:影子定位
- AES ECB模式加解密
- C++中如何使函数返回数组
- 第五周项目1-三角形类雏形(4)
- 记一次javascript和Android代码互调的bug
- Activity做伪弹窗
- 2 mov赋值指令与内联汇编
- python单元测试--这个必须有
- 链表反转系列
- android利用接口回掉传递数据