Import a private RSACryptoServiceProvider blob into CNGKey.Import

六眼飞鱼酱① 提交于 2019-12-11 14:57:31

问题


From a legacy program:

bye[] rsaPrivateKeyExport = RSACryptoProvider.ExportCspBlob(true);

These keys are stored in a file.

As part of a legacy refresh, I need to use CNG RSA keys.

So something like reading the old blob and then converting:

CngKey cngPrv = CngKey.Import(rsaPrvKeyExport, CngKeyBlobFormat.GenericPrivateBlob);

But I cannot get this to work?

How do I convert the old blob type to the new one? Do I only use parts of the old blob?

The key length is 2048.


回答1:


GenericPrivateBlob is a CNG-specific format, it doesn't mean "try any private thing".

CNG is capable of opening CAPI key blobs, using the formats identified at https://docs.microsoft.com/en-us/windows/desktop/api/Bcrypt/nf-bcrypt-bcryptexportkey.

private static readonly CngKeyBlobFormat s_legacyRsaPrivateBlobFormat =
    new CngKeyBlobFormat("CAPIPRIVATEBLOB");

...

byte[] exported;

using (RSACryptoServiceProvider a = new RSACryptoServiceProvider(2048))
{
    exported = a.ExportCspBlob(true);
}

RSA b;

using (CngKey key = CngKey.Import(exported, s_legacyRsaPrivateBlob))
{
    b = new RSACng(key);
}

// This using is broken out here just to show that the constructed object can safely outlive
// the CngKey object that it was created from.
using (b)
{
    ...
}


来源:https://stackoverflow.com/questions/51392846/import-a-private-rsacryptoserviceprovider-blob-into-cngkey-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!