如何从PKCS#12(.p12)文件获取私钥使用C# [英] How to get private key from PKCS#12 (.p12) file using C#

查看:328
本文介绍了如何从PKCS#12(.p12)文件获取私钥使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用PKCS#12证书签署一些数据,但是我从PKCS#12(.p12)文件获取私钥有问题。

  public byte [] sign(string text)
{
string password =1111;
X509Certificate2 cert = new X509Certificate2(c:\\certificate.p12,密码);
byte [] certData = cert.Export(X509ContentType.Pfx,password);

X509Certificate2 newCert = new X509Certificate2(certData,password);
RSACryptoServiceProvider crypt =(RSACryptoServiceProvider)newCert.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));
}

问题是newCert.PrivateKey为null,但如果我使用.pfx

  public byte [] sign(string text)
{
string password =1234;
X509Certificate2 cert = new X509Certificate2(c:\\\certificate.pfx,密码);
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文件获取私钥? / p>

最好的问候



Alan

解决方案

我有一个类似的问题,我发布了这里,虽然对你来说不是一回事,但问题可能也是权限。_
我的建议是,首先你必须确定假设您已经做过)私钥是可导出的,并且您具有该文件的权限。

接下来,尝试将内容类型导出为 X509ContentType.Pkcs12 而不是 X509ContentType.Pfx

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


Im trying to sign some data using PKCS#12 certificate ,however i have problem with obtaining private key from PKCS#12 (.p12) file.

    public byte[] sign(string text)
    {
        string password = "1111";
        X509Certificate2 cert = new X509Certificate2("c:\\certificate.p12",password);
        byte[] certData = cert.Export(X509ContentType.Pfx,password);

        X509Certificate2 newCert = new X509Certificate2(certData, password);
        RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)newCert.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"));
    }

The problem is that newCert.PrivateKey is null but if i am using .pfx certicitae in similar way it works.

    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"));
    }

So the question is how to get that private key from .p12 file ?

Best Regards

Alan

解决方案

I had a similar problem which I posted here, although it is not the same thing for you, the problem may be also permissions.
My suggestions are, first, you have to make sure (which I suppose you already did) that the private key is exportable and you have permissions to the file.
Next, try exporting the content type as X509ContentType.Pkcs12 instead of X509ContentType.Pfx
Finally, if it is possible, why don't you try importing it to the certstore. I belive that's more secure. The steps are in the link above.

这篇关于如何从PKCS#12(.p12)文件获取私钥使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