Java 类javax.crypto.spec.DHParameterSpec 实例源码

项目:ipack    文件:KeyPairGeneratorSpi.java   
public void initialize(
    AlgorithmParameterSpec params,
    SecureRandom random)
    throws InvalidAlgorithmParameterException
{
    if (!(params instanceof DHParameterSpec))
    {
        throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
    }
    DHParameterSpec dhParams = (DHParameterSpec)params;

    param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));

    engine.init(param);
    initialised = true;
}
项目:ipack    文件:AlgorithmParametersSpi.java   
protected void engineInit(
    AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException
{
    if (!(paramSpec instanceof ElGamalParameterSpec) && !(paramSpec instanceof DHParameterSpec))
    {
        throw new InvalidParameterSpecException("DHParameterSpec required to initialise a ElGamal algorithm parameters object");
    }

    if (paramSpec instanceof ElGamalParameterSpec)
    {
        this.currentSpec = (ElGamalParameterSpec)paramSpec;
    }
    else
    {
        DHParameterSpec s = (DHParameterSpec)paramSpec;

        this.currentSpec = new ElGamalParameterSpec(s.getP(), s.getG());
    }
}
项目:OpenJSharp    文件:ParameterCache.java   
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
项目:monarch    文件:GMSEncrypt.java   
/**
 * Initialize the Diffie-Hellman keys. This method is not thread safe
 */
private void initDHKeys(DistributionConfig config) throws Exception {

  dhSKAlgo = config.getSecurityUDPDHAlgo();
  // Initialize the keys when either the host is a peer that has
  // non-blank setting for DH symmetric algo, or this is a server
  // that has authenticator defined.
  if ((dhSKAlgo != null && dhSKAlgo.length() > 0)) {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
    DHParameterSpec dhSpec = new DHParameterSpec(dhP, dhG, dhL);
    keyGen.initialize(dhSpec);
    KeyPair keypair = keyGen.generateKeyPair();

    // Get the generated public and private keys
    dhPrivateKey = keypair.getPrivate();
    dhPublicKey = keypair.getPublic();
  }
}
项目:wolfcrypt-jni    文件:WolfCryptKeyPairGeneratorTest.java   
@Test
public void testKeyPairGeneratorDhInitializeWithParamSpec()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException {

    int testDHKeySizes[] = { 512, 1024, 2048 };

    for (int i = 0; i < testDHKeySizes.length; i++) {

        KeyPairGenerator kpg =
            KeyPairGenerator.getInstance("DH", "wolfJCE");

        DHParameterSpec spec = new DHParameterSpec(
                new BigInteger(prime),
                new BigInteger(base),
                testDHKeySizes[i]);

        kpg.initialize(spec);
        KeyPair pair = kpg.generateKeyPair();
    }
}
项目:wolfcrypt-jni    文件:WolfCryptKeyPairGeneratorTest.java   
@Test
public void testKeyPairGeneratorDhMultipleKeyGen()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException {

    KeyPairGenerator kpg =
        KeyPairGenerator.getInstance("DH", "wolfJCE");

    DHParameterSpec spec = new DHParameterSpec(
            new BigInteger(prime), new BigInteger(base), 512);

    kpg.initialize(spec);

    KeyPair kp1 = kpg.generateKeyPair();
    KeyPair kp2 = kpg.generateKeyPair();
}
项目:jdk8u-jdk    文件:ParameterCache.java   
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
项目:jdk8u-jdk    文件:DHKeyPairGenerator.java   
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
项目:openjdk-jdk10    文件:SupportedGroupsExtension.java   
private static DHParameterSpec getFFDHEDHParameterSpec(
        NamedGroup namedGroup) {
    DHParameterSpec spec = null;
    switch (namedGroup) {
        case FFDHE_2048:
            spec = PredefinedDHParameterSpecs.ffdheParams.get(2048);
            break;
        case FFDHE_3072:
            spec = PredefinedDHParameterSpecs.ffdheParams.get(3072);
            break;
        case FFDHE_4096:
            spec = PredefinedDHParameterSpecs.ffdheParams.get(4096);
            break;
        case FFDHE_6144:
            spec = PredefinedDHParameterSpecs.ffdheParams.get(6144);
            break;
        case FFDHE_8192:
            spec = PredefinedDHParameterSpecs.ffdheParams.get(8192);
    }

    return spec;
}
项目:openjdk-jdk10    文件:SupportedGroupsExtension.java   
private static DHParameterSpec getPredefinedDHParameterSpec(
        NamedGroup namedGroup) {
    DHParameterSpec spec = null;
    switch (namedGroup) {
        case FFDHE_2048:
            spec = PredefinedDHParameterSpecs.definedParams.get(2048);
            break;
        case FFDHE_3072:
            spec = PredefinedDHParameterSpecs.definedParams.get(3072);
            break;
        case FFDHE_4096:
            spec = PredefinedDHParameterSpecs.definedParams.get(4096);
            break;
        case FFDHE_6144:
            spec = PredefinedDHParameterSpecs.definedParams.get(6144);
            break;
        case FFDHE_8192:
            spec = PredefinedDHParameterSpecs.definedParams.get(8192);
    }

    return spec;
}
项目:openjdk-jdk10    文件:ParameterCache.java   
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
项目:openjdk-jdk10    文件:DHKeyPairGenerator.java   
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param algParams the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
项目:openjdk-jdk10    文件:SupportedDHKeys.java   
@Override
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyPairGenerator", "DiffieHellman") == null) {
        System.out.println("No support of DH KeyPairGenerator, skipping");
        return;
    }

    for (SupportedKeySize keySize : SupportedKeySize.values()) {
        System.out.println("Checking " + keySize.primeSize + " ...");
        KeyPairGenerator kpg =
                KeyPairGenerator.getInstance("DiffieHellman", provider);
        kpg.initialize(keySize.primeSize);
        KeyPair kp = kpg.generateKeyPair();
        checkKeyPair(kp, keySize.primeSize, provider);

        DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
        BigInteger p = publicKey.getParams().getP();
        BigInteger g = publicKey.getParams().getG();
        kpg.initialize(new DHParameterSpec(p, g));
        kp = kpg.generateKeyPair();
        checkKeyPair(kp, keySize.primeSize, provider);
    }
}
项目:openjdk9    文件:ParameterCache.java   
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
项目:openjdk9    文件:DHKeyPairGenerator.java   
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param algParams the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
项目:scipio-erp    文件:ValueLinkApi.java   
/**
 * Get a public key object for the ValueLink supplied public key
 * @return PublicKey object of ValueLinks's public key
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public PublicKey getValueLinkPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    // read the valuelink public key
    String publicValue = (String) props.get("payment.valuelink.publicValue");
    byte[] publicKeyBytes = StringUtil.fromHexString(publicValue);

    // initialize the parameter spec
    DHParameterSpec dhParamSpec = this.getDHParameterSpec();

    // load the valuelink public key
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    BigInteger publicKeyInt = new BigInteger(publicKeyBytes);
    DHPublicKeySpec dhPublicSpec = new DHPublicKeySpec(publicKeyInt, dhParamSpec.getP(), dhParamSpec.getG());
    PublicKey vlPublic = keyFactory.generatePublic(dhPublicSpec);

    return vlPublic;
}
项目:privacylayer    文件:AESPlatform.java   
public static KeyPair generateECKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    /* The factors were self-generated [@CapacitorSet]. Source:
    import java.security.AlgorithmParameterGenerator;
    import java.security.AlgorithmParameters;
    import javax.crypto.spec.DHParameterSpec;
    public class Main {
      public static void main(String[] argv) throws Exception {
        AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
        paramGen.init(2048);
        AlgorithmParameters params = paramGen.generateParameters();
        DHParameterSpec dhSpec = params.getParameterSpec(DHParameterSpec.class);
        System.out.println(dhSpec.getG().toString(16));
        System.out.println(dhSpec.getP().toString(16));
      }
    } */
    BigInteger g = new BigInteger(
            "93445990947cef561f52de0fa07a232b07ba78c6d1b3a09d1b838de4d3c51f843c307427b963b2060fb30d8088e5bc8459cf4201987e5d83c2a9c2b72cee53f7905c92c6425f9f97df71b8c09ea97e8435c30b57d6e84bb134af3aeaacf4047da02716c0b85c1b403dba306569aaaa6fb7b01861c4f692af24ad89f02408762380dbdd7186e36d59edf9d2abd93bfe8f04e4e20a214df66dabd02d1b15e6b943ad73a5695110286d6e3b4d35f8f08ece05728645bfb85d29ec561d6db16ac4bb5f58805eea1298b29161f74bac3ff9003dabfcc5fdc7604fb7bfdbf96e9c6c8ca7b357a74a94f62752a780a451bed793400b56a1a9414fa38458ed797896ca8c",
            16);
    BigInteger p = new BigInteger(
            "ab0eab856a13bdc2c35ae735b04b6424f7c8d33beae9f7d28ff58f84a845e727a2cb3d3fcf716ff839e65fbeaa4f9b38eddd3b87c03b1bf4e5dd86f211a7845d67d2a44a64b5126776fc5a210196020e6552930fbb5f98f5f23589d51dee3fbdb9e714989ad966465ee56e3551b216f0e15c257c0aeddbc1e6b394341a4c07a5412e22cda2c052d232ea68c9709d4e1fe359780a9842f7b30130a7bea563c31897e95cc7cff834ac46aa4d56a1f75b5437dd444d7be4e33c069c340020250c713d6219c5b62d252ad348220254ff77cd6ba54cdd0f37ec6d6cc9bd22ea6794b6237f6fb056edfd7132d4a1be3ddc7cfe6fe57b974d5a9d67ac7059cab02b2a7b",
            16);
    final DHParameterSpec dhSpec = new DHParameterSpec(p, g, 511);
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
    keyGen.initialize(dhSpec);
    return keyGen.generateKeyPair();
}
项目:wycheproof    文件:DhiesTest.java   
public DHParameterSpec ike2048() {
  final BigInteger p =
      new BigInteger(
          "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
              + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
              + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
              + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
              + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
              + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
              + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
              + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
              + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
              + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
              + "15728E5A8AACAA68FFFFFFFFFFFFFFFF",
          16);
  final BigInteger g = new BigInteger("2");
  return new DHParameterSpec(p, g);
}
项目:wycheproof    文件:DhiesTest.java   
/**
 * WARNING: This test uses weak crypto (i.e. DHIESWithAES), if supported. Checks that key
 * agreement using DHIES works in the sense that it can decrypt what it encrypts. Unfortunately it
 * seems that there is no secure mode using AES.
 */
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testDhiesBasic() throws Exception {
  DHParameterSpec params = ike2048();
  KeyPairGenerator kf = KeyPairGenerator.getInstance("DH");
  kf.initialize(params);
  KeyPair keyPair = kf.generateKeyPair();
  PrivateKey priv = keyPair.getPrivate();
  PublicKey pub = keyPair.getPublic();
  byte[] message = "Hello".getBytes("UTF-8");
  Cipher dhies;
  try {
    dhies = Cipher.getInstance("DHIESwithAES");
  } catch (NoSuchAlgorithmException ex) {
    // The algorithm isn't supported - even better!
    return;
  }
  dhies.init(Cipher.ENCRYPT_MODE, pub);
  byte[] ciphertext = dhies.doFinal(message);
  System.out.println("testDhiesBasic:" + TestUtil.bytesToHex(ciphertext));
  dhies.init(Cipher.DECRYPT_MODE, priv);
  byte[] decrypted = dhies.doFinal(ciphertext);
  assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
项目:wycheproof    文件:DhTest.java   
public DHParameterSpec openJdk1024() {
  final BigInteger p =
      new BigInteger(
          "fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669"
              + "455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7"
              + "6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb"
              + "83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7",
          16);
  final BigInteger unusedQ = new BigInteger("9760508f15230bccb292b982a2eb840bf0581cf5", 16);
  final BigInteger g =
      new BigInteger(
          "f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267"
              + "5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1"
              + "3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b"
              + "cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a",
          16);
  return new DHParameterSpec(p, g);
}
项目:wycheproof    文件:DhTest.java   
/** Check that key agreement using DH works. */
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testDh() throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
  DHParameterSpec dhparams = ike2048();
  keyGen.initialize(dhparams);
  KeyPair keyPairA = keyGen.generateKeyPair();
  KeyPair keyPairB = keyGen.generateKeyPair();

  KeyAgreement kaA = KeyAgreement.getInstance("DH");
  KeyAgreement kaB = KeyAgreement.getInstance("DH");
  kaA.init(keyPairA.getPrivate());
  kaB.init(keyPairB.getPrivate());
  kaA.doPhase(keyPairB.getPublic(), true);
  kaB.doPhase(keyPairA.getPublic(), true);
  byte[] kAB = kaA.generateSecret();
  byte[] kBA = kaB.generateSecret();
  assertEquals(TestUtil.bytesToHex(kAB), TestUtil.bytesToHex(kBA));
}
项目:jdk8u_jdk    文件:ParameterCache.java   
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
项目:jdk8u_jdk    文件:DHKeyPairGenerator.java   
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
项目:lookaside_java-1.8.0-openjdk    文件:ParameterCache.java   
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
项目:lookaside_java-1.8.0-openjdk    文件:DHKeyPairGenerator.java   
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
项目:statelearner    文件:TLSTestService.java   
public void loadServerKey() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeySpecException {
        char[] password = KEYSTORE_PASSWORD.toCharArray();

        FileInputStream fIn = new FileInputStream(KEYSTORE_FILENAME);
        KeyStore keystore = KeyStore.getInstance("JKS");

        keystore.load(fIn, password);
        serverCertificate = (X509Certificate) keystore.getCertificate("server");
        serverPrivateKey  = (PrivateKey) keystore.getKey("server", password);

        // Generate DH keys for this session
        // Use hardcoded DH parameters
        DHParameterSpec dhParams = new DHParameterSpec(new BigInteger(new byte[] {(byte)0x00, (byte)0xad, (byte)0x77, (byte)0xcd, (byte)0xb7, (byte)0x14, (byte)0x6f, (byte)0xfe, (byte)0x08, (byte)0x1a, (byte)0xee, (byte)0xd2, (byte)0x2c, (byte)0x18, (byte)0x29, (byte)0x62, (byte)0x5a, (byte)0xff, (byte)0x03, (byte)0x5d, (byte)0xde, (byte)0xba, (byte)0x0d, (byte)0xd4, (byte)0x36, (byte)0x15, (byte)0x03, (byte)0x11, (byte)0x21, (byte)0x48, (byte)0xd9, (byte)0x77, (byte)0xfb, (byte)0x67, (byte)0xb0, (byte)0x74, (byte)0x2e, (byte)0x68, (byte)0xed, (byte)0x5a, (byte)0x3f, (byte)0x8a, (byte)0x3e, (byte)0xdb, (byte)0x81, (byte)0xa3, (byte)0x3b, (byte)0xaf, (byte)0x26, (byte)0xe4, (byte)0x54, (byte)0x00, (byte)0x85, (byte)0x0d, (byte)0xfd, (byte)0x23, (byte)0x21, (byte)0xc1, (byte)0xfe, (byte)0x69, (byte)0xe4, (byte)0xf3, (byte)0x57, (byte)0xe6, (byte)0x0a, (byte)0x7c, (byte)0x62, (byte)0xc0, (byte)0xd6, (byte)0x40, (byte)0x3e, (byte)0x94, (byte)0x9e, (byte)0x49, (byte)0x72, (byte)0x5a, (byte)0x21, (byte)0x53, (byte)0xb0, (byte)0x83, (byte)0x05, (byte)0x81, (byte)0x5a, (byte)0xde, (byte)0x17, (byte)0x31, (byte)0xbf, (byte)0xa8, (byte)0xa9, (byte)0xe5, (byte)0x28, (byte)0x1a, (byte)0xfc, (byte)0x06, (byte)0x1e, (byte)0x49, (byte)0xfe, (byte)0xdc, (byte)0x08, (byte)0xe3, (byte)0x29, (byte)0xfe, (byte)0x5b, (byte)0x88, (byte)0x66, (byte)0x39, (byte)0xa8, (byte)0x69, (byte)0x62, (byte)0x88, (byte)0x47, (byte)0x36, (byte)0xf5, (byte)0xdd, (byte)0x92, (byte)0x8f, (byte)0xca, (byte)0x32, (byte)0x4b, (byte)0x87, (byte)0xad, (byte)0xbf, (byte)0xab, (byte)0x4a, (byte)0x9d, (byte)0xd5, (byte)0xb8, (byte)0x2c, (byte)0xc4, (byte)0x43, (byte)0xb2, (byte)0x21, (byte)0xb4, (byte)0x2a, (byte)0x9b, (byte)0x42, (byte)0x17, (byte)0x6d, (byte)0xb6, (byte)0x86, (byte)0x42, (byte)0x41, (byte)0xb1, (byte)0xc7, (byte)0x37, (byte)0x37, (byte)0x95, (byte)0x6d, (byte)0x62, (byte)0xca, (byte)0xa6, (byte)0x57, (byte)0x33, (byte)0x88, (byte)0xe2, (byte)0x31, (byte)0xfe, (byte)0xd1, (byte)0x51, (byte)0xe7, (byte)0x73, (byte)0xae, (byte)0x3c, (byte)0xa7, (byte)0x4b, (byte)0xbc, (byte)0x8a, (byte)0x3d, (byte)0xc5, (byte)0x9a, (byte)0x28, (byte)0x9a, (byte)0xf9, (byte)0x57, (byte)0xb6, (byte)0xec, (byte)0xf6, (byte)0x75, (byte)0xaa, (byte)0x56, (byte)0xc1, (byte)0x42, (byte)0x9f, (byte)0x6a, (byte)0x7c, (byte)0x91, (byte)0x8b, (byte)0x5e, (byte)0xea, (byte)0x54, (byte)0x32, (byte)0x90, (byte)0x8a, (byte)0x9d, (byte)0x76, (byte)0x2a, (byte)0x29, (byte)0x1b, (byte)0x84, (byte)0x35, (byte)0xe6, (byte)0x21, (byte)0x07, (byte)0xb2, (byte)0xcb, (byte)0x5c, (byte)0xf9, (byte)0x5b, (byte)0xe9, (byte)0x5e, (byte)0x1b, (byte)0x80, (byte)0xd5, (byte)0x53, (byte)0xd7, (byte)0xa4, (byte)0x26, (byte)0x58, (byte)0xe4, (byte)0xe9, (byte)0x3f, (byte)0xfd, (byte)0xeb, (byte)0x78, (byte)0xf2, (byte)0x25, (byte)0x02, (byte)0x42, (byte)0xf8, (byte)0x50, (byte)0x13, (byte)0xbb, (byte)0x01, (byte)0x39, (byte)0xf3, (byte)0xcf, (byte)0x5c, (byte)0x51, (byte)0xdf, (byte)0xed, (byte)0xc5, (byte)0xfa, (byte)0xd8, (byte)0x4f, (byte)0xae, (byte)0x76, (byte)0xe8, (byte)0x30, (byte)0xfc, (byte)0x85, (byte)0xaa, (byte)0x8c, (byte)0x91, (byte)0x02, (byte)0x2b, (byte)0x61, (byte)0x87
}), new BigInteger(new byte[] { 0x05 }));

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DiffieHellman");
        keyPairGenerator.initialize(dhParams);

        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        dhPubKey = (DHPublicKey)keyPair.getPublic();
        dhPrivateKey = (DHPrivateKey)keyPair.getPrivate();
    }
