Code example for KeyGenerator

Methods: KeyGenerator.init, generateKey, init

0
 
    private void checkResult(KeyGenerator keyGen) {
        AlgorithmParameterSpec paramsNull = null;
        AlgorithmParameterSpec params = new APSpec();
        try { 
            keyGen.init(0, new SecureRandom());
            fail("IllegalArgumentException must be thrown");
        } catch (IllegalArgumentException e) {
        } 
        try { 
            keyGen.init(77, new SecureRandom());
            fail("IllegalArgumentException must be thrown");
        } catch (IllegalArgumentException e) {
        } 
        keyGen.init(78, new SecureRandom());
        try { 
            keyGen.init(new SecureRandom());
            fail("IllegalArgumentException must be thrown");                
        } catch (IllegalArgumentException e) {
        } 
        assertNull("generateKey must return null", keyGen.generateKey());
        try { 
            keyGen.init(paramsNull, new SecureRandom());
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        } 
        try { 
            keyGen.init(params, new SecureRandom());                
        } catch (Exception e) {
            fail("Unexpected: " + e.toString() + " was thrown");
        } 
        try { 
            keyGen.init(paramsNull);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        } 
        try { 
            keyGen.init(params);                
        } catch (Exception e) {
            fail("Unexpected: " + e.toString() + " was thrown");
        } 
    }