使用 X509Certificate2 提示输入密码以使用私钥执行数字签名
Performing a digital signature using X509Certificate2 prompt for a password to use private key
如何确保 C# 代码使用 password
来使用 certificate's private key
?证书安装在 CurrentUser/Personal
商店中。我试过了,但我的代码仍然使用私钥而不提示输入密码。
byte[] fileData = File.ReadAllBytes(path);
ContentInfo contentInfo = new ContentInfo(fileData);
SignedCms signedCms =
new SignedCms(SubjectIdentifierType.SubjectKeyIdentifier, contentInfo, true);
CmsSigner signer =
new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, MyLoadedCert);
signer.IncludeOption = X509IncludeOption.WholeChain;
signedCms.ComputeSignature(signer); // Works fine without prompting password
byte[] encoded = signedCms.Encode();
File.WriteAllBytes(signatureFilePath, encoded);
当我 运行 你在启用强密钥保护的情况下编写代码时,我遇到了一个异常 Provider could not perform the action since the context was acquired as silent.
。这是因为我没有在 ComputeSignature
方法中指定 silent
参数,默认情况下它设置为 true(看起来是这样)。
当你更改这行代码时
signedCms.ComputeSignature(signer);
至
signedCms.ComputeSignature(signer, false);
然后它会在必要时提示用户交互,即当您在证书导入向导中指定强密钥保护时。可以找到文档 here.
强私钥保护的默认值 action/level 为中等,这意味着用户必须批准(单击“确定”)私钥的使用。您可以将其更改为高保护并根据需要设置密码(请参见下面的屏幕)。
有什么解决办法吗我需要记住密码,但是我用的程序不会一直运行当我需要它的时候我调用exe,
为了解决这个问题,我必须创建一个 运行 在后台运行的服务,这样它才能工作并记住,但我需要它由 exe
PKCS#11 HSM Remember Password
如何确保 C# 代码使用 password
来使用 certificate's private key
?证书安装在 CurrentUser/Personal
商店中。我试过了,但我的代码仍然使用私钥而不提示输入密码。
byte[] fileData = File.ReadAllBytes(path);
ContentInfo contentInfo = new ContentInfo(fileData);
SignedCms signedCms =
new SignedCms(SubjectIdentifierType.SubjectKeyIdentifier, contentInfo, true);
CmsSigner signer =
new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, MyLoadedCert);
signer.IncludeOption = X509IncludeOption.WholeChain;
signedCms.ComputeSignature(signer); // Works fine without prompting password
byte[] encoded = signedCms.Encode();
File.WriteAllBytes(signatureFilePath, encoded);
当我 运行 你在启用强密钥保护的情况下编写代码时,我遇到了一个异常 Provider could not perform the action since the context was acquired as silent.
。这是因为我没有在 ComputeSignature
方法中指定 silent
参数,默认情况下它设置为 true(看起来是这样)。
当你更改这行代码时
signedCms.ComputeSignature(signer);
至
signedCms.ComputeSignature(signer, false);
然后它会在必要时提示用户交互,即当您在证书导入向导中指定强密钥保护时。可以找到文档 here.
强私钥保护的默认值 action/level 为中等,这意味着用户必须批准(单击“确定”)私钥的使用。您可以将其更改为高保护并根据需要设置密码(请参见下面的屏幕)。
有什么解决办法吗我需要记住密码,但是我用的程序不会一直运行当我需要它的时候我调用exe,
为了解决这个问题,我必须创建一个 运行 在后台运行的服务,这样它才能工作并记住,但我需要它由 exe
PKCS#11 HSM Remember Password