kran
kran

Reputation: 73

ECDSA signature generation KeyPair Java to C# - JcaPEMKeyConverter()

I have been converting over some code from a Java Android app to C# using Xamarin and I have come across a problem when trying to generate a signature using a certain snippet of BouncyCastle code.

Is there a replacement function in C# for the line of code "pair = new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) parsed);" ??

This is the Java code:

// Generating the signature
Signature signature = Signature.getInstance("SHA256withECDSA");
Reader rdr = new StringReader("privatekeygoeshere");
Object parsed = new PEMParser(rdr).readObject();
KeyPair pair;
pair = new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) parsed);
PrivateKey signingKey = pair.getPrivate();

signature.initSign(signingKey);
signature.update(nonceData1);
signature.update(nonceData2);
signature.update(collectorID);
signature.update(publicKeyCompressed);

byte[] signedData = signature.sign();

I have found another way to read the private key and create a KeyPair. However, the private key is stored as a AsymmetricCipherKeyPair which I cannot add into the signature.InitSign() function as this requires an IPrivateKey.

The Different ways that I have tried to create a signature do not allow me to update other byte array data to the signature generation like the Java code, this doesn't work for me so I am really stuck. I am also open to any ideas of signature generation.

Example of this here:

AsymmetricKeyParameter signingKey;
            AsymmetricCipherKeyPair keyPair = null;
            using (var textReader = new System.IO.StringReader("privatekeygoeshere"))
            {
                // Only a private key
                Org.BouncyCastle.OpenSsl.PemReader pemReader = new Org.BouncyCastle.OpenSsl.PemReader(textReader);
                keyPair = pemReader.ReadObject() as AsymmetricCipherKeyPair;
                signingKey = keyPair.Private;
            }

Upvotes: 2

Views: 323

Answers (1)

kran
kran

Reputation: 73

I managed to come up with a solution for my problem using a string reader and looping through each array using the Update() command. This works well for me however, if any one can find a better way of doing this... Please comment below.

        AsymmetricKeyParameter signingKey;
        using (var textReader = new System.IO.StringReader(LONG_TERM_PRIVATE_KEY))
        {
            // Only a private key
            Org.BouncyCastle.OpenSsl.PemReader pemReader = new Org.BouncyCastle.OpenSsl.PemReader(textReader);
            keyPair = pemReader.ReadObject() as AsymmetricCipherKeyPair;
            signingKey = keyPair.Private;
        }

        var signer = SignerUtilities.GetSigner("SHA256withECDSA");
        signer.Init(true, signingKey);
        foreach (byte b in terminalNonce)
        {
            signer.Update(b);
        }
        foreach (byte b in mobileDeviceNonce)
        {
            signer.Update(b);
        }
        foreach (byte b in COLLECTOR_ID)
        {
            signer.Update(b);
        }
        foreach (byte b in terminalEphemeralPublicKeyCompressed)
        {
            signer.Update(b);
        }

        var signed = signer.GenerateSignature();

Upvotes: 2

Related Questions