public static byte[] convertDsaASN1toXMLDSIG(byte[] asn1Bytes,int size) throws IOException {
  if (asn1Bytes[0] != 48 || asn1Bytes[1] != asn1Bytes.length - 2 || asn1Bytes[2] != 2) {
    throw new IOException("Invalid ASN.1 format of DSA signature");
  }
  byte rLength=asn1Bytes[3];
  int i;
  for (i=rLength; i > 0 && asn1Bytes[4 + rLength - i] == 0; i--)   ;
  byte sLength=asn1Bytes[5 + rLength];
  int j;
  for (j=sLength; j > 0 && asn1Bytes[6 + rLength + sLength - j] == 0; j--)   ;
  if (i > size || asn1Bytes[4 + rLength] != 2 || j > size) {
    throw new IOException("Invalid ASN.1 format of DSA signature");
  }
 else {
    byte[] xmldsigBytes=new byte[size * 2];
    System.arraycopy(asn1Bytes,4 + rLength - i,xmldsigBytes,size - i,i);
    System.arraycopy(asn1Bytes,6 + rLength + sLength - j,xmldsigBytes,size * 2 - j,j);
    return xmldsigBytes;
  }
}

public static byte [ ] convertDsaASN1toXMLDSIG ( byte [ ] asn1Bytes , int size ) throws IOException { if ( asn1Bytes [ 0 ] != 48 || asn1Bytes [ 1 ] != asn1Bytes . length - 2 || asn1Bytes [ 2 ] != 2 ) { throw new IOException ( "Invalid ASN.1 format of DSA signature" ) ; } byte rLength = asn1Bytes [ 3 ] ; int i ; for ( i = rLength ; i > 0 && asn1Bytes [ 4 + rLength - i ] == 0 ; i -- ) ; byte sLength = asn1Bytes [ 5 + rLength ] ; int j ; for ( j = sLength ; j > 0 && asn1Bytes [ 6 + rLength + sLength - j ] == 0 ; j -- ) ; if ( i > size || asn1Bytes [ 4 + rLength ] != 2 || j > size ) { throw new IOException ( "Invalid ASN.1 format of DSA signature" ) ; } else { byte [ ] xmldsigBytes = new byte [ size * 2 ] ; System . arraycopy ( asn1Bytes , 4 + rLength - i , xmldsigBytes , size - i , i ) ; System . arraycopy ( asn1Bytes , 6 + rLength + sLength - j , xmldsigBytes , size * 2 - j , j ) ; return xmldsigBytes ; } }
converts an asn . 1 dsa value to a xml signature dsa value . the jce dsa signature algorithm creates asn . 1 encoded ( r , s ) value pairs ( see section 2 . 2 . 2 of rfc 3279 ) ; the xml signature requires the core biginteger values .
