AES CBC解密有效,CTR不起作用 [英] AES CBC decryption works, CTR does not

查看:71
本文介绍了AES CBC解密有效,CTR不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AES CBC和CTR模式解密数据.密文以16字节IV开头.

I am trying to decrypt data using AES CBC and CTR mode. The ciphertext has been prepended with the 16 byte IV.

我的密文数据具有以下格式:

I have my ciphertext data in the following format:

vector<vector<byte>> CBCMessages;
vector<vector<byte>> CBCKeys;
vector<vector<byte>> CTRMessages;
vector<vector<byte>> CTRKeys;

我正在使用Crypto ++解密数据.这是我的代码:

I am using Crypto++ to decrypt the data. This is my code:

for (int i = 0; i < CBCMessages.size(); i++)
{
    std::string decryptedtext;

    // split IV from ciphertext
    byte iv[16];
    std::copy(CBCMessages[i].begin(), CBCMessages[i].begin()+16, iv);
    CBCMessages[i].erase(CBCMessages[i].begin(), CBCMessages[i].begin()+16);

    // this block works fine
    AES::Decryption aesDecryption(&(CBCKeys[i][0]), CBCKeys[i].size());
    CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
    StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( &(CBCMessages[i][0]) ), CBCMessages[i].size() );
    stfDecryptor.MessageEnd();

    std::cout << decryptedtext << std::endl;
}

for (int i = 0; i < CTRMessages.size(); i++)
{
    std::string decryptedtext;

    // split IV from ciphertext
    byte iv[16];
    std::copy(CTRMessages[i].begin(), CTRMessages[i].begin()+16, iv);
    CTRMessages[i].erase(CTRMessages[i].begin(), CTRMessages[i].begin()+16);

    // this block produces junk
    AES::Decryption aesDecryption(&(CTRKeys[i][0]), CTRKeys[i].size());
    CTR_Mode_ExternalCipher::Decryption ctrDecryption( aesDecryption, iv );
    StreamTransformationFilter stfDecryptor(ctrDecryption, new CryptoPP::StringSink( decryptedtext ) );
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( &(CTRMessages[i][0]) ), CTRMessages[i].size() );
    stfDecryptor.MessageEnd();

    std::cout << decryptedtext << std::endl;

    // try again with different method - this works fine
    decryptedtext.clear();
    CTR_Mode< AES >::Decryption d;
    d.SetKeyWithIV( &(CTRKeys[i][0]), CTRKeys[i].size(), iv, 16 );
    StringSource( reinterpret_cast<const unsigned char*>( &(CTRMessages[i][0]) ), CTRMessages[i].size(), true, 
        new StreamTransformationFilter( d,
            new StringSink( decryptedtext )
        )
    );

    std::cout << decryptedtext << std::endl;
}

如您所见,中间块(用于CTR解密的第一个块)产生垃圾输出.请注意,该块实际上应该与用于CBC解密的块完全相同.

As you can see, the middle block (first block for CTR decryption) produces junk output. Note that this block should actually be pretty identical to the block used for CBC decryption.

用于CBC解密的块基本上是从此FAQ条目复制的(在2005年10月21日上午10:38 jeffrey之前答复).然后,我更改了此块,以将其用于CTR解密,这是它无法正常工作的时候.第二个CTR块的灵感来自示例程序"部分此处.

The block used for CBC decryption is basically copied from this FAQ entry (answer by 2005-Oct-21 10:38am jeffrey). I have then altered this block to use it for CTR decryption which is when it failed to work. The second CTR block is inspired by the "Sample Program" section here.

第一个CTR代码块中的问题似乎是什么?

What seems to be the problem in the first CTR code block?

推荐答案

可能是因为

AES :: Decryption aesDecryption(&(CTRKeys [i] [0]),CTRKeys [i] .size());

AES::Decryption aesDecryption(&(CTRKeys[i][0]), CTRKeys[i].size());

https://upload.wikimedia.org/wikipedia/commons/3/3c/CTR_decryption_2.svg

点击率模式需要 AES ::加密来解密密码测试

CTR Mode needs AES::Encryption to decrypt the ciphertest

https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

这篇关于AES CBC解密有效,CTR不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