【问题标题】:Encrypt a RSA private key and write it to a file using Crypto++加密 RSA 私钥并使用 Crypto++ 将其写入文件
【发布时间】:2018-12-12 02:42:55
【问题描述】:

在使用 RSA 加密后,我正在尝试将 RSA 私钥导出到文件。 到目前为止,我尝试使用 2 种方法导出它:

int main()
{
    AutoSeededRandomPool rnd;
    const std::string publickey_str = 
    "-----BEGIN PUBLIC KEY-----\n"
    "rsa public key there\n"
    "-----END PUBLIC KEY-----";

    RSA::PublicKey master_publicKey;

    StringSource source(publickey_str, true);
    PEM_Load(source, master_publicKey);

    CryptoPP::RSAES_OAEP_SHA_Encryptor e(master_publicKey);

    InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize(rnd, 4096);

    RSA::PrivateKey privateKey(params);

    FileSink file("exported.bin")

    //Method 1

    FileSink file("exported.bin")
    ArraySource export_private_key(privateKey, sizeof(privateKey),true, AuthenticatedEncryptionFilter(e, new Redirector(file))); //can't find a way to convert privateKey to a valid type for ArraySource

    //Method 2
    privateKey.Save( new AuthenticatedEncryptionFilter(e, new Redirector(file))); //doesn't work, as Save() expects BufferedTransformation

有没有什么方法可以使用这两种方法中的一种来实现我的目标,或者我必须将我的私钥转换为另一种格式(BER 或 PEM 编码),然后再对其应用 RSA?

提前致谢

P.S:我的问题是特定于 Windows 的,如果它有任何影响的话。

【问题讨论】:

  • 下载PEM Pack并用它来密码保护磁盘上的私钥。另外(如果我正确地解析了事情)......如果你用它的公钥加密私钥,那么你可能无法使用密钥来解密自己,因为私钥在解密。

标签: c++ windows encryption crypto++


【解决方案1】:

没有。

由于需要安全填充 - 在本例中为 OAEP,RSA 只能安全地加密(比密钥大小)更小的数据量。 RSA 与任何密码一样,对 字节 进行加密,因此将密钥更改为文本肯定无济于事。它还会进一步扩大需要加密的密钥大小。

您可以做的是使用混合加密:首先使用 AES 在安全操作模式下使用新的随机密钥进行加密,然后使用 RSA 加密 AES 密钥。


您可能需要考虑一下您的安全方案; RSA 加密仅在 RSA 解密密钥比您正在加密的 RSA 密钥保持更安全时才有用。否则,您只是将问题转移到 包装和解包私钥的 RSA 密钥对。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2020-12-07
    • 2011-03-20
    • 2013-05-07
    • 2018-01-09
    • 1970-01-01
    相关资源
    最近更新 更多