c# - 使用 BouncyCaSTLe 的数字签名验证 - ECDSA with SHA 256, C#

标签 c# bouncycastle

以下是我的场景 - 我从条形码读取数据并将其转换为纯文本。此文本是条形码数据+数字签名的组合。数字签名附加在最后,这使我能够将实际数据和数字签名数据分开。使用 sha256 对数字签名数据进行哈希处理 -用户向我发送一个公钥作为 Windows 证书文件(.cer 扩展名)。

要求的实现: -需要从证书中提取公钥,并根据提供的条形码数据和数字签名验证公钥。

这是我用来验证签名的代码

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

我能够实现的是:使用 bouncy caSTLe 库提取 public

问题:

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 中对数字签名值进行编码。我以前用 ASCII 编码。 这解决了问题,我能够验证签名。

关于c# - 使用 BouncyCaSTLe 的数字签名验证 - ECDSA with SHA 256, C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263641/

相关文章:

java - 将 "RSA/ECB/PKCS7Padding"与充气城堡一起使用

Java 8 使用带有 SealedObject 的 ECIES 密码失败,并显示 'NullPointerException' - 字符串不能为空

java - AES 128 CFB、Java/BouncyCaSTLe 与 Ruby/OpenSSL

c# - AutoResetEvent澄清

c# - 如何通过缓存键锁定?

c# - 使用成员作为实现者实现接口(interface)

c# - 在 ASP.NET 中使用 Ext JS

java - 具有公钥的 RSA 签名作为未在 java 中验证的文本

android - 在 android 应用程序中打包文件(但不在 res 或 assets 中)

c# - 非 Controller 类无法访问 DbContext