C# 如何使用C从PKCS#12(.p12)文件中获取私钥#

C# 如何使用C从PKCS#12(.p12)文件中获取私钥#,c#,encryption,x509certificate2,pkcs#12,C#,Encryption,X509certificate2,Pkcs#12,我试图使用PKCS#12证书对一些数据进行签名,但是我在从PKCS#12(.p12)文件获取私钥时遇到问题 问题是newCert.PrivateKey为空,但如果我以类似的方式使用.pfx certicate,它就会工作 public byte[] sign(string text) { string password = "1234"; X509Certificate2 cert = new X509Certificate2("c:\\certi

我试图使用PKCS#12证书对一些数据进行签名,但是我在从PKCS#12(.p12)文件获取私钥时遇到问题

问题是newCert.PrivateKey为空,但如果我以类似的方式使用.pfx certicate,它就会工作

    public byte[] sign(string text)
    {
        string password = "1234";
        X509Certificate2 cert = new X509Certificate2("c:\\certificate.pfx", password);
        RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
        SHA1Managed sha1 = new SHA1Managed();
        UnicodeEncoding encoding = new UnicodeEncoding();
        byte[] data = encoding.GetBytes(text);
        byte[] hash = sha1.ComputeHash(data);
        return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
    }

所以问题是如何从.p12文件中获取私钥?

看一看。它看起来非常相似。

在中,它说
.export()
不支持
Pfx
类型,只支持
Cert
SerializedCert
,和
Pkcs12
我发布了一个类似的问题,虽然对您来说不是同一件事,但问题也可能是权限问题。
我的建议是,首先,您必须确保(我想您已经这样做了)私钥是可导出的,并且您拥有对该文件的权限。
接下来,尝试将内容类型导出为
X509ContentType.Pkcs12
而不是
X509ContentType.Pfx


最后,如果可能,为什么不尝试将其导入certstore。我相信这样更安全。步骤在上面的链接中。

这是为使用Android而做的-因此下面的R.raw.key是我在Android raw文件夹中的文件

我打开key.p12作为输入流。然后我使用示例中所示的库将其转换为私钥

我的代码如下所示

Security.addProvider(new de.flexiprovider.core.FlexiCoreProvider());
    // Next, we have to read the private PKCS #12 file, since the the
    // private key used for signing is contained in this file:
    DERDecoder dec = new DERDecoder(getResources().openRawResource(
            R.raw.key));
    PFX pfx = new PFX();
    try {
        pfx.decode(dec);
        SafeBag safeBag = pfx.getAuthSafe().getSafeContents(0)
                .getSafeBag(0);
        PKCS8ShroudedKeyBag kBag = (PKCS8ShroudedKeyBag) safeBag
                .getBagValue();
        char[] password = "my password for the p12".toCharArray();
        privKey = kBag.getPrivateKey(password);
        new AsyncLoadStorage(this).execute();
    } catch (ASN1Exception e) {

嗨,蒂莫雷斯。谢谢你的链接,但是这个问题的解决方案显然对我不起作用。你有没有为.PFX和.P12使用过相同的代码?根据你的P12是从哪里来的,我相信应该是一样的。谢谢你的回答。权限不是问题,不可导出密钥也是如此。我尝试将类型更改为
X509ContentType.Pkcs12
,但结果相同。尽管将其导入certstore可能是一个解决方案,但我相信它是用Java实现的。当我写这个程序的时候,我需要它来用C做这个,但是谢谢分享。
Security.addProvider(new de.flexiprovider.core.FlexiCoreProvider());
    // Next, we have to read the private PKCS #12 file, since the the
    // private key used for signing is contained in this file:
    DERDecoder dec = new DERDecoder(getResources().openRawResource(
            R.raw.key));
    PFX pfx = new PFX();
    try {
        pfx.decode(dec);
        SafeBag safeBag = pfx.getAuthSafe().getSafeContents(0)
                .getSafeBag(0);
        PKCS8ShroudedKeyBag kBag = (PKCS8ShroudedKeyBag) safeBag
                .getBagValue();
        char[] password = "my password for the p12".toCharArray();
        privKey = kBag.getPrivateKey(password);
        new AsyncLoadStorage(this).execute();
    } catch (ASN1Exception e) {