项目:statelearner    文件:TLSTestService.java   
public void loadClientKey() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, InvalidAlgorithmParameterException {
        char[] password = KEYSTORE_PASSWORD.toCharArray();

        FileInputStream fIn = new FileInputStream(KEYSTORE_FILENAME);
        KeyStore keystore = KeyStore.getInstance("JKS");

        keystore.load(fIn, password);
        clientCertificate = (X509Certificate) keystore.getCertificate("client");
        clientPrivateKey  = (PrivateKey) keystore.getKey("client", password);

        // Generate DH keys for this session
        // Use hardcoded DH parameters
        DHParameterSpec dhParams = new DHParameterSpec(new BigInteger(new byte[] {(byte)0x00, (byte)0xad, (byte)0x77, (byte)0xcd, (byte)0xb7, (byte)0x14, (byte)0x6f, (byte)0xfe, (byte)0x08, (byte)0x1a, (byte)0xee, (byte)0xd2, (byte)0x2c, (byte)0x18, (byte)0x29, (byte)0x62, (byte)0x5a, (byte)0xff, (byte)0x03, (byte)0x5d, (byte)0xde, (byte)0xba, (byte)0x0d, (byte)0xd4, (byte)0x36, (byte)0x15, (byte)0x03, (byte)0x11, (byte)0x21, (byte)0x48, (byte)0xd9, (byte)0x77, (byte)0xfb, (byte)0x67, (byte)0xb0, (byte)0x74, (byte)0x2e, (byte)0x68, (byte)0xed, (byte)0x5a, (byte)0x3f, (byte)0x8a, (byte)0x3e, (byte)0xdb, (byte)0x81, (byte)0xa3, (byte)0x3b, (byte)0xaf, (byte)0x26, (byte)0xe4, (byte)0x54, (byte)0x00, (byte)0x85, (byte)0x0d, (byte)0xfd, (byte)0x23, (byte)0x21, (byte)0xc1, (byte)0xfe, (byte)0x69, (byte)0xe4, (byte)0xf3, (byte)0x57, (byte)0xe6, (byte)0x0a, (byte)0x7c, (byte)0x62, (byte)0xc0, (byte)0xd6, (byte)0x40, (byte)0x3e, (byte)0x94, (byte)0x9e, (byte)0x49, (byte)0x72, (byte)0x5a, (byte)0x21, (byte)0x53, (byte)0xb0, (byte)0x83, (byte)0x05, (byte)0x81, (byte)0x5a, (byte)0xde, (byte)0x17, (byte)0x31, (byte)0xbf, (byte)0xa8, (byte)0xa9, (byte)0xe5, (byte)0x28, (byte)0x1a, (byte)0xfc, (byte)0x06, (byte)0x1e, (byte)0x49, (byte)0xfe, (byte)0xdc, (byte)0x08, (byte)0xe3, (byte)0x29, (byte)0xfe, (byte)0x5b, (byte)0x88, (byte)0x66, (byte)0x39, (byte)0xa8, (byte)0x69, (byte)0x62, (byte)0x88, (byte)0x47, (byte)0x36, (byte)0xf5, (byte)0xdd, (byte)0x92, (byte)0x8f, (byte)0xca, (byte)0x32, (byte)0x4b, (byte)0x87, (byte)0xad, (byte)0xbf, (byte)0xab, (byte)0x4a, (byte)0x9d, (byte)0xd5, (byte)0xb8, (byte)0x2c, (byte)0xc4, (byte)0x43, (byte)0xb2, (byte)0x21, (byte)0xb4, (byte)0x2a, (byte)0x9b, (byte)0x42, (byte)0x17, (byte)0x6d, (byte)0xb6, (byte)0x86, (byte)0x42, (byte)0x41, (byte)0xb1, (byte)0xc7, (byte)0x37, (byte)0x37, (byte)0x95, (byte)0x6d, (byte)0x62, (byte)0xca, (byte)0xa6, (byte)0x57, (byte)0x33, (byte)0x88, (byte)0xe2, (byte)0x31, (byte)0xfe, (byte)0xd1, (byte)0x51, (byte)0xe7, (byte)0x73, (byte)0xae, (byte)0x3c, (byte)0xa7, (byte)0x4b, (byte)0xbc, (byte)0x8a, (byte)0x3d, (byte)0xc5, (byte)0x9a, (byte)0x28, (byte)0x9a, (byte)0xf9, (byte)0x57, (byte)0xb6, (byte)0xec, (byte)0xf6, (byte)0x75, (byte)0xaa, (byte)0x56, (byte)0xc1, (byte)0x42, (byte)0x9f, (byte)0x6a, (byte)0x7c, (byte)0x91, (byte)0x8b, (byte)0x5e, (byte)0xea, (byte)0x54, (byte)0x32, (byte)0x90, (byte)0x8a, (byte)0x9d, (byte)0x76, (byte)0x2a, (byte)0x29, (byte)0x1b, (byte)0x84, (byte)0x35, (byte)0xe6, (byte)0x21, (byte)0x07, (byte)0xb2, (byte)0xcb, (byte)0x5c, (byte)0xf9, (byte)0x5b, (byte)0xe9, (byte)0x5e, (byte)0x1b, (byte)0x80, (byte)0xd5, (byte)0x53, (byte)0xd7, (byte)0xa4, (byte)0x26, (byte)0x58, (byte)0xe4, (byte)0xe9, (byte)0x3f, (byte)0xfd, (byte)0xeb, (byte)0x78, (byte)0xf2, (byte)0x25, (byte)0x02, (byte)0x42, (byte)0xf8, (byte)0x50, (byte)0x13, (byte)0xbb, (byte)0x01, (byte)0x39, (byte)0xf3, (byte)0xcf, (byte)0x5c, (byte)0x51, (byte)0xdf, (byte)0xed, (byte)0xc5, (byte)0xfa, (byte)0xd8, (byte)0x4f, (byte)0xae, (byte)0x76, (byte)0xe8, (byte)0x30, (byte)0xfc, (byte)0x85, (byte)0xaa, (byte)0x8c, (byte)0x91, (byte)0x02, (byte)0x2b, (byte)0x61, (byte)0x87
}), new BigInteger(new byte[] { 0x05 }));

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DiffieHellman");
        keyPairGenerator.initialize(dhParams);

        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        dhPubKey = (DHPublicKey)keyPair.getPublic();
        dhPrivateKey = (DHPrivateKey)keyPair.getPrivate();
    }
