1

現在、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」と関係があるようです。

とにかく、これは私がボタンライブラリでやろうとしていることです:

// 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サイズを変更することはできません。

4

1 に答える 1

0

使用している暗号モードを実際に確認することはできません。Rijndael-256 の場合、ブロック サイズは 256 ビットになります。そうでない場合、ナンスはライブラリによってどこかでカットされる可能性があります。その場合、最初の 128 ビットのみが使用される可能性があります。

とはいえ、最初の例ではカウンター モード暗号化を使用し、もう 1 つの例では CBC モード暗号化を使用しているため、コードは機能しません。

于 2012-12-30T00:02:33.787 に答える