public static byte[] convertXMLDSIGtoASN1(byte xmldsigBytes[]) throws IOException {
  int rawLen=xmldsigBytes.length / 2;
  int i;
  for (i=rawLen; (i > 0) && (xmldsigBytes[rawLen - i] == 0); i--)   ;
  int j=i;
  if (xmldsigBytes[rawLen - i] < 0) {
    j+=1;
  }
  int k;
  for (k=rawLen; (k > 0) && (xmldsigBytes[2 * rawLen - k] == 0); k--)   ;
  int l=k;
  if (xmldsigBytes[2 * rawLen - k] < 0) {
    l+=1;
  }
  int len=2 + j + 2+ l;
  if (len > 255) {
    throw new IOException("Invalid XMLDSIG format of ECDSA signature");
  }
  int offset;
  byte asn1Bytes[];
  if (len < 128) {
    asn1Bytes=new byte[2 + 2 + j+ 2+ l];
    offset=1;
  }
 else {
    asn1Bytes=new byte[3 + 2 + j+ 2+ l];
    asn1Bytes[1]=(byte)0x81;
    offset=2;
  }
  asn1Bytes[0]=48;
  asn1Bytes[offset++]=(byte)len;
  asn1Bytes[offset++]=2;
  asn1Bytes[offset++]=(byte)j;
  System.arraycopy(xmldsigBytes,rawLen - i,asn1Bytes,(offset + j) - i,i);
  offset+=j;
  asn1Bytes[offset++]=2;
  asn1Bytes[offset++]=(byte)l;
  System.arraycopy(xmldsigBytes,2 * rawLen - k,asn1Bytes,(offset + l) - k,k);
  return asn1Bytes;
}

public static byte [ ] convertXMLDSIGtoASN1 ( byte xmldsigBytes [ ] ) throws IOException { int rawLen = xmldsigBytes . length / 2 ; int i ; for ( i = rawLen ; ( i > 0 ) && ( xmldsigBytes [ rawLen - i ] == 0 ) ; i -- ) ; int j = i ; if ( xmldsigBytes [ rawLen - i ] < 0 ) { j += 1 ; } int k ; for ( k = rawLen ; ( k > 0 ) && ( xmldsigBytes [ 2 * rawLen - k ] == 0 ) ; k -- ) ; int l = k ; if ( xmldsigBytes [ 2 * rawLen - k ] < 0 ) { l += 1 ; } int len = 2 + j + 2 + l ; if ( len > 255 ) { throw new IOException ( "Invalid XMLDSIG format of ECDSA signature" ) ; } int offset ; byte asn1Bytes [ ] ; if ( len < 128 ) { asn1Bytes = new byte [ 2 + 2 + j + 2 + l ] ; offset = 1 ; } else { asn1Bytes = new byte [ 3 + 2 + j + 2 + l ] ; asn1Bytes [ 1 ] = ( byte ) 0x81 ; offset = 2 ; } asn1Bytes [ 0 ] = 48 ; asn1Bytes [ offset ++ ] = ( byte ) len ; asn1Bytes [ offset ++ ] = 2 ; asn1Bytes [ offset ++ ] = ( byte ) j ; System . arraycopy ( xmldsigBytes , rawLen - i , asn1Bytes , ( offset + j ) - i , i ) ; offset += j ; asn1Bytes [ offset ++ ] = 2 ; asn1Bytes [ offset ++ ] = ( byte ) l ; System . arraycopy ( xmldsigBytes , 2 * rawLen - k , asn1Bytes , ( offset + l ) - k , k ) ; return asn1Bytes ; }
converts a xml signature ecdsa value to an asn . 1 dsa value . the java jce ecdsa signature algorithm creates asn . 1 encoded ( r , s ) value pairs ; the xml signature requires the core biginteger values .
