2012-11-24 44 views
1

我目前有一些代碼可以正確使用LibTomCrypt庫,但不能與Botan(我試圖將其轉換爲Botan)。Botan AES-256,32位InitialVector

我的(工作)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'有關。

無論如何,這就是我試圖與牡丹庫做:

// 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大小不是一個選項。

+3

AES的塊大小爲128位,IV爲一個塊。儘管你的妄想,加密算法消耗的IV將*爲16字節長。 –

+0

你應該使用常量而不是硬編碼文字,它們使代碼難以閱讀,難以改變,並且其他人更容易看到你的錯誤。如果存在的話,使用庫特定的(例如,我假定在LibTomCrypt中,AES對ctr_start()的第一個參數具有常量0)? –

回答

0

我們無法真正看到您使用的密碼模式。如果它是Rijndael-256,那麼塊大小將是256位。如果不是,那麼隨機數很可能會被庫中的某處切斷 - 在這種情況下,可能只有前128位被使用。

也就是說,代碼將無法工作,因爲您在第一個示例中使用了計數器模式加密,而在另一箇中使用了CBC模式加密。