Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Verify Signature, Loading PUBLIC KEY From CRT file?

Tags:

I reviewed many forums and examples, but none helped me. I need verify signature from any webservice. I have test.crt file with public key for verify.

static bool Verify(string text, string signature) {   X509Certificate2 cert = new X509Certificate2(       HttpContext.Current.Server.MapPath("test-server.cert"));   RSACryptoServiceProvider csp = (RSACryptoServiceProvider) cert.PublicKey.Key;    // Hash the data   SHA1Managed sha1 = new SHA1Managed();   UnicodeEncoding encoding = new UnicodeEncoding();   byte[] data = encoding.GetBytes(text);   byte[] sign = Convert.FromBase64String(signature);   byte[] hash = sha1.ComputeHash(data);    return csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), sign); } 

But result is always false :(

I have an OpenSSL example:

openssl base64 -d -in signature -out signature.bin openssl dgst -sha1 -verify test-server.pub -signature signature.bin from_gateway 
like image 863
drup Avatar asked Jun 27 '12 08:06

drup


People also ask

How do I verify a public key signature?

Encrypt the original message (1) with the public key to obtain a hash. Encrypt the decrypted message (3) to get a second hash and compare to (4) to verify that they are identical.

How do you verify a cryptographic signature?

Verifying a signature will tell you if the signed data has changed or not. When a digital signature is verified, the signature is decrypted using the public key to produce the original hash value. The data that was signed is hashed. If the two hash values match, then the signature has been verified.

How do I verify a signature in Openssl?

To verify a signature, the recipient first decrypts the signature using a public key that matches with the senders private key. This produces a digest. Then the recipient calculates a digest from the received data and verifies that it matches with the one in the signature. If the digest match, the signature is valid.

How do I know if my RSA signature is valid?

RSA Digital Signatures To sign a message m, just apply the RSA function with the private key to produce a signature s; to verify, apply the RSA function with the public key to the signature, and check that the result equals the expected message. That's the textbook description of RSA signatures.


1 Answers

I suspect the use UnicodeEncoding could be the reason for failures. As demonstrated below the bytes of ASCIIEncoding and UnicodeEncoding are not same for the reason ASCII is a 8bit encoding and in Windows Unicode encoding is 16bit wide. In your other question Can not get signature you've used the ASCIIEncoding. So assuming signature is computed on the ASCIIEncoding bytes of the text/string and verify using UnicodeEncoding obviously will not match.

string text = "Hello"; Console.WriteLine("ASCIIEncoding bytes length: {0}", new ASCIIEncoding().GetBytes(text).Length); Console.WriteLine("UnicodeEncoding bytes length: {0}", new UnicodeEncoding().GetBytes(text).Length); 

Outputs

ASCIIEncoding bytes length: 5 UnicodeEncoding bytes length: 10 
like image 137
Sivachandran Avatar answered Sep 20 '22 17:09

Sivachandran