gpt4 book ai didi

c++ - Botan 加载现有的 RSA 私钥

转载 作者:行者123 更新时间:2023-11-28 04:43:13 46 4
gpt4 key购买 nike

我正在使用 Botan C++ 库来签署和验证一些 license.ini 文件。我已将 Botan PK_Signer 设置为使用 RSA 算法来加密使用 PKCS v1.5 创建的散列。这是我的代码:

uint8_t private_key[] = "private key I already have generated."

// Read file content that needs to be signed.
std::string licensePath = argv[1];
std::string fileContents = readFileContent(licensePath);

// Prepare Botan RSA signer. PKCS1 v1.5 is used.
Botan::AutoSeeded_RNG rng;
Botan::secure_vector<uint8_t> secure_key_vector(private_key, private_key + sizeof(private_key) / sizeof(private_key[0]));

/////// NEXT LINE THROWS EXCEPTION!
Botan::RSA_PrivateKey rsa_priv_key(Botan::AlgorithmIdentifier(), secure_key_vector);
//////////////////////////////////////

Botan::PK_Signer signer(rsa_priv_key, rng, "EMSA-PKCS1-v1_5");

// Create signature.
signer.update(cleanStr(fileContents));
std::vector<uint8_t> signature = signer.signature(rng);
std::string hexSignature = Botan::hex_encode(signature);

生成RSA_PrivateKey对象的标线抛出异常:

Exception thrown at 0x00007FF85D2BC3C7 (vcruntime140.dll) in license_signer.exe: 0xC0000005: Access violation reading location 0x0000021A8FA07000.

我以前从未使用过 Botan 库。如果有人知道为什么会发生这种情况或知道如何实现,请提供帮助。谢谢。

最佳答案

Using Botan 2.12.1

#include <string>
#include <botan/auto_rng.h>
#include <botan/auto_rng.h>
#include <botan/base64.h>
#include <botan/pkcs8.h>
#include <botan/rsa.h>
#include <botan/x509_key.h>
#include <botan/data_src.h>
#include <botan/pubkey.h>

//Assuming the private and public keys are in Base64 encoded BER.

Botan::AutoSeeded_RNG rng;
Botan::RSA_PrivateKey keyPair(rng, static_cast<size_t>(4096));
std::string privateKey = Botan::base64_encode(Botan::PKCS8::BER_encode(keyPair));
std::string publicKey = Botan::base64_encode(Botan::X509::BER_encode(keyPair));

//Loading the public key

Botan::SecureVector<uint8_t> keyBytes(Botan::base64_decode(publicKey));
std::unique_ptr<Botan::Public_Key> pbk(Botan::X509::load_key(std::vector(keyBytes.begin(), keyBytes.end())));

//Loading the private key

Botan::SecureVector<uint8_t> keyBytes(Botan::base64_decode(privateKey));
Botan::DataSource_Memory source(keyBytes);
std::unique_ptr<Botan::Private_Key> pvk(Botan::PKCS8::load_key(source));

关于c++ - Botan 加载现有的 RSA 私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49795411/

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