public static BigInteger decodeMPI(byte[] mpi,boolean hasLength){
  byte[] buf;
  if (hasLength) {
    int length=(int)readUint32BE(mpi,0);
    buf=new byte[length];
    System.arraycopy(mpi,4,buf,0,length);
  }
 else   buf=mpi;
  if (buf.length == 0)   return BigInteger.ZERO;
  boolean isNegative=(buf[0] & 0x80) == 0x80;
  if (isNegative)   buf[0]&=0x7f;
  BigInteger result=new BigInteger(buf);
  return isNegative ? result.negate() : result;
}

public static BigInteger decodeMPI ( byte [ ] mpi , boolean hasLength ) { byte [ ] buf ; if ( hasLength ) { int length = ( int ) readUint32BE ( mpi , 0 ) ; buf = new byte [ length ] ; System . arraycopy ( mpi , 4 , buf , 0 , length ) ; } else buf = mpi ; if ( buf . length == 0 ) return BigInteger . ZERO ; boolean isNegative = ( buf [ 0 ] & 0x80 ) == 0x80 ; if ( isNegative ) buf [ 0 ] &= 0x7f ; BigInteger result = new BigInteger ( buf ) ; return isNegative ? result . negate ( ) : result ; }
mpi encoded numbers are produced by the openssl bn_bn2mpi function . they consist of a 4 byte big endian length field , followed by the stated number of bytes representing the number in big endian format ( with a sign bit ) .
