c# - 使用加密 c# 在 PKCS8 中导出 CngKey

标签 c# encryption encryption-asymmetric cng

如何加密导出 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 允许导出 key ,但只能以纯文本形式导出。我想创建一个使用对称 key 加密的 PCKS8 blob。

谢谢

最佳答案

由于 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可以用作构建参数列表的示例。这不好玩。

关于c# - 使用加密 c# 在 PKCS8 中导出 CngKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44305299/

相关文章:

c# - MVC4 共享模板

c# - 反序列化后 JSON 字段值变为 null

python - 在python中保护密码

.net - 如何使用 .NET Framework 执行 RSAES-OAEP 加密和解密?

c# - 桌面应用程序和服务器之间的加密 - C# 到 PHP

ruby-on-rails - Ruby:使用私钥/公钥进行文件加密/解密

c# - 编译我的程序时出现此错误。运算符 "+ "不能应用于 "System.Random"和 "System.Random"的操作数

c# - 在 C# 中,我应该按值传递参数并返回相同的变量,还是按引用传递?

java - 将 php mcrypt_encrypt MCRYPT_3DES 转换为 Java

linux - 将 Azure 加密用于 Linux IaaS VM 及其连接的硬盘