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