使用AES / Crypto++解密

Decrypting using AES / Crypto++

本文关键字:解密 Crypto++ AES 使用      更新时间:2023-10-16

我正在尝试弄清楚如何使用AES解密密文块。我正在使用加密++库 - 或者至少尝试使用该库。但我一无所获。我假设运行这个解密算法只需要几行代码,但我无法弄清楚。这就是我写的。现在开始笑吧:

#include <stdio.h>
#include <cstdlib>
#include <rijndael.h>
#include <sha.h>
using namespace std;
int main()
{
    // Decryption
    CTR_Mode< AES >::Decryption decryptor;
    decryptor.SetKeyWithIV( cbcKey, AES::DEFAULT_KEYLENGTH, cbcCipher );
}

谁能给我一个简短的教程,说明如何使用crypto++在给定解密密钥的情况下"简单地"解密16字节的密文块?他们的文档比您在上面看到的密文更神秘(至少对我来说),而且我通过搜索几乎没有找到帮助。 谢谢。

crypto++库网页上的FAQ包含指向"教程"的指针,去那里阅读。

谁能给我一个简短的教程,说明如何使用crypto++在给定解密密钥的情况下"简单地"解密16字节的密文块?

这来自Crypto++维基。它提供了一个点击率模式示例。

AutoSeededRandomPool prng;
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
prng.GenerateBlock( key, key.size() );
byte ctr[ AES::BLOCKSIZE ];
prng.GenerateBlock( ctr, sizeof(ctr) );
string plain = "CTR Mode Test";
string cipher, encoded, recovered;
/*********************************
*********************************/
try
{
    cout << "plain text: " << plain << endl;
    CTR_Mode< AES >::Encryption e;
    e.SetKeyWithIV( key, key.size(), ctr );
    // The StreamTransformationFilter adds padding
    //  as required. ECB and CBC Mode must be padded
    //  to the block size of the cipher. CTR does not.
    StringSource ss1( plain, true, 
        new StreamTransformationFilter( e,
            new StringSink( cipher )
        ) // StreamTransformationFilter      
    ); // StringSource
}
catch( CryptoPP::Exception& e )
{
    cerr << e.what() << endl;
    exit(1);
}
/*********************************
*********************************/
// Pretty print cipher text
StringSource ss2( cipher, true,
    new HexEncoder(
        new StringSink( encoded )
    ) // HexEncoder
); // StringSource
cout << "cipher text: " << encoded << endl;
/*********************************
*********************************/
try
{
    CTR_Mode< AES >::Decryption d;
    d.SetKeyWithIV( key, key.size(), ctr );
    // The StreamTransformationFilter removes
    //  padding as required.
    StringSource ss3( cipher, true, 
        new StreamTransformationFilter( d,
            new StringSink( recovered )
        ) // StreamTransformationFilter
    ); // StringSource
    cout << "recovered text: " << recovered << endl;
}
catch( CryptoPP::Exception& e )
{
    cerr << e.what() << endl;
    exit(1);
}

下面是运行示例程序的结果:

$ ./crytpopp-test.exe 
key: F534FC7F0565A8CF1629F01DB31AE3CA
counter: A4D16CBC010DACAA2E54FA676B57A345
plain text: CTR Mode Test
cipher text: 12455EDB41020E6D751F207EE6
recovered text: CTR Mode Test

事实上,Crypto++教程很难遵循。一般来说,为了使用Crypto++ API,你至少需要有一些关于密码学的基本知识,我建议这个密码学课程。

话虽如此,让我回答你的问题。由于它有点模棱两可,我假设您想使用 CTR 模式解密 AES 密码。作为输入,假设您有一个密码、IV 和密钥(全部在 std::string 中以十六进制表示)。

