RSA decoding with public key or veryfing
我有以下问题:
我的应用程序收到一个带有公钥的证书、一个RSA编码的(带有私钥)sha256签名和一堆数据。
我必须检查数据的sha256哈希是否与RSA编码的哈希相同。看起来像:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | bool checkSHA(X509Certificate2 certificate, byte[] RsaSHASign, byte[] dataToCheck) { byte[] publicKey = certificate.GetPublicKey(); RSACryptoServiceProvider crsa = new RSACryptoServiceProvider(); //Get an instance of RSAParameters from ExportParameters function. RSAParameters RSAKeyInfo = crsa.ExportParameters(true); //Set RSAKeyInfo to the public key values. RSAKeyInfo.Modulus = publicKey; //Import key parameters into RSA. crsa.ImportParameters(RSAKeyInfo); SHA256 sha = SHA256.Create(); byte[] hashValue1 = sha.ComputeHash(dataToCheck); string OID = CryptoConfig.MapNameToOID("SHA256"); //here I must check if the counted hash is the same as the one passed as RSA encoded return crsa.VerifyData(RsaSHASign, OID, hashValue1); } |
我真正需要的是RSA编码之前的散列值,但我想没有私钥是不可能的。
不幸的是,即使我知道哈希值相同(在RSA之前),VerifyData也总是返回false。我真的被卡住了:(
我希望这个解决方案不难,但不知何故还是搞不清楚。我尝试使用bouncycastle和openssl包装器,但也没有成功。
我用这种方式:
1 2 3 4 5 6 | X509Certificate2 Pe = new X509Certificate2(Pe_file); RSACryptoServiceProvider rsa_e = (RSACryptoServiceProvider)Pe.PublicKey.Key; if (rsa_e.VerifyHash(hash,"SHA256", data)) { Debug.Print("OK"); } |