【问题标题】:How to use Shamir Secret Sharing Class in Crypto++如何在 Crypto++ 中使用 Shamir 秘密共享类
【发布时间】:2014-01-04 05:56:54
【问题描述】:

我尝试在 Crypto++ 中使用 SecretSharing 类,但无法使用。

这是我的代码:

using namespace CryptoPP;

void secretSharing(){
    AutoSeededRandomPool rng;
    SecretSharing shamir(rng, 4, 6); 
    byte test[] = {'a', 'b', 'c', 'd'};
    shamir.Put(test, 4); 
    //shamir.MessageEnd();

    //cout << shamir.TotalBytesRetrievable() <<endl;
}

编译运行后得到:

./main 
terminate called after throwing an instance of 'CryptoPP::BufferedTransformation::NoChannelSupport'
  what():  unknown: this object doesn't support multiple channels
[1]    3597 abort (core dumped)  ./main

SecretSharing::SecretSharing()的声明是:

SecretSharing (RandomNumberGenerator &amp;rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)

我应该给它一个BufferedTransformation*,但我应该使用哪个类?

Crypto++ 中是否有任何秘密共享示例代码?

【问题讨论】:

  • test.cpp 中有一个使用示例。看起来您应该传递一个指向 ChannelSwitch 类的指针。

标签: c++ cryptography crypto++


【解决方案1】:

以弗雷泽的回答为基础,代码如下。

void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed)
{
    RandomPool rng;
    rng.IncorporateEntropy((byte *)seed, strlen(seed));

    ChannelSwitch *channelSwitch;
    FileSource source(filename, false, new SecretSharing(rng,
        threshold, nShares, channelSwitch = new ChannelSwitch));

    vector_member_ptrs<FileSink> fileSinks(nShares);
    string channel;
    for (int i=0; i<nShares; i++)
    {
        char extension[5] = ".000";
        extension[1]='0'+byte(i/100);
        extension[2]='0'+byte((i/10)%10);
        extension[3]='0'+byte(i%10);
        fileSinks[i].reset(new FileSink((string(filename)+extension).c_str()));

        channel = WordToString<word32>(i);
        fileSinks[i]->Put((byte *)channel.data(), 4);
        channelSwitch->AddRoute(channel, *fileSinks[i], DEFAULT_CHANNEL);
    }

    source.PumpAll();
}

在上面的代码中,有一个名为ChannelSwitch 的变量是用new 创建的。 FileSource source 拥有它并将删除它。但他需要一个命名变量(而不是匿名或临时变量),因为他后来调用了channelSwitch-&gt;AddRoute

Wei 可以使用 Redirector 来完成它以允许堆栈分配(远离内存管理器)并确保 FileSource 过滤器不会删除它:

ChannelSwitch channelSwitch;
FileSource source(filename, false, new SecretSharing(rng,
    threshold, nShares, new Redirector(channelSwitch));
...
channelSwitch.AddRoute(channel, *fileSinks[i], DEFAULT_CHANNEL);

以及恢复代码:

void SecretRecoverFile(int threshold, const char *outFilename, char *const *inFilenames)
{
    SecretRecovery recovery(threshold, new FileSink(outFilename));

    vector_member_ptrs<FileSource> fileSources(threshold);
    SecByteBlock channel(4);
    int i;
    for (i=0; i<threshold; i++)
    {
        fileSources[i].reset(new FileSource(inFilenames[i], false));
        fileSources[i]->Pump(4);
        fileSources[i]->Get(channel, 4);
        fileSources[i]->Attach(new ChannelSwitch(recovery, string((char *)channel.begin(), 4)));
    }

    while (fileSources[0]->Pump(256))
        for (i=1; i<threshold; i++)
            fileSources[i]->Pump(256);

    for (i=0; i<threshold; i++)
        fileSources[i]->PumpAll();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多