3
以下是我的場景 - 我從條形碼讀取數據並將其轉換爲純文本。該文本是條形碼數據+數字簽名的組合。數字簽名被附加到末尾,這使我能夠分離出實際數據和數字簽名數據。使用sha256 散列數字簽名數據 - 用戶將公鑰發送給Windows證書文件(.cer extension
)。使用BouncyCastle的數字簽名驗證 - 使用SHA 256的ECDSA,C#
必需的實現: - 從證書中提取公鑰並根據條碼數據和提供的數字簽名驗證公鑰。
這裏是我試圖用它來驗證簽名
//Note :
//1. certPath : is the path where my certificate is located
//2. GetStreamdata get the stream of data from the certificate.
//Get the certificate data here
Org.BouncyCastle.X509.X509Certificate cert1 = new X509CertificateParser().ReadCertificate(GetStreamData(cerPath));
//get the public key
ECPublicKeyParameters ecPublic = (ECPublicKeyParameters)cert1.GetPublicKey();
//create a signerutility with type SHA-256withECDSA
ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
//initial signer with the public key
signer.Init(false, ecPublic);
//get signature in bytes : digitalsignature parameter contains signature that should be used.
byte[] dBytes = encoding.GetBytes(digitalsignature);
//block/finalise update to signer : data : is the actual data.
signer.BlockUpdate(data, 0, data.Length);
try
{
//verify signature
verified = signer.VerifySignature(dBytes);
}
catch(Exception ex)
{
_log.LogException(ex);
}
什麼是我能達到被代碼:
Exception thrown on signer.verifysignature
Message=Unable to cast object of type 'Org.BouncyCastle.Asn1.DerApplicationSpecific' to type 'Org.BouncyCastle.Asn1.Asn1Sequence'.
Source=BouncyCastle.Crypto