C# 使用智能卡证书签名数据不';不要在x64平台上工作
我有一个在windows上运行的应用程序,我想用任何智能卡证书对数据进行签名 对不起,我的英语不是我的母语。 以下是我使用的代码:C# 使用智能卡证书签名数据不';不要在x64平台上工作,c#,smartcard,x509certificate2,C#,Smartcard,X509certificate2,我有一个在windows上运行的应用程序,我想用任何智能卡证书对数据进行签名 对不起,我的英语不是我的母语。 以下是我使用的代码: string heslo = "289-Black Joe"; var myStore = new X509Store(); myStore.Open(OpenFlags.ReadOnly); foreach(X509Certificate2 cert in myStore.Certificates) { if(!cert.HasPrivateKey) c
string heslo = "289-Black Joe";
var myStore = new X509Store();
myStore.Open(OpenFlags.ReadOnly);
foreach(X509Certificate2 cert in myStore.Certificates)
{
if(!cert.HasPrivateKey) continue; // not smartcard for sure
try
{
var rsa = cert.PrivateKey as RSACryptoServiceProvider; // exception here
if(rsa == null) continue; // not smart card cert again
if(rsa.CspKeyContainerInfo.HardwareDevice) // sure - smartcard
{
signedHash = rsa.SignHash(HashData(heslo), CryptoConfig.MapNameToOID("SHA1"));
var pom = Encoding.Default.GetString(signedHash);
myStore.Close();
return;
}
} catch(Exception ex)
{
Console.WriteLine(ex);
}
}
myStore.Close();
当在项目设置中的Build->Platform target->preference 32-bit中选中时,代码工作。当迭代证书到达卡证书时,它向卡请求PIN码,然后对我的数据进行签名。但当“首选32位”被取消选中时(这正是我需要的),请求PIN码的窗口将被跳过,因为无法获取证书的私钥
我不太清楚为什么会发生这种情况,我也不太熟悉证书的使用。请参阅:(SHA-256使用32位字,而SHA-512使用64位字)。您需要使用Sha256。请看:谢谢您的快速回复。示例代码永远不会到达64位选项处的rsa.SignHash方法。它不会要求智能卡输入PIN码,但在32位选项中,它会询问。@JiříVrbas可能是智能卡驱动程序问题,即您只有32位驱动程序。我会尝试在64位操作系统上存储我的
certutil-user-store
,以证明这一点。Certutil应该是64位应用程序。它将尝试使用密钥并对其进行签名/加密检查。@pepo当我运行certutil脚本时,它也不会要求卡的PIN码,所以我可能会错过驱动程序。谢谢。@jdweng哈希函数与此无关。当然,您可以在64位平台上编程SHA-256,在32位机器上编程SHA-512,而不会出现任何问题。