[英]Digital Signature Verification using BouncyCastle - ECDSA with SHA 256, C#
以下是我的方案 - 我從條形碼中讀取數據並將其轉換為純文本。 此文本是條形碼數據+數字簽名的組合。 數字簽名附加到末尾,這使我能夠分離出實際數據和數字簽名數據。 使用sha256對數字簽名數據進行哈希處理 - 用戶將公鑰作為Windows證書文件( .cer extension
)發送給我。
必需的實現: - 需要從證書中提取公鑰,並根據條形碼數據和提供的數字簽名驗證公鑰。
這是我試圖用來驗證簽名的代碼
//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
問題是我必須在iso-8859-1中編碼digitalsignature值。 我以前用ASCII編碼。 這解決了問題,我能夠驗證簽名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.