/** Generate a Diffie-Hellman keypair of the specified size. */ DHCrypt(int keyLength, SecureRandom random) { try { KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman"); kpg.initialize(keyLength, random); KeyPair kp = kpg.generateKeyPair(); privateKey = kp.getPrivate(); DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic()); publicValue = spec.getY(); modulus = spec.getP(); base = spec.getG(); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not generate DH keypair", e); } }
/** * Generate a Diffie-Hellman keypair using the specified parameters. * * @param modulus the Diffie-Hellman modulus P * @param base the Diffie-Hellman base G */ DHCrypt(BigInteger modulus, BigInteger base, SecureRandom random) { this.modulus = modulus; this.base = base; try { KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman"); DHParameterSpec params = new DHParameterSpec(modulus, base); kpg.initialize(params, random); KeyPair kp = kpg.generateKeyPair(); privateKey = kp.getPrivate(); DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic()); publicValue = spec.getY(); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not generate DH keypair", e); } }