public static byte[] convertASN1toXMLDSIG(byte asn1Bytes[]) throws IOException {
  if (asn1Bytes.length < 8 || asn1Bytes[0] != 48) {
    throw new IOException("Invalid ASN.1 format of ECDSA signature");
  }
  int offset;
  if (asn1Bytes[1] > 0) {
    offset=2;
  }
 else   if (asn1Bytes[1] == (byte)0x81) {
    offset=3;
  }
 else {
    throw new IOException("Invalid ASN.1 format of ECDSA signature");
  }
  byte rLength=asn1Bytes[offset + 1];
  int i;
  for (i=rLength; (i > 0) && (asn1Bytes[(offset + 2 + rLength) - i] == 0); i--)   ;
  byte sLength=asn1Bytes[offset + 2 + rLength+ 1];
  int j;
  for (j=sLength; (j > 0) && (asn1Bytes[(offset + 2 + rLength+ 2+ sLength) - j] == 0); j--)   ;
  int rawLen=Math.max(i,j);
  if ((asn1Bytes[offset - 1] & 0xff) != asn1Bytes.length - offset || (asn1Bytes[offset - 1] & 0xff) != 2 + rLength + 2+ sLength || asn1Bytes[offset] != 2 || asn1Bytes[offset + 2 + rLength] != 2) {
    throw new IOException("Invalid ASN.1 format of ECDSA signature");
  }
  byte xmldsigBytes[]=new byte[2 * rawLen];
  System.arraycopy(asn1Bytes,(offset + 2 + rLength) - i,xmldsigBytes,rawLen - i,i);
  System.arraycopy(asn1Bytes,(offset + 2 + rLength+ 2+ sLength) - j,xmldsigBytes,2 * rawLen - j,j);
  return xmldsigBytes;
}

public static byte [ ] convertASN1toXMLDSIG ( byte asn1Bytes [ ] ) throws IOException { if ( asn1Bytes . length < 8 || asn1Bytes [ 0 ] != 48 ) { throw new IOException ( "Invalid ASN.1 format of ECDSA signature" ) ; } int offset ; if ( asn1Bytes [ 1 ] > 0 ) { offset = 2 ; } else if ( asn1Bytes [ 1 ] == ( byte ) 0x81 ) { offset = 3 ; } else { throw new IOException ( "Invalid ASN.1 format of ECDSA signature" ) ; } byte rLength = asn1Bytes [ offset + 1 ] ; int i ; for ( i = rLength ; ( i > 0 ) && ( asn1Bytes [ ( offset + 2 + rLength ) - i ] == 0 ) ; i -- ) ; byte sLength = asn1Bytes [ offset + 2 + rLength + 1 ] ; int j ; for ( j = sLength ; ( j > 0 ) && ( asn1Bytes [ ( offset + 2 + rLength + 2 + sLength ) - j ] == 0 ) ; j -- ) ; int rawLen = Math . max ( i , j ) ; if ( ( asn1Bytes [ offset - 1 ] & 0xff ) != asn1Bytes . length - offset || ( asn1Bytes [ offset - 1 ] & 0xff ) != 2 + rLength + 2 + sLength || asn1Bytes [ offset ] != 2 || asn1Bytes [ offset + 2 + rLength ] != 2 ) { throw new IOException ( "Invalid ASN.1 format of ECDSA signature" ) ; } byte xmldsigBytes [ ] = new byte [ 2 * rawLen ] ; System . arraycopy ( asn1Bytes , ( offset + 2 + rLength ) - i , xmldsigBytes , rawLen - i , i ) ; System . arraycopy ( asn1Bytes , ( offset + 2 + rLength + 2 + sLength ) - j , xmldsigBytes , 2 * rawLen - j , j ) ; return xmldsigBytes ; }
converts an asn . 1 ecdsa value to a xml signature ecdsa value . the java jce ecdsa signature algorithm creates asn . 1 encoded ( r , s ) value pairs ; the xml signature requires the core biginteger values .
