如何使用仅给定密钥库的c#对哈希进行签名和加密
本文关键字:哈希进 加密 何使用 密钥 | 更新日期: 2023-09-27 18:24:07
我使用的密钥库是使用vc6.0和cryptoAPI创建的。密钥库包含所有交换/签名密钥。因此,我可以很好地使用公钥来使用RSA加密数据,但当需要对数据进行签名或解密时,我似乎找不到如何使用私钥进行解密。
我见过很多网站使用rsa=(RSACryptoServiceProvider)cert.PrivateKey;
但在我的密钥库中,当我查看证书时,所有私钥都不存在。
//这就是我设置商店的方式
public cryptTest(string storeName)
{
store = new X509Store(storeName);
this.storename = storename;
}
//这就是我从商店获得证书的方法
public X509Certificate2 getCertificate(string ID, certType ct)
{
if (store == null)
{
return null;
}
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 cert in store.Certificates)
{
if ((ct == certType.exchange && cert.Subject.Contains("Exchange")) ||
(ct == certType.signature && cert.Subject.Contains("Signature")))
{
if (cert.Subject.Contains(ID)) // if the ID match
{
// todo check date etc ! is cert still valid if not delete etc.
store.Close();
return cert;
}
}
}
store.Close();
return null;
}
但是在证书中,它们从来没有私钥,那么我怎么可能使用密钥库中的证书进行解密或签名呢?
万分感谢!
我认为MSDN上的EncryptTo/DecryptTo:Encryption in.NET with CryptoAPI Certificate Stores文章应该有你需要的答案。