Search in sources :

Example 51 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method test_getAlgorithm.

public void test_getAlgorithm() throws NoSuchAlgorithmException {
    Mock_KeyAgreement mka = new Mock_KeyAgreement(null, null, null);
    assertNull(mka.getAlgorithm());
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(), validValues[i]);
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 52 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method testInit01.

/**
     * Test for the methods <code>init(Key key)</code>
     * <code>init(Key key, SecureRandom random)</code>
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
     * Assertion: throws InvalidKeyException when key is inappropriate
     */
public void testInit01() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    SecureRandom random = null;
    AlgorithmParameterSpec aps = null;
    DHParameterSpec dhPs = new DHParameterSpec(new BigInteger("56"), new BigInteger("56"));
    for (int i = 0; i < kAgs.length; i++) {
        try {
            kAgs[i].init(publKey);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, new SecureRandom());
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, random);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, dhPs);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, aps);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, dhPs, new SecureRandom());
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
    }
}
Also used : SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 53 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method testDoPhase.

/**
     * Test for <code>doPhase(Key key, boolean lastPhase)</code> method
     * Assertion: throws InvalidKeyException if key is not appropriate
     */
public void testDoPhase() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
    SecureRandom randomNull = null;
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kAgs.length; i++) {
        try {
            kAgs[i].doPhase(publKey, true);
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        //expected
        }
        kAgs[i].init(privKey);
        try {
            kAgs[i].doPhase(privKey, false);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].doPhase(privKey, true);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        kAgs[i].init(privKey, dhPs);
        kAgs[i].doPhase(publKey, true);
        kAgs[i].init(privKey, dhPs, random);
        kAgs[i].doPhase(publKey, true);
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

Example 54 with KeyAgreement

use of javax.crypto.KeyAgreement in project Gradle-demo by Arisono.

the class DHUtil method getSecretKey.

/**
	 * 根据对方的公钥和自己的私钥生成本地密钥
	 */
public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
    //实例化密钥工厂
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    //将公钥从字节数组转换为publicKey
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
    PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
    //将私钥从字节数组转换为privateKey
    PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(privateKey);
    PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
    //准备根据以上公钥和私钥生成本地密钥SecretKey
    //先实例化KeyAgreement
    KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
    //用自己的私钥初始化keyAgreement
    keyAgreement.init(priKey);
    //结合对方的公钥进行运算
    keyAgreement.doPhase(pubKey, true);
    //开始生成本地密钥secretKey   密钥算法为对称密码算法
    //DES、3DES、AES
    SecretKey secretKey = keyAgreement.generateSecret("DES");
    return secretKey.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyAgreement(javax.crypto.KeyAgreement) KeyFactory(java.security.KeyFactory)

Example 55 with KeyAgreement

use of javax.crypto.KeyAgreement in project jdk8u_jdk by JetBrains.

the class ECDHCrypt method getAgreedSecret.

// called by ClientHandshaker with either the server's static or
// ephemeral public key
SecretKey getAgreedSecret(PublicKey peerPublicKey) throws SSLHandshakeException {
    try {
        KeyAgreement ka = JsseJce.getKeyAgreement("ECDH");
        ka.init(privateKey);
        ka.doPhase(peerPublicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException("Could not generate secret").initCause(e);
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Aggregations

KeyAgreement (javax.crypto.KeyAgreement)55 KeyPairGenerator (java.security.KeyPairGenerator)15 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 InvalidKeyException (java.security.InvalidKeyException)12 KeyFactory (java.security.KeyFactory)12 SecretKey (javax.crypto.SecretKey)10 DHParameterSpec (javax.crypto.spec.DHParameterSpec)10 KeyPair (java.security.KeyPair)9 Cipher (javax.crypto.Cipher)9 IOException (java.io.IOException)7 PublicKey (java.security.PublicKey)7 BigInteger (java.math.BigInteger)6 SecureRandom (java.security.SecureRandom)6 DHPublicKey (javax.crypto.interfaces.DHPublicKey)6 SecretKeySpec (javax.crypto.spec.SecretKeySpec)6 GeneralSecurityException (java.security.GeneralSecurityException)5 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)5 IvParameterSpec (javax.crypto.spec.IvParameterSpec)5 PrivateKey (java.security.PrivateKey)4 CertificateException (java.security.cert.CertificateException)4