Search in sources :

Example 21 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class OldKeyPairGeneratorTestDH method testKeyPairGenerator.

@BrokenTest("Takes ages due to DH computations. Disabling for now.")
public void testKeyPairGenerator() throws NoSuchAlgorithmException {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("DH");
    generator.initialize(1024);
    KeyPair keyPair = generator.generateKeyPair();
    assertNotNull("no keypair generated", keyPair);
    assertNotNull("no public key generated", keyPair.getPublic());
    assertNotNull("no private key generated", keyPair.getPrivate());
    new KeyAgreementHelper("DH").test(keyPair);
}
Also used : KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator) KeyAgreementHelper(tests.security.KeyAgreementHelper) BrokenTest(dalvik.annotation.BrokenTest)

Example 22 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class KeyFactory2Test method test_getKeySpecLjava_security_KeyLjava_lang_Class.

public void test_getKeySpecLjava_security_KeyLjava_lang_Class() throws Exception {
    // java.lang.Class)
    for (int i = 0; i < keyfactAlgs.length; i++) {
        KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
        // We don't use getInstance
        SecureRandom random = new SecureRandom();
        keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
        KeyPair keys = keyGen.generateKeyPair();
        if (keepalive != null) {
            keepalive.interrupt();
        }
        KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
        KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
        PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
        PublicKey publicKey = fact.generatePublic(publicKeySpec);
        assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
        assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
        KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(), X509EncodedKeySpec.class);
        assertTrue("improper key spec for encoded public key", encodedSpec.getClass().equals(X509EncodedKeySpec.class));
        encodedSpec = fact.getKeySpec(keys.getPrivate(), PKCS8EncodedKeySpec.class);
        assertTrue("improper key spec for encoded private key", encodedSpec.getClass().equals(PKCS8EncodedKeySpec.class));
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) SecureRandom(java.security.SecureRandom) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 23 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class KeyPairGenerator1Test method testKeyPairGenerator09.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code>
     * method
     * Assertion: returns KeyPairGenerator object
     */
public void testKeyPairGenerator09() throws NoSuchAlgorithmException, IllegalArgumentException {
    if (!DSASupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyPairGenerator kpg;
    for (int i = 0; i < algs.length; i++) {
        kpg = KeyPairGenerator.getInstance(algs[i], validProvider);
        assertEquals("Incorrect algorithm", kpg.getAlgorithm().toUpperCase(), algs[i].toUpperCase());
        assertEquals("Incorrect provider", kpg.getProvider(), validProvider);
    }
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator)

Example 24 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class KeyPairGenerator1Test method testKeyPairGenerator13.

/**
     * Test for methods: <code>initialize(int keysize)</code>
     * <code>initialize(int keysize, SecureRandom random)</code>
     * <code>initialize(AlgorithmParameterSpec param)</code>
     * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
     * <code>generateKeyPair()</code>
     * <code>genKeyPair()</code>
     * Assertion: initialize(int ...) throws InvalidParameterException when
     * keysize in incorrect Assertion: initialize(AlgorithmParameterSpec
     * ...)throws UnsupportedOperationException Assertion: generateKeyPair() and
     * genKeyPair() return not null KeyPair Additional class MyKeyPairGenerator2
     * is used
     */
public void testKeyPairGenerator13() {
    int[] keys = { -1, -250, 1, 63, -512, -1024 };
    SecureRandom random = new SecureRandom();
    KeyPairGenerator mKPG = new MyKeyPairGenerator2(null);
    assertEquals("Algorithm must be null", mKPG.getAlgorithm(), MyKeyPairGenerator2.getResAlgorithm());
    assertNull("genKeyPair() must return null", mKPG.genKeyPair());
    assertNull("generateKeyPair() mut return null", mKPG.generateKeyPair());
    for (int i = 0; i < keys.length; i++) {
        try {
            mKPG.initialize(keys[i]);
            fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
        } catch (InvalidParameterException e) {
        }
        try {
            mKPG.initialize(keys[i], random);
            fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
        } catch (InvalidParameterException e) {
        }
    }
    try {
        mKPG.initialize(64);
    } catch (InvalidParameterException e) {
        fail("Unexpected InvalidParameterException was thrown");
    }
    try {
        mKPG.initialize(64, null);
    } catch (InvalidParameterException e) {
        fail("Unexpected InvalidParameterException was thrown");
    }
    try {
        mKPG.initialize(null, random);
    } catch (UnsupportedOperationException e) {
    // on j2se1.4 this exception is not thrown
    } catch (InvalidAlgorithmParameterException e) {
        fail("Unexpected InvalidAlgorithmParameterException was thrown");
    }
}
Also used : MyKeyPairGenerator2(org.apache.harmony.security.tests.support.MyKeyPairGenerator2) InvalidParameterException(java.security.InvalidParameterException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator)

Example 25 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class KeyPairGenerator1Test method testKeyPairGenerator11.

/**
     * Test for methods:
     * <code>initialize(int keysize)</code>
     * <code>initialize(int keysize, SecureRandom random)</code>
     * <code>initialize(AlgorithmParameterSpec param)</code>
     * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
     * Assertion: throws InvalidParameterException or
     * InvalidAlgorithmParameterException when parameters keysize or param are
     * incorrect
     */
public void testKeyPairGenerator11() throws NoSuchAlgorithmException, NoSuchProviderException {
    if (!DSASupported) {
        fail(NotSupportMsg);
        return;
    }
    int[] keys = { -10000, -1024, -1, 0, 10000 };
    KeyPairGenerator[] kpg = createKPGen();
    assertNotNull("KeyPairGenerator objects were not created", kpg);
    SecureRandom random = new SecureRandom();
    AlgorithmParameterSpec aps = null;
    for (int i = 0; i < kpg.length; i++) {
        for (int j = 0; j < keys.length; j++) {
            try {
                kpg[i].initialize(keys[j]);
                kpg[i].initialize(keys[j], random);
            } catch (InvalidParameterException e) {
            }
        }
        try {
            kpg[i].initialize(aps);
            kpg[i].initialize(aps, random);
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

KeyPairGenerator (java.security.KeyPairGenerator)197 KeyPair (java.security.KeyPair)145 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)43 SecureRandom (java.security.SecureRandom)39 PublicKey (java.security.PublicKey)27 PrivateKey (java.security.PrivateKey)26 X509Certificate (java.security.cert.X509Certificate)23 KeyFactory (java.security.KeyFactory)21 IOException (java.io.IOException)19 BigInteger (java.math.BigInteger)17 GeneralSecurityException (java.security.GeneralSecurityException)15 Signature (java.security.Signature)15 Date (java.util.Date)15 Cipher (javax.crypto.Cipher)15 KeyAgreement (javax.crypto.KeyAgreement)15 RSAPublicKey (java.security.interfaces.RSAPublicKey)14 X500Principal (javax.security.auth.x500.X500Principal)13 ECPrivateKey (java.security.interfaces.ECPrivateKey)12 ECPublicKey (java.security.interfaces.ECPublicKey)12 HashMap (java.util.HashMap)11