简体   繁体   English

单声道的PFX / PKCS12到SNK转换

[英]PFX/PKCS12 to SNK conversion for mono

This is follow up on Mono xbuild error CS1548 - key file has incorrect format 这是对Mono xbuild错误CS1548的跟进 - 密钥文件格式不正确

Hi, I have an application that is written in C# using VS2008. 嗨,我有一个使用VS2008用C#编写的应用程序。 At present we are porting this app to Mac using Mono. 目前我们正在使用Mono将此应用程序移植到Mac。

I have tried to extract the key from the pfx file. 我试图从pfx文件中提取密钥。 First I used 我先用过

`sn -pc key.pfx key.snk`

this gave me an error of 这给了我一个错误

'Failed to extract public key for key pair -- Keyset does not exist'.

I then used 然后我用了

`sn -p key.pfx key.snk`

this created the snk file that I wanted. 这创建了我想要的snk文件。 I then in mono selected the project Option > Assembly Signing When built the error 然后我在单声道选择了项目选项>程序集签名时构建错误

'key.snk is missing private key needed for signing'.

I think I understand that if I make a new snk key that I can have both private and public keys in it. 我想我明白,如果我创建一个新的snk密钥,我可以在其中包含私钥和公钥。 It just that because of Legacy issues we would really like to be able to use the original pfx key values. 只是因为遗留问题,我们真的希望能够使用原始的pfx密钥值。

Big thanks Poupou for coming up with the answer I have just added the code to the little program I made to get my snk. 非常感谢Poupou提出答案我刚刚将代码添加到我为获取我的snk而制作的小程序中。

using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace PfxSnk
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            X509Certificate2 cert = new X509Certificate2(@"KEY.pfx", "pfxPassword", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
            RSACryptoServiceProvider provider = (RSACryptoServiceProvider)cert.PrivateKey;

            byte[] array = provider.ExportCspBlob(!provider.PublicOnly);

            using (FileStream fs = new FileStream("FileName.snk", FileMode.Create, FileAccess.Write))
            {
                fs.Write(array, 0, array.Length);
            }
        }
    }
}

sn -p is used to extract a public key from a strongname. sn -p被用来提取一个公共密钥从强名称。

However you need the private key in order to sign an assembly - so this (built-in sn ) conversion is not helpful for your goal. 但是,您需要私钥才能对程序集进行签名 - 因此此(内置sn )转换对您的目标没有帮助。

Sadly a quick look at Microsoft sn options does not document any option to do what you're looking for. 遗憾的是,快速查看Microsoft sn选项并不会记录任何选项来执行您正在寻找的内容。

My suggestion is to write a small tool, re-using Mono sn and Mono.Security.dll source code, to read the PFX (pkcs#12) file and write it back as a SNK file. 我的建议是编写一个小工具,重新使用Mono snMono.Security.dll源代码,读取PFX(pkcs#12)文件并将其作为SNK文件写回。

请尝试使用sn -p key.pfx key.snk

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

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