简体   繁体   English

使用BouncyCastle进行数字签名验证 - 带有SHA 256,C#的ECDSA

[英]Digital Signature Verification using BouncyCastle - ECDSA with SHA 256, C#

Following is my scenario - I read data from a barcode and its converted into a plain text. 以下是我的方案 - 我从条形码中读取数据并将其转换为纯文本。 This text is a combination of barcode data + digital signature. 此文本是条形码数据+数字签名的组合。 Digital signature is appended to the end, which enables me to separate out the actual data and digital signature data. 数字签名附加到末尾,这使我能够分离出实际数据和数字签名数据。 Digital signature data is hashed using sha256 -User send me a public key as windows certificate file ( .cer extension ). 使用sha256对数字签名数据进行哈希处理 - 用户将公钥作为Windows证书文件( .cer extension )发送给我。

Required implementation : -Need to extract public key from the certificate and validate the public key against the barcode data and the digital signature provided. 必需的实现: - 需要从证书中提取公钥,并根据条形码数据和提供的数字签名验证公钥。

Here is the code I am trying to use to verify signature 这是我试图用来验证签名的代码

//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); 
                    } 

what was I able to achieve was : extract public using bouncy castle libraries 我能够实现的是:使用充气城堡图书馆提取公众

Problem : 问题:

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 

The problem was that I had to encode digitalsignature value in iso-8859-1. 问题是我必须在iso-8859-1中编码digitalsignature值。 I was encoding in ASCII before. 我以前用ASCII编码。 This solves the problem and I was able to validate signature. 这解决了问题,我能够验证签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 使用带有 SHA256 证书的 ECDSA 进行签名验证 - C# signature verification using ECDSA with SHA256 certificate 如何在C#中使用带有ECDSA算法的SHA256创建数字签名 - How to create a digital signature using SHA256 with ECDSA algorithm in C# 使用BouncyCastle验证在C#中用ECDSA(带有SHA256)签名的XMLSignature抛出InvalidCastException - Verifying XMLSignature signed with ECDSA (with SHA256) in C# using BouncyCastle throws InvalidCastException 如何在 C# 中使用 SHA256 签名对 ECDSA 进行签名和验证 - How to sign and verify an ECDSA with SHA256 signature in C# 不使用BouncyCastle的C#中的数字签名 - Digital signature in c# without using BouncyCastle 使用 BouncyCastle 使用 SHA-256withECDSA 的 C# 签名数据每次都会产生不同的签名 - C# Sign Data with SHA-256withECDSA using BouncyCastle produce different Signature everytime 使用 c# 使用 sha256 进行数字签名 - Digital sign with sha256 with c# 使用SoftHSM 2.2.0(带有SHA256的ECDSA)C#.net从Pkcs11Interop为CKM_ECDSA_SHA256签名PDF - Signing PDF from Pkcs11Interop for CKM_ECDSA_SHA256 using SoftHSM 2.2.0 (ECDSA with SHA256) C# .net SSO SAML的签名XML签名验证(使用sha256) - Signed XML signature verification for SSO SAML (Using sha256) 使用 BouncyCastle 验证 ECDSA 签名 - Verifying ECDSA signature with BouncyCastle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM