博坦 AES-256,32 位初始矢量

Botan AES-256, 32 bit InitialVector

本文关键字:AES-256 博坦      更新时间:2023-10-16

我目前有一些代码可以与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 0n");
            return -1;
        }
        if (ctr_done(&ctr) != 0)
        {
            fclose(inputFile);
            printf("ERROR: ctr_done did not return 0n");
            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 0n");
    }

如您所见,我的初始向量 (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 大小不是一种选择。

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

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