Search in sources :

Example 31 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.

the class KeyAgreementThread method test.

@Override
public void test() throws Exception {
    AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH");
    apg.init(1024, new SecureRandom());
    AlgorithmParameters ap = apg.generateParameters();
    DHParameterSpec ps = ap.getParameterSpec(DHParameterSpec.class);
    KeyAgreementGen kag1 = new KeyAgreementGen(ps);
    KeyAgreementGen kag2 = new KeyAgreementGen(ps);
    byte[] bArray1 = kag1.getPublicKeyBytes();
    byte[] bArray2 = kag2.getPublicKeyBytes();
    byte[] sk1 = kag1.getSecretKey(algName, bArray2);
    byte[] sk2 = kag2.getSecretKey(algName, bArray1);
    if (Arrays.areEqual(sk1, sk2) == false) {
        throw new Exception("Generated keys are not the same");
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 32 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec 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 33 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec 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 34 with DHParameterSpec

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

the class DHUtil method initKey.

/**
	 * 乙方根据甲方公钥初始化并返回密钥对
	 */
public static Map<String, Object> initKey(byte[] key) throws Exception {
    //将甲方公钥从字节数组转换为publicKey
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
    //实例化密钥工厂
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    //产生甲方公钥pubKey
    DHPublicKey dhPublicKey = (DHPublicKey) keyFactory.generatePublic(keySpec);
    //剖析甲方公钥,得到其参数
    DHParameterSpec dhParameterSpec = dhPublicKey.getParams();
    //实例化密钥对生成器
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
    //用甲方公钥初始化密钥对生成器
    keyPairGenerator.initialize(dhParameterSpec);
    //产生密钥对
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    //得到乙方公钥
    DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();
    //得到乙方私钥
    DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
    //将公钥和私钥封装到Map中,方便以后使用
    Map<String, Object> keyMap = new HashMap<String, Object>();
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) DHPublicKey(javax.crypto.interfaces.DHPublicKey) HashMap(java.util.HashMap) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 35 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.

the class KeyPairGeneratorTest method getDHParams.

/**
     * DH parameters pre-generated so that the test doesn't take too long.
     * These parameters were generated with:
     *
     * openssl gendh 512 | openssl dhparams -C
     */
private static AlgorithmParameterSpec getDHParams() {
    BigInteger p = new BigInteger("E7AB1768BD75CD24700960FFA32D3F1557344E587101237532CC641646ED7A7C104743377F6D46251698B665CE2A6CBAB6714C2569A7D2CA22C0CF03FA40AC93", 16);
    BigInteger g = new BigInteger("02", 16);
    return new DHParameterSpec(p, g, 512);
}
Also used : BigInteger(java.math.BigInteger) DHParameterSpec(javax.crypto.spec.DHParameterSpec)

Aggregations

DHParameterSpec (javax.crypto.spec.DHParameterSpec)44 BigInteger (java.math.BigInteger)18 KeyPair (java.security.KeyPair)13 KeyPairGenerator (java.security.KeyPairGenerator)13 KeyAgreement (javax.crypto.KeyAgreement)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 SecureRandom (java.security.SecureRandom)7 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)6 InvalidKeyException (java.security.InvalidKeyException)6 KeyFactory (java.security.KeyFactory)6 PublicKey (java.security.PublicKey)6 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)6 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)6 DHPublicKey (javax.crypto.interfaces.DHPublicKey)5 IOException (java.io.IOException)4 PrivateKey (java.security.PrivateKey)4 CertificateException (java.security.cert.CertificateException)4 X509Certificate (java.security.cert.X509Certificate)4 X509ExtendedKeyManager (javax.net.ssl.X509ExtendedKeyManager)4 X509KeyManager (javax.net.ssl.X509KeyManager)4