我正在尝试在符合 PIV 的智能卡上执行主动卡身份验证。请参阅 this document 的附录 A.2了解更多详情。
我有来自关联 X.509 证书的公钥。我需要向卡发送一些随机数据,它将使用其私钥对其进行签名,然后我需要使用公钥验证签名。
我放置的文档中的示例指出,他们发送的要签名的数据是“根据 PKCS #1 v1.5 签名填充方案进行编码的”。我如何像这样编码我的随机数据?我认为填充数据是 RSA 签名过程的一部分。
// Generate random data
byte[] randomData = new byte[128];
Random random = new Random();
random.NextBytes(randomData);
// Hash the random data
SHA1Managed sha1 = new SHA1Managed();
byte[] hash = sha1.ComputeHash(randomData);
// Send the hash to the Smart Card and get back signed data
byte[] signature = SendToSmartCardForSignature(hash);
// Verify the data and the signature match using the public key
RSACryptoServiceProvider rsa = smartCardCertificate.PublicKey;
bool verified = rsa.VerifyData(randomData, CryptoConfig.MapNameToOID("SHA1"), signature);
// verified is false...why?
最佳答案
使用 .NET 加密功能执行此操作的“正确”方法是实现您自己的 System.Security.Cryptography.RSA 实现。 DecryptValue
方法应调用智能卡“原始”RSA 私钥加密方法。另外,请记住覆盖 KeySize
属性。
然后您可以使用 RSAPKCS1SignatureFormatter填充数据并调用智能卡。
关于.net - 如何在 .Net 中使用 PKCS #1 v1.5 编码数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4692829/