0
我試圖使用自簽名證書對字符串進行簽名,然後驗證字符串是否正確簽名。無法使用X509Certificate2驗證簽名
這是我如何創建我的自簽名證書:
makecert -r -n "CN=AuthCert2" -ss my -a sha1 -pe
這之後,我的證書導出到一個PFX文件,並嘗試運行下面的代碼:
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Net;
using System.Net.Security;
[TestClass]
public class TestSign
{
[TestMethod]
public void TestSignAndVerify()
{
string toSignString = "This is my string to sign";
byte[] data = UnicodeEncoding.UTF8.GetBytes(toSignString);
SHA1Managed sha1 = new SHA1Managed();
byte[] hash = sha1.ComputeHash(data);
X509Certificate2 signCert = new X509Certificate2("authcert2.pfx", "authpass");
var csp = (RSACryptoServiceProvider)signCert.PrivateKey;
byte[] signedData = csp.SignData(hash, CryptoConfig.MapNameToOID("SHA1"));
RSACryptoServiceProvider csp2 = (RSACryptoServiceProvider)signCert.PublicKey.Key;
bool result = csp2.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signedData);
}
}
但我總是得到的結果是錯誤的。我相信我錯過了一些非常基本的東西。任何想法是怎麼回事,或者我可以如何調試這個問題?
謝謝,