Code example for KeyPairGenerator

Methods: genKeyPair, generateKeyPair, initialize

0
                        .concat(defaultAlg.concat(post)),
                KeyPairGeneratorProviderClass);
        Security.insertProviderAt(mProv, 1);
    } 
 
    private void checkResult(KeyPairGenerator keyPairGen, int mode)
            throws InvalidAlgorithmParameterException { 
        AlgorithmParameterSpec pp = null;
        switch (mode) {
        case 1: 
            try { 
                keyPairGen.initialize(pp, new SecureRandom());
                fail("InvalidAlgorithmParameterException must be thrown");
            } catch (InvalidAlgorithmParameterException e) {
            } 
            keyPairGen.initialize(1000, new SecureRandom());
            try { 
                keyPairGen.initialize(-1024, new SecureRandom());
                fail("InvalidParameterException must be thrown");
            } catch (InvalidParameterException e) {
                assertEquals("Incorrect exception", e.getMessage(),
                        "Incorrect keysize parameter"); 
            } 
            try { 
                keyPairGen.initialize(100, null);
                fail("InvalidParameterException must be thrown");
            } catch (InvalidParameterException e) {
                assertEquals("Incorrect exception", e.getMessage(),
                        "Incorrect random"); 
            } 
            keyPairGen.generateKeyPair();
            keyPairGen.genKeyPair();
            break; 
        case 2: 
            try { 
                keyPairGen.initialize(pp, new SecureRandom());
            } catch (UnsupportedOperationException e) {
                // js2e does not throw this exception 
            } 
            keyPairGen.initialize(1000, new SecureRandom());
            try { 
                keyPairGen.initialize(63, new SecureRandom());
                fail("InvalidParameterException must be thrown");
            } catch (InvalidParameterException e) {
            } 
            keyPairGen.initialize(100, null);
            assertNull("Not null KeyPair", keyPairGen.generateKeyPair());
            assertNull("Not null KeyPair", keyPairGen.genKeyPair());
            break; 
        case 3: 
            keyPairGen.initialize(pp, new SecureRandom());
            keyPairGen.initialize(pp);
            keyPairGen.initialize(1000, new SecureRandom());
            keyPairGen.initialize(100);
 
            assertNotNull("Null KeyPair", keyPairGen.generateKeyPair());
            assertNotNull("Null KeyPair", keyPairGen.genKeyPair());
            break; 
        case 4: 
            try { 
                keyPairGen.initialize(pp, null);
                fail("UnsupportedOperationException must be thrown");
            } catch (UnsupportedOperationException e) {
            } 
            keyPairGen.initialize(pp, new SecureRandom());
            keyPairGen.initialize(101, new SecureRandom());
            keyPairGen.initialize(10000);
            try { 
                keyPairGen.initialize(101, null);
                fail("IllegalArgumentException must be thrown for null random");
            } catch (IllegalArgumentException e) {
            } 
            try { 
                keyPairGen.initialize(99, new SecureRandom());
                fail("InvalidParameterException must be thrown for invalid key");
            } catch (InvalidParameterException e) {
            } 
            try { 
                keyPairGen.initialize(99);
                fail("InvalidParameterException must be thrown for invalid key");
            } catch (InvalidParameterException e) {
            } 
            try { 
                keyPairGen.initialize(199, null);
                fail("IllegalArgumentException must be thrown for null random");
            } catch (IllegalArgumentException e) {
            } 
            assertNull("Not null KeyPair", keyPairGen.generateKeyPair());