首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >毒蛇在Crypto++中的实现

毒蛇在Crypto++中的实现
EN

Stack Overflow用户
提问于 2014-11-10 06:06:46
回答 1查看 1.2K关注 0票数 1

我尝试用Crypto++实现蛇形算法。但是我发现很少有关于Cryptopp::Serpent (包括http://www.cryptopp.com/)的文档,有人能给我一个工作良好的简单的蛇例子吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-10 06:54:07

来自毒蛇上的Crypto++维基页面

代码语言:javascript
复制
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);
}

典型的输出如下所示。注意,每次运行都会产生不同的结果,因为键和初始化向量是随机生成的。

代码语言:javascript
复制
$ ./Driver.exe
key: BE4295539F6BD1752FD0A80229EF8847
iv: 00963F59224794D5AD4252094358FBC3
plain text: CBC Mode Test
cipher text: CF2CF2547E02F6D34D97246E8042ED89
recovered text: CBC Mode Test

没什么大不了的。你可以交换任何块密码进出。同样的代码将适用于AES、Camellia、3 AES等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26837758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档