【问题标题】:Import a private RSACryptoServiceProvider blob into CNGKey.Import将私有 RSACryptoServiceProvider blob 导入 CNGKey.Import
【发布时间】:2018-12-25 19:58:42
【问题描述】:

来自旧程序:

再见[] rsaPrivateKeyExport = RSACryptoProvider.ExportCspBlob(true);

这些密钥存储在一个文件中。

作为旧版刷新的一部分,我需要使用 CNG RSA 密钥。

就像读取旧的 blob 然后转换:

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

但我不能让它工作?

如何将旧的 blob 类型转换为新的类型?我只使用旧 blob 的一部分吗?

密钥长度为 2048。

【问题讨论】:

    标签: c# cryptography rsa cng


    【解决方案1】:

    GenericPrivateBlob 是 CNG 特有的格式,并不意味着“尝试任何私人的东西”。

    CNG 能够使用https://docs.microsoft.com/en-us/windows/desktop/api/Bcrypt/nf-bcrypt-bcryptexportkey 标识的格式打开 CAPI 密钥 blob。

    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)
    {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 2011-05-11
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 2014-07-07
      • 2013-07-31
      • 1970-01-01
      相关资源
      最近更新 更多