DERValue derSignature = new DERValue(DER.BIT_STRING, new BitString(result)); baos.reset(); DERWriter.write(baos, derSignature); result = baos.toByteArray();
  public byte[] encodeSignature(Object signature)  {    BigInteger[] rs = (BigInteger[]) signature;    DERValue derR = new DERValue(DER.INTEGER, rs[0]);    DERValue derS = new DERValue(DER.INTEGER, rs[1]);    ArrayList dssSigValue = new ArrayList(2);    dssSigValue.add(derR);    dssSigValue.add(derS);    DERValue derDssSigValue = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,                                           dssSigValue);    byte[] result;    ByteArrayOutputStream baos = new ByteArrayOutputStream();    try      {        DERWriter.write(baos, derDssSigValue);        result = baos.toByteArray();        // put it in a BIT STRING        DERValue derSignature = new DERValue(DER.BIT_STRING,                                             new BitString(result));        baos.reset();        DERWriter.write(baos, derSignature);        result = baos.toByteArray();      }    catch (IOException x)      {        InvalidParameterException y = new InvalidParameterException();        y.initCause(x);        throw y;      }    return result;  }