title: How to encrypt chat application in java?
body: <p>I'm trying to write a chat program with RSA encryption in Java. I found RSA class:</p>
  
  <pre><code>public class RSA {
  
  private BigInteger n, d, e;
  
  private int bits = 1024;
  
  /** Create an instance that can encrypt using someone elses public key. */
  public RSA(BigInteger newn, BigInteger newe) {
      n = newn;
      e = newe;
  }
  
  /** Create an instance that can both encrypt and decrypt. */
  public RSA(int bits) {
      this.bits = bits;
      SecureRandom r = new SecureRandom();
      BigInteger p = new BigInteger(this.bits / 2, 100, r);
      BigInteger q = new BigInteger(this.bits / 2, 100, r);
      n = p.multiply(q);
      BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
          .subtract(BigInteger.ONE));
      e = new BigInteger("3");
      while (m.gcd(e).intValue() &gt; 1) {
          e = e.add(new BigInteger("2"));
      }
      d = e.modInverse(m);
  }
  
  /** Encrypt the given plaintext message. */
  public synchronized String encrypt(String message) {
      return (new BigInteger(message.getBytes())).modPow(e, n).toString();
  }
  
  /** Encrypt the given plaintext message. */
  public synchronized BigInteger encrypt(BigInteger message) {
      return message.modPow(e, n);
  }
  
  /** Decrypt the given ciphertext message. */
  public synchronized String decrypt(String message) {
      return new String((new BigInteger(message)).modPow(d, n).toByteArray());
  }
  
  /** Decrypt the given ciphertext message. */
  public synchronized BigInteger decrypt(BigInteger message) {
      return message.modPow(d, n);
  }
  
  /** Generate a new public and private key set. */
  public synchronized void generateKeys() {
      SecureRandom r = new SecureRandom();
      BigInteger p = new BigInteger(bits / 2, 100, r);
      BigInteger q = new BigInteger(bits / 2, 100, r);
      n = p.multiply(q);
      BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
          .subtract(BigInteger.ONE));
      e = new BigInteger("3");
      while (m.gcd(e).intValue() &gt; 1) {
          e = e.add(new BigInteger("2"));
      }
      d = e.modInverse(m);
  }
  
  /** Return the modulus. */
  public synchronized BigInteger getN() {
      return n;
  }
  
  /** Return the public key. */
  public synchronized BigInteger getE() {
      return e;
  }
  }
  </code></pre>
  
  <p>When server starts it creates own RSA object: </p>
  
  <pre><code>public static RSA ownRsa = new RSA(1024);
  </code></pre>
  
  <p>So, after that I created 2 methods for clients:</p>
  
  <pre><code>public static BigInteger getServersN() {
      return ownRsa.getN();
  }
  
  
  public static BigInteger getServersE() {
      return ownRsa.getE();
  }
  </code></pre>
  
  <p>When client starts, it also creates his own RSA object, also he creates another rsa object with server's public key:</p>
  
  <pre><code>public static RSA ownRsa = new RSA(1024);
  public static RSA rsa = new RSA(ServerAction.getServersN(), ServerAction.getServersE());
  </code></pre>
  
  <p>So when client sends message it will be encrypted with server's public key(N and E). When message arrives to server I can decrypt it. But how can I encrypt message on server to send it to another clients? As I understood I need to send public keys from each client to server but then I have to encrypt lots of messages and keep all public keys. Maybe my logic completely wrong or I don't get something. Can anyone give any advice? Thank you.</p>