public String signMessage(String message,@Nullable KeyParameter aesKey) throws KeyCrypterException {
  if (priv == null)   throw new IllegalStateException("This ECKey does not have the private key necessary for signing.");
  byte[] data=Utils.formatMessageForSigning(message);
  Sha256Hash hash=Sha256Hash.createDouble(data);
  ECDSASignature sig=sign(hash,aesKey);
  int recId=-1;
  for (int i=0; i < 4; i++) {
    ECKey k=ECKey.recoverFromSignature(i,sig,hash,isCompressed());
    if (k != null && Arrays.equals(k.pub,pub)) {
      recId=i;
      break;
    }
  }
  if (recId == -1)   throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
  int headerByte=recId + 27 + (isCompressed() ? 4 : 0);
  byte[] sigData=new byte[65];
  sigData[0]=(byte)headerByte;
  System.arraycopy(Utils.bigIntegerToBytes(sig.r,32),0,sigData,1,32);
  System.arraycopy(Utils.bigIntegerToBytes(sig.s,32),0,sigData,33,32);
  return new String(Base64.encode(sigData),Charset.forName("UTF-8"));
}

public String signMessage ( String message , @ Nullable KeyParameter aesKey ) throws KeyCrypterException { if ( priv == null ) throw new IllegalStateException ( "This ECKey does not have the private key necessary for signing." ) ; byte [ ] data = Utils . formatMessageForSigning ( message ) ; Sha256Hash hash = Sha256Hash . createDouble ( data ) ; ECDSASignature sig = sign ( hash , aesKey ) ; int recId = - 1 ; for ( int i = 0 ; i < 4 ; i ++ ) { ECKey k = ECKey . recoverFromSignature ( i , sig , hash , isCompressed ( ) ) ; if ( k != null && Arrays . equals ( k . pub , pub ) ) { recId = i ; break ; } } if ( recId == - 1 ) throw new RuntimeException ( "Could not construct a recoverable key. This should never happen." ) ; int headerByte = recId + 27 + ( isCompressed ( ) ? 4 : 0 ) ; byte [ ] sigData = new byte [ 65 ] ; sigData [ 0 ] = ( byte ) headerByte ; System . arraycopy ( Utils . bigIntegerToBytes ( sig . r , 32 ) , 0 , sigData , 1 , 32 ) ; System . arraycopy ( Utils . bigIntegerToBytes ( sig . s , 32 ) , 0 , sigData , 33 , 32 ) ; return new String ( Base64 . encode ( sigData ) , Charset . forName ( "UTF-8" ) ) ; }
signs a text message using the standard bitcoin messaging signing format and returns the signature as a base64 encoded string .
