如何使用Compact Framework在C#中验证X.509证书
我正在尝试使用C#和.NetCF验证X.509证书.我有CA证书,如果我理解正确,我需要使用此CA证书中的公钥来解密不受信任证书的签名.这应该给我不受信任证书的计算哈希值.然后我应该自己计算证书的哈希值并确保两个值匹配.
我已经玩了几天了,我没有走得太远.我一直在使用X509Certificate和RSACryptoServiceProvider类.首先,我尝试从X509Certificate类中获取公钥和签名.我能够获得公钥而不是签名.接下来,我尝试解析构成证书的二进制数据,这允许我获取签名(以及我想要的任何其他数据),但我无法使用RSACryptoServiceProvider解密签名.我试过这样的事情,但在我试图解密的时候不断得到说“Bad Key”的异常: RSAParameters rsaParams = new RSAParameters(); rsaParams.Exponent = exp; rsaParams.Modulus = mod; RSACryptoServiceProvider rsaServ = new RSACryptoServiceProvider(); rsaServ.ImportParameters(rsaParams); byte[] decryptedSig = rsaServ.Decrypt(encryptedSig,false); 任何建议将不胜感激. 编辑: X509Certificate2 cert = new X509Certificate2(certBytes); X509Certificate2 certCA1 = new X509Certificate2(@"C:certscertCA1.cer"); byte[] encryptedSig = new byte[256]; Array.Copy(certBytes,certBytes.Length - 256,encryptedSig,256); RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certA1.PublicKey.Key; bool good = rsa.VerifyData(cert.RawData,"1.3.14.3.2.26",encryptedSig); 正如我所说,我能够手动解码和解释证书的二进制数据,所以我很确定cert.RawData是证书的签名数据,最后256个字节是加密签名.字符串是我从证书中获得的哈希算法的OID,但我并不是100%确定它是正确的. VerifyData返回false,但我不知道为什么. 思考? 解决方法
这是我的代码.
RSACryptoServiceProvider rsa = signingCertificate_GetPublicKey(); return rsa.VerifyData( SignedValue(),CryptoConfig.MapNameToOID( "SHA1" ),Signature() ); RSACryptoServiceProvider signingCertificate_GetPublicKey() { RSACryptoServiceProvider publicKey = new RSACryptoServiceProvider(); RSAParameters publicKeyParams = new RSAParameters(); publicKeyParams.Modulus = GetPublicKeyModulus(); publicKeyParams.Exponent = GetPublicKeyExponent(); publicKey.ImportParameters( publicKeyParams ); return publicKey; } byte[] GetPublicKeyExponent() { // The value of the second TLV in your Public Key } byte[] GetPublicKeyModulus() { // The value of the first TLV in your Public Key } byte[] SignedValue() { // The first TLV in your Ceritificate } byte[] Signature() { // The value of the third TLV in your Certificate } 我希望这有助于解决这个问题的任何人. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |