为证书对象分配private是失败的(CryptographicException:Access被拒绝。)

时间:2018-03-21 07:22:34

标签: c# cryptography signing cryptoapi

我正在使用CmsSigner类来签名数据。我正在使用HSM提供程序的证书,因此,我正在尝试设置CspParameter来教我的代码它应该使用什么提供程序。

            X509Certificate2 cert = GetCertificate("Signer cert");
        ContentInfo content = new ContentInfo(FileToSign);

        CspParameters csp = new CspParameters()
        {
            ProviderName = "nCipher RSA AES provider",
            ProviderType = 1,
            KeyNumber = (int)KeyNumber.Signature,
            Flags = CspProviderFlags.NoPrompt | CspProviderFlags.UseExistingKey | CspProviderFlags.UseMachineKeyStore,
            KeyContainerName = "myContainer"
        };

        RSACryptoServiceProvider priKey = new RSACryptoServiceProvider(csp);
        cert.PrivateKey = priKey;
        CmsSigner signer = new CmsSigner(cert);
        signer.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1");
        signer.IncludeOption = X509IncludeOption.EndCertOnly;
        signer.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.UtcNow));
        signedCms = new SignedCms(content, true);
        signedCms.ComputeSignature(signer);

RSACryptoServiceProvider实例化(RSACryptoServiceProvider priKey = new RSACryptoServiceProvider(csp);)现在已使用其他标志进行修复。但是下一行cert.PrivateKey = priKey失败并显示Access denied错误。

我的代码是否存在明显问题?

0 个答案:

没有答案