#include <iostream>
#include <string>
#include "crypto++/modes.h" // For CTR_Mode
#include "crypto++/filters.h" //For StringSource
#include "crypto++/aes.h" // For AES
#include "crypto++/hex.h" // For HexDecoder
int main(int argc, char* argv[]) {
    // Our input:
    // Note: the input was previously generated by the same cipher 
    std::string iv_string = "37C6D22FADE22B2D924598BEE2455EFC";
    std::string cipher_string = "221DF9130F0E05E7E87C89EE6A";
    std::string key_string = "7D9BB722DA2DC8674E08C3D44AAE976F";
    std::cout << "Cipher text: " << cipher_string << std::endl;
    std::cout << "Key: " << key_string << std::endl;
    std::cout << "IV: " << iv_string << std::endl;
    // 1. Decode iv: 
    // At the moment our input is encoded in string format...
    // we need it in raw hex: 
    byte iv[CryptoPP::AES::BLOCKSIZE] = {};
    // this decoder would transform our std::string into raw hex:
    CryptoPP::HexDecoder decoder;
    decoder.Put((byte*)iv_string.data(), iv_string.size());
    decoder.MessageEnd();
    decoder.Get(iv, sizeof(iv));
    // 2. Decode cipher:
    // Next, we do a similar trick for cipher, only here we would leave raw hex
    //  in a std::string.data(), since it is convenient for us to pass this
    // std::string to the decryptor mechanism:
    std::string cipher_raw;
    {
        CryptoPP::HexDecoder decoder;
        decoder.Put((byte*)cipher_string.data(), cipher_string.size());
        decoder.MessageEnd();
        long long size = decoder.MaxRetrievable();
        cipher_raw.resize(size);       
        decoder.Get((byte*)cipher_raw.data(), cipher_raw.size());
        // If we print this string it's completely rubbish: 
        // std::cout << "Raw cipher: " << cipher_raw << std::endl;
    }
    // 3. Decode the key:
    // And finally the same for the key:
    byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
    {
        CryptoPP::HexDecoder decoder;
        decoder.Put((byte*)key_string.data(), key_string.size());
        decoder.MessageEnd();
        decoder.Get(key, sizeof(key));
    }
    // 4. Decrypt:
    std::string decrypted_text;
    try {
            CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption d;
            d.SetKeyWithIV(key, sizeof(key), iv);
            CryptoPP::StringSource ss(
                cipher_raw, 
                true, 
                new CryptoPP::StreamTransformationFilter(
                    d,
                    new CryptoPP::StringSink(decrypted_text)
                ) // StreamTransformationFilter
            ); // StringSource
            std::cout << "Decrypted text: " << decrypted_text << std::endl;
    }
    catch( CryptoPP::Exception& e ) {
            std::cerr << e.what() << std::endl;
            exit(1);
    }
    return 0;
}

我在Ubutunu 14.04上编译了它,使用Crypto++562:

g++ -Wall -std=c++0x -o prog practicalAES.cpp -lcryptopp

如果我运行该程序,我会得到以下输出:

Cipher text: 221DF9130F0E05E7E87C89EE6A Key: 7D9BB722DA2DC8674E08C3D44AAE976F IV: 37C6D22FADE22B2D924598BEE2455EFC Decrypted text: CTR Mode Test

它在这里并不真正可见,但 Key 和 IV 的长度相同 - 16 字节(或 128 位)。这是块大小,因此此密码是AES-128。由于是CTR模式,因此不添加填充,并且密码和纯文本具有相同的字节数。

另请注意,60% 的代码涉及将字符串解码为十六进制,而解密本身只是最后一步(因此,如果您的输入数据为原始十六进制,则不需要解码)。

Crypto++ 的教程侧重于过滤器、源和接收器的使用,在我看来,这些都过于复杂。在您的情况下,代码实际上非常简单:

CTR_Mode< AES >::Decryption decryptor;
decryptor.SetKeyWithIV( cbcKey, AES::DEFAULT_KEYLENGTH, cbcCipher );
decryptor.ProcessData(output, input, size);
// Decrypt another piece of data with the same key
decryptor.Resynchronize(new_iv, new_iv_length);
decryptor.ProcessData(new_output, new_input, size);