Search in sources :

Example 46 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 47 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 48 with KeyAgreement

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

the class ECDHKeyAgreementTest method testGenerateSecret_withoutBuffer.

void testGenerateSecret_withoutBuffer(Provider provider) throws Exception {
    KeyAgreement keyAgreement = getKeyAgreement(provider);
    keyAgreement.init(KAT_PRIVATE_KEY2);
    keyAgreement.doPhase(KAT_PUBLIC_KEY1, true);
    byte[] secret = keyAgreement.generateSecret();
    assertTrue(Arrays.equals(KAT_SECRET, secret));
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 49 with KeyAgreement

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

the class ECDHKeyAgreementTest method testGenerateSecret_withNullOutputBuffer.

void testGenerateSecret_withNullOutputBuffer(Provider provider) throws Exception {
    KeyAgreement keyAgreement = getKeyAgreement(provider);
    keyAgreement.init(KAT_PRIVATE_KEY1);
    keyAgreement.doPhase(KAT_PUBLIC_KEY2, true);
    try {
        keyAgreement.generateSecret(null, 0);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 50 with KeyAgreement

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

the class ECDHKeyAgreementTest method testDoPhase_withPhaseWhichIsNotLast.

void testDoPhase_withPhaseWhichIsNotLast(Provider provider) throws Exception {
    KeyAgreement keyAgreement = getKeyAgreement(provider);
    keyAgreement.init(KAT_PRIVATE_KEY1);
    try {
        keyAgreement.doPhase(KAT_PUBLIC_KEY2, false);
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Aggregations

KeyAgreement (javax.crypto.KeyAgreement)56 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