我尝试在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 ++中是否有任何秘密共享示例代码?
答案 0 :(得分:4)
以弗雷泽的答案为基础,这是代码。
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->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();
}