我尝试用Botan加密RSA私钥,但是,似乎无法为Botan随机生成器设置种子或保存它。
因此,当我尝试解密密钥(通过重新运行程序)时,结果会有所不同。
有一些代码:
std::unique_ptr<Botan::RandomNumberGenerator> rng(new Botan::System_RNG());
Botan::RSA_PrivateKey key(*rng.get(), 2048);
// We need to don't use a totally random rng in order to be able to decrypt the key.
QFile priv_key(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/priv_key.rsa");
// If the private key does not exist, create it.
if (!priv_key.exists()) {
Botan::RSA_PublicKey keyPub(key);
auto encoded_key = Botan::PKCS8::BER_encode(key, *rng.get(), QSysInfo::machineUniqueId().toStdString());
qDebug() << encoded_key.size();
QByteArray arr(reinterpret_cast<char*>(encoded_key.data()), encoded_key.size());
priv_key.open(QFile::WriteOnly | QFile::Truncate);
priv_key.write(arr);
priv_key.flush();
priv_key.close();
}
else
qDebug() << "already has a private key";
qDebug() << "filename: " << priv_key.fileName();
try {
auto decoded_key = Botan::PKCS8::load_key(priv_key.fileName().toStdString(),
*rng.get(),
QSysInfo::machineUniqueId().toStdString());
qDebug() << (decoded_key->private_key_bits() == key.private_key_bits());
} catch (std::exception& e) {
qDebug() << e.what();
}