.net - 如何在 .Net 中使用 PKCS #1 v1.5 编码数据

标签 .net compact-framework cryptography rsa smartcard

我正在尝试在符合 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/

相关文章:

c# - 使用 ref 传递引用类型是否节省内存?

c# - 如何在 C# 中执行 Python 的 zip?

c# - 在 .Net Compact Framework 中将文件内容读取为字符串

c# - 在 .net Compact Framework 中使用 WEB API 身份验证

c# - 使用 FromUri 绑定(bind)没有参数名称前缀的对象

python - 在 pythonnet 中使用 OWdotNET.dll(.NET 的 python)

c# - 紧凑框架中的透明度

cryptography - 给定密文和 key 时如何确定所使用的加密方案

c# - .NET 密码学中 SignData() 和 CreateSignature() 的区别

security - MD5 哈希加盐 - 在哪里存储盐