目前,我有一些与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大小不是一个选项。
发布于 2012-12-30 00:02:33
我们看不出你用的是什么密码模式。如果它是Rijndael-256,那么块大小将是256位。如果不是的话,那么现在可能会被库剪切--在这种情况下,很可能只使用前128位。
尽管如此,代码将永远无法工作,因为您在第一个示例中使用计数器模式加密,而在另一个示例中使用CBC模式加密。
https://stackoverflow.com/questions/13544426
复制相似问题