项目:Aki-SSL    文件:KeyPairGeneratorSpi.java   
public void initialize(
    AlgorithmParameterSpec params,
    SecureRandom random)
    throws InvalidAlgorithmParameterException
{
    if (!(params instanceof DHParameterSpec))
    {
        throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
    }
    DHParameterSpec dhParams = (DHParameterSpec)params;

    param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));

    engine.init(param);
    initialised = true;
}
项目:Aki-SSL    文件:AlgorithmParametersSpi.java   
protected void engineInit(
    AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException
{
    if (!(paramSpec instanceof ElGamalParameterSpec) && !(paramSpec instanceof DHParameterSpec))
    {
        throw new InvalidParameterSpecException("DHParameterSpec required to initialise a ElGamal algorithm parameters object");
    }

    if (paramSpec instanceof ElGamalParameterSpec)
    {
        this.currentSpec = (ElGamalParameterSpec)paramSpec;
    }
    else
    {
        DHParameterSpec s = (DHParameterSpec)paramSpec;

        this.currentSpec = new ElGamalParameterSpec(s.getP(), s.getG());
    }
}
项目:javify    文件:DHKeyPairGeneratorSpi.java   
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
    throws InvalidAlgorithmParameterException
{
  HashMap attributes = new HashMap();
  if (params != null)
    {
      if (! (params instanceof DHGenParameterSpec) &&
          ! (params instanceof DHParameterSpec))
        throw new InvalidAlgorithmParameterException("params");

      attributes.put(GnuDHKeyPairGenerator.DH_PARAMETERS, params);
    }

  if (random != null)
    attributes.put(GnuDHKeyPairGenerator.SOURCE_OF_RANDOMNESS, random);

  attributes.put(GnuDHKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
                 Integer.valueOf(Registry.ASN1_ENCODING_ID));
  adaptee.setup(attributes);
}
项目:ipack    文件:BouncyCastleProviderConfiguration.java   
public DHParameterSpec getDHDefaultParameters(int keySize)
{
    Object params = dhThreadSpec.get();
    if (params == null)
    {
        params = dhDefaultParams;
    }

    if (params instanceof DHParameterSpec)
    {
        DHParameterSpec spec = (DHParameterSpec)params;

        if (spec.getP().bitLength() == keySize)
        {
            return spec;
        }
    }
    else if (params instanceof DHParameterSpec[])
    {
        DHParameterSpec[] specs = (DHParameterSpec[])params;

        for (int i = 0; i != specs.length; i++)
        {
            if (specs[i].getP().bitLength() == keySize)
            {
                return specs[i];
            }
        }
    }

    return null;
}
项目:ipack    文件:JCEDHPublicKey.java   
JCEDHPublicKey(
    BigInteger        y,
    DHParameterSpec   dhSpec)
{
    this.y = y;
    this.dhSpec = dhSpec;
}
项目:ipack    文件:JCEDHPublicKey.java   
private void readObject(
    ObjectInputStream   in)
    throws IOException, ClassNotFoundException
{
    this.y = (BigInteger)in.readObject();
    this.dhSpec = new DHParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject(), in.readInt());
}
项目:ipack    文件:JCEDHPrivateKey.java   
private void readObject(
    ObjectInputStream   in)
    throws IOException, ClassNotFoundException
{
    x = (BigInteger)in.readObject();

    this.dhSpec = new DHParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject(), in.readInt());
}
项目:ipack    文件:BCDHPublicKey.java   
BCDHPublicKey(
    BigInteger y,
    DHParameterSpec dhSpec)
{
    this.y = y;
    this.dhSpec = dhSpec;
}
项目:ipack    文件:BCDHPublicKey.java   
private void readObject(
    ObjectInputStream   in)
    throws IOException, ClassNotFoundException
{
    in.defaultReadObject();

    this.dhSpec = new DHParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject(), in.readInt());
    this.info = null;
}
项目:ipack    文件:AlgorithmParametersSpi.java   
protected AlgorithmParameterSpec localEngineGetParameterSpec(
    Class paramSpec) 
    throws InvalidParameterSpecException
{
    if (paramSpec == DHParameterSpec.class)
    {
        return currentSpec;
    }

    throw new InvalidParameterSpecException("unknown parameter spec passed to DH parameters object.");
}
项目:ipack    文件:AlgorithmParametersSpi.java   
protected void engineInit(
    AlgorithmParameterSpec paramSpec) 
    throws InvalidParameterSpecException
{
    if (!(paramSpec instanceof DHParameterSpec))
    {
        throw new InvalidParameterSpecException("DHParameterSpec required to initialise a Diffie-Hellman algorithm parameters object");
    }

    this.currentSpec = (DHParameterSpec)paramSpec;
}
项目:ipack    文件:KeyAgreementSpi.java   
protected void engineInit(
    Key                     key,
    AlgorithmParameterSpec  params,
    SecureRandom            random) 
    throws InvalidKeyException, InvalidAlgorithmParameterException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey    privKey = (DHPrivateKey)key;

    if (params != null)
    {
        if (!(params instanceof DHParameterSpec))
        {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
        DHParameterSpec p = (DHParameterSpec)params;

        this.p = p.getP();
        this.g = p.getG();
    }
    else
    {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }

    this.x = this.result = privKey.getX();
}
项目:ipack    文件:AlgorithmParameterGeneratorSpi.java   
protected AlgorithmParameters engineGenerateParameters()
{
    DHParametersGenerator pGen = new DHParametersGenerator();

    if (random != null)
    {
        pGen.init(strength, 20, random);
    }
    else
    {
        pGen.init(strength, 20, new SecureRandom());
    }

    DHParameters p = pGen.generateParameters();

    AlgorithmParameters params;

    try
    {
        params = AlgorithmParameters.getInstance("DH", BouncyCastleProvider.PROVIDER_NAME);
        params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}