简体   繁体   English

如何使用C#创建/导出到.PVK文件?

[英]How do I create/export to a .PVK file using C#?

I have an RSA private key loaded in an RSACryptoServiceProvider object. 我在RSACryptoServiceProvider对象中加载了RSA私钥。 I need to export this to a .pvk file for use with SQL Server. 我需要将其导出到.pvk文件以用于SQL Server。

Is there a way to do this from within c#? 有没有办法从C#中做到这一点? I see it's possible using openssl... https://stackoverflow.com/a/51207929/5924962 我认为有可能使用openssl ... https://stackoverflow.com/a/51207929/5924962

This is the only documentation I could find on the pvk file format: https://web.archive.org/web/20141117214716/http://www.drh-consultancy.demon.co.uk/pvk.html 这是我可以找到的有关pvk文件格式的唯一文档: https ://web.archive.org/web/20141117214716/http://www.drh-consultancy.demon.co.uk/pvk.html

Mono project has an open source implementation of the PVK format, Mono项目具有PVK格式的开源实现,

https://github.com/mono/mono/blob/master/mcs/class/Mono.Security/Mono.Security.Authenticode/PrivateKey.cs https://github.com/mono/mono/blob/master/mcs/class/Mono.Security/Mono.Security.Authenticode/PrivateKey.cs

Note the Mono.Security NuGet package is not official. 请注意, Mono.Security NuGet软件包不是官方的。

If you look at the output of RSACryptoServiceProvider.ExportCspBlob you'll see that it starts with the same sequence of bytes as offset 24 of an unencrypted PVK file. 如果查看RSACryptoServiceProvider.ExportCspBlob的输出,您会发现它以与未加密的PVK文件的偏移量24相同的字节序列开头。 So a PVK file is just the CSP blob with a header: 因此,PVK文件只是带有标头的CSP Blob:

using (var output = new BinaryWriter(File.Create("rsa.pvk")))
{
    output.Write(0xB0B5F11Eu);  // PVK magic number
    output.Write(0u);
    output.Write(1u);           // KEYTYPE_KEYX for RSA
    output.Write(0u);           // not encrypted
    output.Write(0u);           // encryption salt length

    var cspBlob = rsaCSP.ExportCspBlob(true);
    output.Write((uint)cspBlob.Length);
    output.Write(cspBlob);
}

I've verified that OpenSSL will read this 我已验证OpenSSL会阅读此内容

openssl rsa -in rsa.pvk -inform PVK

and will generate the same PEM private key export as the code in this answer that generates the PEM private key directly from the RSACryptoServiceProvider. 并将生成与该答案的代码相同的PEM私钥导出,该答案直接从RSACryptoServiceProvider生成PEM私钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM