如何在Crypto ++中加载Base64 RSA密钥 [英] How to load Base64 RSA keys in Crypto++

查看:101
本文介绍了如何在Crypto ++中加载Base64 RSA密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为正在制作的程序编写辅助函数,并且需要将键作为字符串返回.找到了一种将RSA密钥从PrivateKey/PublicKey转换为Base64字符串的方法.

I'm trying to write helper functions for a program I'm making and I need to return the keys as strings. Found a way to convert the RSA keys from PrivateKey/PublicKey to Base64 string.

int main()
{
    //Generate params
    AutoSeededRandomPool rng;
    InvertibleRSAFunction params;
    params.Initialize(rng, 4096);

    //Generate Keys
    RSA::PrivateKey privKey(params);
    RSA::PublicKey pubKey(params);

    //Encode keys to Base64
    string encodedPriv, encodedPub;

    Base64Encoder privKeySink(new StringSink(encodedPriv));
    privKey.DEREncode(privKeySink);

    Base64Encoder pubKeySink(new StringSink(encodedPub));
    privKey.DEREncode(pubKeySink);

    RSA::PrivateKey pvKeyDecoded;
    RSA::PublicKey pbKeyDecoded;

    //how to decode...

    system("pause");
    return 0;
}

现在,如何重新加载编码后的密钥?我找不到任何相关信息.

Now, how do I load the encoded keys back? I wasn't able to find any information on that.

推荐答案

RSA::PrivateKey pvKeyDecoded;
RSA::PublicKey pbKeyDecoded;

//how to decode...

您可以执行以下操作:

StringSource ss(encodedPriv, true, new Base64Decoder);
pvKeyDecoded.BERDecode(ss);


您还应该解决此问题:


You should also fix this:

Base64Encoder pubKeySink(new StringSink(encodedPub));
privKey.DEREncode(pubKeySink);  // pubKey.DEREncode

并且一旦写入密钥,您应该调用 MessageEnd():

And you should call MessageEnd() once the key is written:

Base64Encoder privKeySink(new StringSink(encodedPriv));
privKey.DEREncode(privKeySink);
privKeySink.MessageEnd();

Base64Encoder pubKeySink(new StringSink(encodedPub));
pubKey.DEREncode(pubKeySink);
pubKeySink.MessageEnd();

您还可能会从Crypto ++ Wiki中找到键和格式.

You might also find Keys and Formats helpful from the Crypto++ wiki.

这篇关于如何在Crypto ++中加载Base64 RSA密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