Реализация Serpent в Crypto++

Я пытаюсь реализовать алгоритм Serpent с помощью Crypto++. Но я нашел небольшой документ, связанный с Cryptopp::Serpent (включая http://www.cryptopp.com/ ) Может ли кто-нибудь дать мне хорошо проработанный простой пример Serpent?


person user3558391    schedule 10.11.2014    source источник
comment
У вас все еще есть проблемы?   -  person jww    schedule 17.11.2014


Ответы (1)


С вики-страницы Crypto++ на Serpent:

AutoSeededRandomPool prng;

SecByteBlock key(Serpent::DEFAULT_KEYLENGTH);
prng.GenerateBlock(key, key.size());

byte iv[Serpent::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));

string plain = "CBC Mode Test";
string cipher, encoded, recovered;

/*********************************\
\*********************************/

try
{
    cout << "plain text: " << plain << endl;

    CBC_Mode< Serpent >::Encryption e;
    e.SetKeyWithIV(key, key.size(), iv);

    // The StreamTransformationFilter adds padding
    //  as required. ECB and CBC Mode must be padded
    //  to the block size of the cipher.
    StringSource ss1(plain, true, 
        new StreamTransformationFilter(e,
            new StringSink(cipher)
        ) // StreamTransformationFilter
    ); // StringSource
}
catch(const CryptoPP::Exception& e)
{
    cerr << e.what() << endl;
    exit(1);
}

/*********************************\
\*********************************/

// Pretty print
StringSource ss2(cipher, true,
    new HexEncoder(
        new StringSink(encoded)
    ) // HexEncoder
); // StringSource

cout << "cipher text: " << encoded << endl;

/*********************************\
\*********************************/

try
{
    CBC_Mode< Serpent >::Decryption d;
    d.SetKeyWithIV(key, key.size(), iv);

    // The StreamTransformationFilter removes
    //  padding as required.
    StringSource ss3(cipher, true, 
        new StreamTransformationFilter(d,
            new StringSink(recovered)
        ) // StreamTransformationFilter
    ); // StringSource

    cout << "recovered text: " << recovered << endl;
}
catch(const CryptoPP::Exception& e)
{
    cerr << e.what() << endl;
    exit(1);
}

Типичный вывод показан ниже. Обратите внимание, что каждый запуск будет давать разные результаты, поскольку ключ и вектор инициализации генерируются случайным образом.

$ ./Driver.exe
key: BE4295539F6BD1752FD0A80229EF8847
iv: 00963F59224794D5AD4252094358FBC3
plain text: CBC Mode Test
cipher text: CF2CF2547E02F6D34D97246E8042ED89
recovered text: CBC Mode Test

Там не так много. Вы можете поменять местами любой блочный шифр. Тот же код будет работать с AES, Camellia, 3DES и т. д.

person jww    schedule 10.11.2014
comment
Я какое-то время читаю газету о змее, но не могу ее понять. Является ли stackoverflow местом, где можно спросить о том, как работают алгоритмы? - person Chit Khine; 13.10.2016