Pluralsight.Crypto.CryptContext.Open()抛出System.IO.FileNotFoundException

时间:2016-08-31 06:35:32

标签: c# self-signed pfx

当我想使用以下方法创建自签名证书时:

public static byte[] CreateSelfSignCertificatePfx(string x500, DateTime startTime, DateTime endTime, SecureString password)
{
    X509Certificate2 cert;
    using (var ctx = new CryptContext())
    {
        ctx.Open();

        var certProps = new SelfSignedCertProperties();
        certProps.IsPrivateKeyExportable = true;
        certProps.KeyBitLength = 4096;
        certProps.Name = new X500DistinguishedName(x500);
        certProps.ValidFrom = startTime;
        certProps.ValidTo = endTime;

        cert = ctx.CreateSelfSignedCertificate(certProps);

        //X509Certificate2UI.DisplayCertificate(cert);
    }
    var result = cert.Export(X509ContentType.Pfx, password);
    return result;
}

它抛出异常......

System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at Pluralsight.Crypto.Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero()
   at Pluralsight.Crypto.CryptContext.Open()

一些提示?

0 个答案:

没有答案