views:

509

answers:

3

Hello, sorry for my english.

I have a little problem. I want to verify the integrity of my certificat.

I make this code:

using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates; 

SHA1Managed sha1 = new SHA1Managed();
RSACryptoServiceProvider csp = null;
AsymmetricAlgorithm rsaAlgo = certificatEnCours.PublicKey.Key;
byte[] data = null;
byte[] hash = null;

string keyPublic = "";
string signatureNumérique = "";

bool verif = false;

// ------------- PART 1 -------------

signatureNumérique = certificatEnCours.Thumbprint;

data = Convert.FromBase64String(signatureNumérique);

// ------------- PART 2 -------------

hash = sha1.ComputeHash(certificatEnCours.RawData);

keyPublic = rsaAlgo.ToXmlString(false);

csp = new RSACryptoServiceProvider();

csp.FromXmlString(keyPublic);

// ------------------------------

verif = csp.VerifyData(hash, CryptoConfig.MapNameToOID("SHA1"), data);

but i have already value "false" for the var "verif"

A: 

There is no actual question here. You are right that you are unconditionally ignoring the initial value of verif. More importantly, have you considered using X509Certificate2 to do verification?:

X509Certificate2 x2 = new X509Certificate2(certificatEnCours);
bool verif = x2.Verify();

I think this is wiser than re-inventing the wheel.

EDIT: If you are verifying a chain of certificates I believe you want to use X509Chain and in particular the ChainStatus property.

Matthew Flaschen
A: 

Just one question. Do you absolutely need to check the certificate manually? Can't you use the integrated X509Certificate2.Verify method?

AlexDrenea
A: 

Thanks for your quick response.

You are faster that the french's forum. ^^

Sorry, that i have forgot put a question.

I want to verify that the certificate "child" was send by the certificate "father".

Someone has said me about this, it's for this reason that i have make this code.

But when i use the fonction cert2.Verify();, i have the answer false.

Do you have some idea??