【问题标题】:Botan AES-256, 32 bit InitialVectorBotan AES-256,32 位 InitialVector
【发布时间】:2012-11-24 18:51:00
【问题描述】:

我目前有一些代码可以与 LibTomCrypt 库一起正常工作,但不能与 Botan 一起使用(我正在尝试将其转换为 Botan)。

我的(工作的)LibTomCrypt 代码:

    // read the initial vector
    unsigned char iv[0x20];
    fseek(inputFile, 0x20, SEEK_SET);
    fread(iv, 0x20, 1, inputFile);

    // call ctr_start
    res = ctr_start(0, iv, key, 0x20, 0, 0, &ctr);

    if (res == 0)
    {
        printf("decrypting data...\n");

        // read the encrypyted data
        unsigned char cipheredText[0x3A8];
        fread(cipheredText, 0x3A8, 1, inputFile);

        // decrypt the data
        unsigned char uncipheredText[0x3A8];
        if (ctr_decrypt(cipheredText, uncipheredText, 0x3A8, &ctr) != 0)
        {
            fclose(inputFile);
            printf("ERROR: ctr_decrypt did not return 0\n");
            return -1;
        }
        if (ctr_done(&ctr) != 0)
        {
            fclose(inputFile);
            printf("ERROR: ctr_done did not return 0\n");
            return -1;
        }

        printf("writing decrypted data...\n");

        // get the decrypted path
        char *decPath = concat(fileName, ".dec", 4);

        // write the decrypted data to disk
        FILE *outFile = fopen(decPath, "w");
        fwrite(uncipheredText, 0x3A8, 1, outFile);
        fclose(outFile);
    }
    else
    {
        printf("ERROR: ctr_start did not return 0\n");
    }

如您所见,我的初始向量 (IV) 的大小为 0x20 (32)。我不知道为什么这会与这个库一起工作,但我去了这个方法,它似乎与 LibTomCrypt 中的“blocklen”有关。

无论如何,这就是我想要对 Botan 库做的事情:

// get the iv
t1Stream->setPosition(0x20);
BYTE rawIV[0x20];
t1Stream->readBytes(rawIV, 0x20);

// get the encrypted data
t1Stream->setPosition(0x40);
BYTE cipheredText[0x3A8];
t1Stream->readBytes(cipheredText, 0x3A8);

// setup the keys & IV
Botan::SymmetricKey symKey(key, 0x20);
Botan::InitializationVector IV(rawIV, 0x20);

// setup the 'pipe' ?
Botan::Pipe pipe(Botan::get_cipher("AES-256/CBC/NoPadding", symKey, IV, Botan::DECRYPTION));

但它总是在调用“get_cipher”时抛出这个:

terminate called after throwing an instance of 'Botan::Invalid_Key_Length'
  what():  Botan: AES-256 cannot accept a key of length 32

如果我将 IV 大小更改为 16,则它确实可以正常工作,但由于 IV 不正确,因此无法处理内容。

另外,不能在我的加密代码中更改 IV 大小。

【问题讨论】:

  • AES 的块大小为 128 位,IV 为一个块。尽管有你的错觉,加密算法消耗的 IV 是 16 个字节长。
  • 你真的应该使用常量而不是硬编码的文字,它们使代码难以阅读,难以更改,而且其他人更容易看到你的错误。如果存在,请使用特定于库的那些(例如,我假设在 LibTomCrypt 中,对于 ctr_start() 的第一个参数,AES 将具有常量 0?

标签: c++ aes botan


【解决方案1】:

我们无法真正看到您使用的密码模式。如果是 Rijndael-256,那么块大小将为 256 位。如果不是,则该 nonce 可能会被库剪切到某个地方 - 在这种情况下,很可能只使用了前 128 位。

也就是说,代码永远不会工作,因为您在第一个示例中使用计数器模式加密,而在另一个示例中使用 CBC 模式加密。

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 1970-01-01
    • 2017-11-07
    • 2021-01-27
    • 2018-04-04
    • 1970-01-01
    • 2015-04-10
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多