如何在Crypto ++中使用Shamir Secret Sharing类 [英] How to use Shamir Secret Sharing Class in Crypto++
问题描述
我试图在Crypto ++中使用 SecretSharing 类,但是我无法使用
I tried to use the SecretSharing Class in Crypto++, but I couldn't make it work.
这是我的代码:
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;
}
编译并运行后,我将得到:
After compile and run, I will get:
./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阈值,int nShares,BufferedTransformation * attachment = NULL,bool addPadding = true)
我应该给它一个 BufferedTransformation *
,但是究竟应该使用哪个类?
Should I give it a BufferedTransformation*
, but exactly which class should I use?
Crypto ++中是否有任何秘密共享示例代码?
Are there any Secret Sharing example code in 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();
}
在上面的代码中,有一个名为 ChannelSwitch
用 new
创建的变量。 文件源
源
拥有它,并将其删除。但是他需要一个命名变量(而不是匿名变量或临时变量),因为后来他调用 channelSwitch-> AddRoute
In the code above, there's a named ChannelSwitch
variable created with new
. The FileSource
source
owns it and will delete it. But he needed a named variable (and not an anonymous or temporary) because he later calls channelSwitch->AddRoute
Wei可以使用 Redirector
来做到这一点,以允许进行堆栈分配(不使用内存管理器),并确保 FileSource
过滤器不会将其删除:
Wei could have done it with a Redirector
to allow a stack allocation (stay out of the memory manager) and to ensure the FileSource
filter does not delete it:
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();
}
这篇关于如何在Crypto ++中使用Shamir Secret Sharing类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!