繁体   English   中英

如何在Crypto ++中使用Shamir Secret Sharing类

[英]How to use Shamir Secret Sharing Class in Crypto++

我试图在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 &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)

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

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

基于弗雷泽的答案,这是代码。

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();
}

在上面的代码中,使用new创建了一个名为ChannelSwitch变量。 FileSource source拥有它,并将其删除。 但是他需要一个命名变量(而不是匿名变量或临时变量),因为他后来调用channelSwitch->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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM