Search in sources :

Example 26 with KeyPairGenerator

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

the class KeyPairGenerator2Test method GetInstance03.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code>
     * method
     * Assertions:
     * throws NullPointerException  when algorithm is null
     * throws NoSuchAlgorithmException when algorithm is incorrect;
     * throws IllegalArgumentException when provider is null;
     * returns KeyPairGenerator object
     */
private void GetInstance03(int mode) throws NoSuchAlgorithmException, IllegalArgumentException, InvalidAlgorithmParameterException {
    try {
        KeyPairGenerator.getInstance(null, mProv);
        fail("NullPointerException or KeyStoreException must be thrown");
    } catch (NoSuchAlgorithmException e) {
    } catch (NullPointerException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            KeyPairGenerator.getInstance(invalidValues[i], mProv);
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    Provider prov = null;
    for (int i = 0; i < validValues.length; i++) {
        String alg = validValues[i].concat(post);
        try {
            KeyPairGenerator.getInstance(alg, prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(alg).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    KeyPairGenerator kpG;
    for (int i = 0; i < validValues.length; i++) {
        String alg = validValues[i].concat(post);
        kpG = KeyPairGenerator.getInstance(alg, mProv);
        assertEquals("Incorrect algorithm", kpG.getAlgorithm().toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase());
        assertEquals("Incorrect provider", kpG.getProvider(), mProv);
        checkResult(kpG, mode);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyPairGenerator(java.security.KeyPairGenerator) Provider(java.security.Provider)

Example 27 with KeyPairGenerator

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

the class KeyPairGenerator3Test method testGenKeyPair02.

/**
     * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
     * methods
     * Assertion: these methods are used without previously initialization
     */
public void testGenKeyPair02() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException {
    if (!DSASupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyPairGenerator[] kpg = createKPGen();
    assertNotNull("KeyPairGenerator objects were not created", kpg);
    KeyPair kp, kp1;
    for (int i = 0; i < kpg.length; i++) {
        kp = kpg[i].generateKeyPair();
        kp1 = kpg[i].genKeyPair();
        assertFalse("Incorrect private key", kp.getPrivate().equals(kp1.getPrivate()));
        assertFalse("Incorrect public key", kp.getPublic().equals(kp1.getPublic()));
    }
}
Also used : KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator)

Example 28 with KeyPairGenerator

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

the class KeyPairGenerator3Test method testGenKeyPair01.

/**
     * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
     * methods
     * Assertion: KeyPairGenerator was initialized before the invocation
     * of these methods
     */
public void testGenKeyPair01() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException {
    if (!DSASupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyPairGenerator[] kpg = createKPGen();
    assertNotNull("KeyPairGenerator objects were not created", kpg);
    KeyPair kp, kp1;
    SecureRandom rr = new SecureRandom();
    for (int i = 0; i < kpg.length; i++) {
        kpg[i].initialize(512, rr);
        kp = kpg[i].generateKeyPair();
        kp1 = kpg[i].genKeyPair();
        assertFalse("Incorrect private key", kp.getPrivate().equals(kp1.getPrivate()));
        assertFalse("Incorrect public key", kp.getPublic().equals(kp1.getPublic()));
    }
}
Also used : KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator)

Example 29 with KeyPairGenerator

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

the class KeyPairGenerator4Test method test_initializeLjava_security_spec_AlgorithmParameterSpec.

/**
     * java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec)
     */
public void test_initializeLjava_security_spec_AlgorithmParameterSpec() throws Exception {
    // create DSAParams
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    keyPairGenerator.initialize(1024);
    DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair().getPublic();
    DSAParams params = key.getParams();
    KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
    keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG()));
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 30 with KeyPairGenerator

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

the class KeyPairGenerator4Test method test_initializeILjava_security_SecureRandom.

/**
     * java.security.KeyPairGenerator#initialize(int,
     *        java.security.SecureRandom)
     */
public void test_initializeILjava_security_SecureRandom() throws Exception {
    KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
    keyPair.initialize(1024, new SecureRandom());
}
Also used : SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator)

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