3

如何通过加密将 CngKey 导出到 PKCS#8?

static void Main(string[] args)
    {
        CngKeyCreationParameters ckcParams = new CngKeyCreationParameters()
        {
            ExportPolicy = CngExportPolicies.AllowExport,
            KeyCreationOptions = CngKeyCreationOptions.None,
            KeyUsage = CngKeyUsages.AllUsages,                
        };
        ckcParams.Parameters.Add(new CngProperty("Length", BitConverter.GetBytes(2048), CngPropertyOptions.None));

        myCngKey = CngKey.Create(CngAlgorithm.Rsa, "theCngKey", ckcParams);

        byte[] privatePlainTextBlob = myCngKey.Export(CngKeyBlobFormat.Pkcs8PrivateBlob);
 }

将 ExportPolicy 设置为 AllowPlainTextExport 允许导出密钥,但只能以纯文本形式导出。我想创建一个用对称密钥加密的 PCKS8 blob。

谢谢

4

1 回答 1

1

由于CngKey.Export不接受密码,您必须手动 P/Invoke 到NCryptExportKey,提供 NCRYPTBUFFER_PKCS_SECRET 值(Unicode/UCS-2 编码密码,带有显式空终止符)。

http://source.dot.net/#System.Security.Cryptography.Cng/Common/System/Security/Cryptography/ECCng.ImportExport.cs,8b172741466df7a1可以用作构建参数列表的示例。这不好玩。

于 2017-06-01T16:04:30.257 回答