Responsive Ad Area

Share This Post

test

How to using openssl implement the Java DSADERtoPlain in C++?

As a beginner, I meet a challenge when I using Openssl implement the ECDSA in C++.

In Java, it had

byte[] sig = ctx.sign();

sig = DSADERtoPlain(sig);

The implement of DSADERtoPlain() is the following:

private byte[] DSADERtoPlain(byte[] sig) throws IOException {

    ASN1Sequence seq = (ASN1Sequence) ASN1Primitive.fromByteArray(sig);

    if (seq.size() != 2) {

        throw new IOException(ErrorCode.MalformedSignature);

    } else if (!Arrays.equals(sig, seq.getEncoded("DER"))) {

        throw new IOException(ErrorCode.MalformedSignature);

    }



    byte[] r = ASN1Integer.getInstance(seq.getObjectAt(0)).getValue().toByteArray();

    byte[] s = ASN1Integer.getInstance(seq.getObjectAt(1)).getValue().toByteArray();

    int ri = (r[0] == 0) ? 1 : 0;

    int rl = r.length - ri;

    int si = (s[0] == 0) ? 1 : 0;

    int sl = s.length - si;

    byte[] res;

    if (rl > sl) {

        res = new byte[rl * 2];

    } else {

        res = new byte[sl * 2];

    }

    System.arraycopy(r, ri, res, res.length/2 - rl, rl);

    System.arraycopy(s, si, res, res.length-sl, sl);

    return res;

}

But when I use openssl in C++, to generate the signature, I just use the following:

EVP_SignInit_ex();

EVP_SignUpdate();

EVP_SignFinal();

The result of above is longer than the result of Java.

I try to fix this problem by using <openssl/asn1.h>, but the reference seems too difficult for me.

So, how could I implement the Java DSADERtoPlain in C++ by using openssl?


How to using openssl implement the Java DSADERtoPlain in C++?
How to using openssl implement the Java DSADERtoPlain in C++?
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>