我目前有一些代码可以与 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");
}
如您所见,我的初始 vector (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,那么 block 大小将是 256 位。如果不是,则 nonce 可能会被库在某处剪切 - 在这种情况下,很可能只使用前 128 位。
也就是说,代码永远不会工作,因为您在第一个示例中使用计数器模式加密,而在另一个示例中使用 CBC 模式加密。
关于c++ - 牡丹 AES-256,32 位 InitialVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13544426/