public CipherParameters encrypt(byte[] out,int outOff,int keyLen) throws IllegalArgumentException {
  if (key.isPrivate()) {
    throw new IllegalArgumentException("Public key required for encryption");
  }
  BigInteger n=key.getModulus();
  BigInteger e=key.getExponent();
  BigInteger r=BigIntegers.createRandomInRange(ZERO,n.subtract(ONE),rnd);
  BigInteger c=r.modPow(e,n);
  byte[] C=BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8,c);
  System.arraycopy(C,0,out,outOff,C.length);
  return generateKey(n,r,keyLen);
}

public CipherParameters encrypt ( byte [ ] out , int outOff , int keyLen ) throws IllegalArgumentException { if ( key . isPrivate ( ) ) { throw new IllegalArgumentException ( "Public key required for encryption" ) ; } BigInteger n = key . getModulus ( ) ; BigInteger e = key . getExponent ( ) ; BigInteger r = BigIntegers . createRandomInRange ( ZERO , n . subtract ( ONE ) , rnd ) ; BigInteger c = r . modPow ( e , n ) ; byte [ ] C = BigIntegers . asUnsignedByteArray ( ( n . bitLength ( ) + 7 ) / 8 , c ) ; System . arraycopy ( C , 0 , out , outOff , C . length ) ; return generateKey ( n , r , keyLen ) ; }
generate and encapsulate a random session key .
