c++ - 如何在 Crypto++ 中使用 Shamir secret 共享类

标签 c++ cryptography crypto++

我尝试使用 SecretSharing在 Crypto++ 中上课,但我无法让它工作。

这是我的代码:

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++ 中是否有 secret 共享示例代码?

最佳答案

基于 Fraser 的回答,这是代码。

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

关于c++ - 如何在 Crypto++ 中使用 Shamir secret 共享类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917558/

相关文章:

c++ - 使用 Crypto++ 将文件的 CRC 计算为数值

c++ - ElGamal 加密示例?

ruby - 如何用另一种语言解密由 Ruby 的 `symmetric-encryption` gem 加密的数据?

c++ - 如何在 Crypto++ 中加载 Base64 RSA key

c++ - 更改 std::endl 以输出 CR+LF 而不是 LF

c++ - 创建单独的实现和头文件

c# - 我将如何在 PHP 中生成相同的 token ? (来自.NET)

winapi - 验证可执行文件的 Authenticode 签名时内存泄漏?

c++ - 调用 fork 时缓冲的 C++ 流的竞争条件 -- 强制刷新?

c++ - CMake 在 build 和 src 目录之间拆分生成的文件