我有一个小问题。我想验证证书的完整性。
所以我做了这段代码:
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 signatureLikeInteger = "";
bool verif = false;
// ------------- PART 1 -------------
signatureLikeInteger = certificatEnCours.Thumbprint;
data = Convert.FromBase64String(signatureLikeInteger);
// ------------- 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);
我的问题是我的变量“ false
”上已经有值“ verif
”。
这里没有实际的问题。没错,您无条件忽略了verif的初始值。更重要的是,您是否考虑过使用X509Certificate2进行验证?:
X509Certificate2 x2 = new X509Certificate2(certificatEnCours);
bool verif = x2.Verify();
我认为这比重新发明轮子更明智。
编辑:如果您要验证证书链,我相信您想使用X509Chain尤其是ChainStatus属性。