verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle
verify signature with bouncy castle verify signature with bouncy castle
verify signature with bouncy castle
General Topics include: .NET Framework
Note: Most posts can be categorized into one of the forums below, where experts on that topic will be able to help you find a solution to your problem faster and more efficiently.

Enjoy an ad free experience by logging in. Not a member yet? Register.
Go Back  Xtreme .NET Talk > > > verify signature with bouncy castle


Reply
 
Thread Tools Display Modes
  #1  
Old 10-06-2017, 12:50 AM
lamy's Avatar
lamy lamy is offline
Regular

Preferred language:
C#, VB.NET
 
Join Date: Dec 2005
Location: under your bed
Posts: 68
lamy is on a distinguished road
Talking sign & verify signature with bouncy castle



EDIT: SOLVED IT

Since the built-in cryptography library of .NET requires that the OS has support for SHA2 (SHA256, SHA384 & SHA512) I opted to use a 3rd party library which is Bouncy Castle.

I was able to make the signing & verifying work after googling for a couple of hours and doing some trial and error plus mixing a lot of code samples.

I am using .PFX & .CER (DER format) for my certificates generated by makecert.

Here's the code

Code:
/*
// How to load the certificates by file

X509Certificate2 privateCertificate = new X509Certificate2(privateKeyPath, phraseKey, X509KeyStorageFlags.Exportable);

X509Certificate2 publicCertificate = new X509Certificate2(publicKeyPath);
*/

        private byte[] SignSha2(string Message, X509Certificate2 certificate)
        {
            var key = readPrivateKey(certificate);           
            var keyParameter = new RsaKeyParameters(key.IsPrivate, ((RsaPrivateCrtKeyParameters)key).Modulus, ((RsaPrivateCrtKeyParameters)key).Exponent);

            ISigner sig;

            string digestString = "";
            switch (certificate.SignatureAlgorithm.FriendlyName.ToUpper())
            {
                case "SHA256RSA":
                    digestString = "SHA256withRSA";
                    break;
                case "SHA384RSA":
                    digestString = "SHA384withRSA";
                    break;
                case "SHA512RSA":
                    digestString = "SHA512withRSA";
                    break;
            }

            sig = SignerUtilities.GetSigner(digestString);
            sig.Init(true, key);
            
            var bytes = Encoding.UTF8.GetBytes(Message);
            sig.BlockUpdate(bytes, 0, bytes.Length);

            return sig.GenerateSignature();
        }

        private AsymmetricKeyParameter readPrivateKey(X509Certificate2 certificate)
        {
            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certificate.PrivateKey;

            RSAParameters parameters = rsa.ExportParameters(true);

            return new RsaPrivateCrtKeyParameters(
                new BigInteger(1, parameters.Modulus),
                new BigInteger(1, parameters.Exponent),
                new BigInteger(1, parameters.D),
                new BigInteger(1, parameters.P),
                new BigInteger(1, parameters.Q),
                new BigInteger(1, parameters.DP),
                new BigInteger(1, parameters.DQ),
                new BigInteger(1, parameters.InverseQ));
        }

        private bool VerifySha2(X509Certificate2 certificate, byte[] dataBytes, byte[] signatureBytes)
        {
            var cert = DotNetUtilities.FromX509Certificate(certificate);
            var publicKey = cert.GetPublicKey();

            string digestString = "";
            switch (certificate.SignatureAlgorithm.FriendlyName.ToUpper())
            {
                case "SHA256RSA":
                    digestString = "SHA256withRSA";
                    break;
                case "SHA384RSA":
                    digestString = "SHA384withRSA";
                    break;
                case "SHA512RSA":
                    digestString = "SHA512withRSA";
                    break;
            }

            var signer = SignerUtilities.GetSigner(digestString);
            signer.Init(false, publicKey);
            signer.BlockUpdate(dataBytes, 0, dataBytes.Length);

            return signer.VerifySignature(signatureBytes);
        }
__________________
slow down when you need to hurry, stop when you need to move on,
look back when you need to forget, or you might slip and leave sanity

Last edited by lamy; 10-06-2017 at 02:21 AM. Reason: SOLVED IT
Reply With Quote
Reply

Bookmarks

Tags
biginteger1, digeststring, var, byte[], string, private, return, signature, asymmetrickeyparameter, break;, key, certificate, digest, bytes, castle, signing, isigner, message, library, verify, bouncy, sha2digest, code, error, key;

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: "managed PInvoke signature does not match the unmanaged target signature." Dre__ General 4 03-18-2010 11:58 PM
Verify datatable has records cpopham Database / XML / Reporting 2 04-11-2005 09:27 AM
Is there a way to verify Webservice url if it's exists or not? goodmorningsky Network 6 03-22-2005 03:13 PM
Please verify ADO.NET Databinding and Recordset Updates Napivo1972 Database / XML / Reporting 0 09-23-2004 09:36 AM
Verify image talahaski Graphics and Multimedia 2 05-14-2004 12:59 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle verify signature with bouncy castle
verify signature with bouncy castle
verify signature with bouncy castle
 
verify signature with bouncy castle
verify signature with bouncy castle