我尝试使用Crypto ++实现Serpent算法。 但是我找到了与Cryptopp :: Serpent相关的小文件(包括http://www.cryptopp.com/) 有人能给我一个很好的简单的Serpent例子吗?
答案 0 :(得分:0)
来自Crypto++ wiki page on 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等。