0

I am trying to convert a hex ECDSA signature to ASN1 DER but getting error.

Please help with pointers to correct

Java function :

 public ASN1Sequence convertSignatureToASN1DER(String signatureHex)
 {  
        System.out.println("Encoding hex signature to ASN1 DER... ");  

        System.out.println("Received signature string : "+signatureHex);  
        //extracting r and s   
        System.out.println("Extracting r and s  as string");  
        byte[] first32SignatureBytes = Arrays.copyOfRange(DatatypeConverter.parseHexBinary(signatureHex), 0,32);  
        String signaturePartR = Hex.encodeHexString(first32SignatureBytes);
        byte[] last32SignatureBytes = Arrays.copyOfRange(DatatypeConverter.parseHexBinary(signatureHex), 32,64);  
        String signaturePartS = Hex.encodeHexString(last32SignatureBytes);  


        //converting r and s to asn1 and putting them in sequence  
        System.out.println("Converting r and s to asn1 and putting them in sequence");  
        ASN1Integer r = new ASN1Integer(new BigInteger(signaturePartR, 16));  
        ASN1Integer s = new ASN1Integer(new BigInteger(signaturePartS, 16));  
        ASN1Sequence seq = new DERSequence(new ASN1Encodable[] { r, s });  
        System.out.println("r and s converted to asn1.. ");  


        return seq; 
    }

Error log :

Received signature string : b2b31575f8536b284430C01217f688be3a9faf4ba0ba3a9093f983e40d630ec722a7a25b01403cff0d00b3b853d230f8e96ff832b15d4ccc75203cb65896a2d5  
Extracting r and s  as string  
Converting r and s to asn1 and putting them in sequence
[2m2018-03-12 10:37:21.958[0;39m [31mERROR[0;39m [35m25020[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet]   [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z] with root cause

java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
                    at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_77]
                    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_77]
pedrofb
  • 37,271
  • 5
  • 94
  • 142
vison
  • 59
  • 1
  • 9
  • 1
    I would guess you have messed up your classpath somehow, but I have no idea how. – President James K. Polk Mar 16 '18 at 16:44
  • See https://stackoverflow.com/a/2518002/6371459 Probably the bouncycastle version used to compile is different that the one used to execute – pedrofb Mar 16 '18 at 16:54
  • I don't see any problem with the code provided, other than the fact that you should keep to one way of encoding / decoding (e.g. the Bouncy provided `Hex` class) and that you should really prefer byte arrays over String values. – Maarten Bodewes Mar 17 '18 at 17:12
  • The above code runs fine on my system, so I'm voting to close the question as this is likely a classpath issue, and we don't have the required info to solve that. – Maarten Bodewes Mar 18 '18 at 15:08
  • Thank you.. error disappeared after reference library cleanup.. regards – vison Mar 19 '18 at 15:23
  • I know you have closed, but is there a tool available to cross check encoding? Example : I give input as b2b31575f8536b284410d01217f688be3a9faf4ba0ba3a9093f983e40d630ec722a7a25b01403cff0d00b3b853d230f8e96ff832b15d4ccc75203cb65896a2d5 and output is 3045022100B2B31575F8536B284410D01217F688BE3A9FAF4BA0BA3A9093F983E40D630EC7022022A7A25B01403CFF0D00B3B853D230F8E96FF832B15D4CCC75203CB65896A2D5 I am not sure if this is correct – vison Mar 20 '18 at 18:06
  • I can see that encoded signature is in the format as per ASN1 DER Received Signature R = b2b31575f8536b284410d01217f688be3a9faf4ba0ba3a9093f983e40d630ec7 | S = 22a7a25b01403cff0d00b3b853d230f8e96ff832b15d4ccc75203cb65896a2d5 Breakdown ASN1 DER signature format 0x30 b1 0x02 b2 (vr) 0x02 b3 (vs): 30 45 02 21 00B2B31575F8536B284410D01217F688BE3A9FAF4BA0BA3A9093F983E40D630EC7 02 20 22A7A25B01403CFF0D00B3B853D230F8E96FF832B15D4CCC75203CB65896A2D5 – vison Mar 20 '18 at 19:14
  • Just to let you know: since you didn't "@" me, those comments never reached their destination. Probably best to ask a new question next time. – Maarten Bodewes Jan 25 '20 at 13:18

0 Answers0