public static byte[] bigIntegerToByteArray(BigInteger value,int blockSize,boolean removeSignByte){
  byte[] array=value.toByteArray();
  int remainder=array.length % blockSize;
  byte[] result=array;
  byte[] tmp;
  if (removeSignByte && result[0] == 0x0) {
    tmp=new byte[result.length - 1];
    System.arraycopy(result,1,tmp,0,tmp.length);
    result=tmp;
    remainder=tmp.length % blockSize;
  }
  if (remainder > 0) {
    tmp=new byte[result.length + blockSize - remainder];
    System.arraycopy(result,0,tmp,blockSize - remainder,result.length);
    result=tmp;
  }
  return result;
}

public static byte [ ] bigIntegerToByteArray ( BigInteger value , int blockSize , boolean removeSignByte ) { byte [ ] array = value . toByteArray ( ) ; int remainder = array . length % blockSize ; byte [ ] result = array ; byte [ ] tmp ; if ( removeSignByte && result [ 0 ] == 0x0 ) { tmp = new byte [ result . length - 1 ] ; System . arraycopy ( result , 1 , tmp , 0 , tmp . length ) ; result = tmp ; remainder = tmp . length % blockSize ; } if ( remainder > 0 ) { tmp = new byte [ result . length + blockSize - remainder ] ; System . arraycopy ( result , 0 , tmp , blockSize - remainder , result . length ) ; result = tmp ; } return result ; }
takes a biginteger value and returns its byte array representation filled with 0x00 bytes to achieve the block size length .
