Package tools.elgamal

Source Code of tools.elgamal.KeyGenerator

package tools.elgamal;

import java.math.BigInteger;
import java.util.Random;

public class KeyGenerator {

  private BigInteger p;

  private BigInteger g;

  private BigInteger a;

  private BigInteger b;

  public KeyGenerator(int bitlength) {
    Random random = new Random(System.currentTimeMillis());
    p = BigInteger.probablePrime(bitlength, random);
    // Use Random number to find a generator
    while (true) {
      BigInteger pg = BigInteger.probablePrime(bitlength, random).mod(p);
      if (!pg.equals(BigInteger.ONE)
          && !pg.modPow(BigInteger.valueOf(2), p).equals(
              BigInteger.ONE)
          && !pg.modPow(
              p.subtract(BigInteger.ONE).divide(
                  BigInteger.valueOf(2)), p).equals(
              BigInteger.ONE)) {
        g = pg;
        break;
      }
    }
    a = BigInteger.probablePrime(bitlength - 1, random).mod(p);
    b = g.modPow(a, p);
  }

  public PublicKey getPublicKey() {
    return new PublicKey(p, g, b);
  }

  public PrivateKey getPrivateKey() {
    return new PrivateKey(p, g, a);
  }
}
TOP

Related Classes of tools.elgamal.KeyGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.