gpt4 book ai didi

c++ - 在 Crypto++ 中从内存中加载 key

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:15 27 4
gpt4 key购买 nike

我正在尝试将公共(public) DLIES key 保存到内存中,然后再次读取它,但我不断收到异常 BER decode error .我用 ArraySink , ArraySource和一个 char[64]在两个 CryptoPP::DLIES<>::PublicKey 之间传输 key 的缓冲区.我什至验证公钥是好的。我错过了什么?

下面是无法正常工作的完整示例。如何修改它以便正确加载 key ?

#include <iostream>
#include <gfpcrypt.h>
#include <filters.h>
#include <osrng.h>

int main() {
try {
CryptoPP::DefaultAutoSeededRNG rng;

CryptoPP::DLIES<>::PrivateKey privateKey;
privateKey.GenerateRandomWithKeySize(rng, 10);

CryptoPP::DLIES<>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);

if (!publicKey.Validate(rng, 3)) {
std::cout << "Something wrong with public key." << std::endl;
return 1;
}

byte buf[64];
CryptoPP::ArraySink sink(buf, 64);
publicKey.Save(sink);

CryptoPP::ArraySource source((const char *)buf, sink.TotalPutLength());
CryptoPP::DLIES<>::PublicKey pk;
pk.Load(source);
} catch (CryptoPP::Exception &ex) {
std::cout << ex.what() << std::endl;
return 1;
}

return 0;
}

最佳答案

问题在于未设置 ArraySource 构造函数的 pumpAll=true 第 3 个参数。添加后,它起作用了。另一个可行的解决方案是改用 ByteQueue。为了完整起见,我在下面粘贴了两个工作示例。

ArraySource 版本:

#include <iostream>
#include <gfpcrypt.h>
#include <filters.h>
#include <osrng.h>

int main() {
try {
CryptoPP::DefaultAutoSeededRNG rng;

CryptoPP::DLIES<>::PrivateKey privateKey;
privateKey.GenerateRandomWithKeySize(rng, 10);

CryptoPP::DLIES<>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);

if (!publicKey.Validate(rng, 3)) {
std::cout << "Something wrong with sent public key." << std::endl;
return 1;
}

byte buf[64];
CryptoPP::ArraySink sink(buf, 64);
publicKey.Save(sink);

CryptoPP::ArraySource source(buf, sink.TotalPutLength(), true);
CryptoPP::DLIES<>::PublicKey pk;
pk.Load(source);

if (!pk.Validate(rng, 3)) {
std::cout << "Something wrong with received public key." << std::endl;
return 1;
}
} catch (CryptoPP::Exception &ex) {
std::cout << ex.what() << std::endl;
return 1;
}

return 0;
}

ByteQueue 版本(我最后发现更方便):

#include <iostream>
#include <gfpcrypt.h>
#include <filters.h>
#include <osrng.h>

int main() {
try {
CryptoPP::DefaultAutoSeededRNG rng;

CryptoPP::DLIES<>::PrivateKey privateKey;
privateKey.GenerateRandomWithKeySize(rng, 10);

CryptoPP::DLIES<>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);

if (!publicKey.Validate(rng, 3)) {
std::cout << "Something wrong with sent public key." << std::endl;
return 1;
}

CryptoPP::ByteQueue queue;
publicKey.Save(queue);
CryptoPP::lword size = queue.TotalBytesRetrievable();

byte buf[64];
queue.Get(buf, size);

CryptoPP::ByteQueue queue2;
queue2.Put(buf, size);
CryptoPP::DLIES<>::PublicKey pk;
pk.Load(queue2);

if (!pk.Validate(rng, 3)) {
std::cout << "Something wrong with received public key." << std::endl;
return 1;
}
} catch (CryptoPP::Exception &ex) {
std::cout << ex.what() << std::endl;
return 1;
}

return 0;
}

关于c++ - 在 Crypto++ 中从内存中加载 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26007768/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com