Search in sources :

Example 21 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class myKeyGenerator method testInitKey.

/*
     * Test for <code>init(int keysize)</code> and
     * <code>init(int keysize, SecureRandom random)</code> methods
     * Assertion: throws InvalidParameterException if keysize is wrong
     *
     */
public void testInitKey() throws Exception {
    byte flag = 0xF;
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    if (defaultAlgorithm.equals(validAlgorithmsKeyGenerator[validAlgorithmsKeyGenerator.length - 1])) {
        return;
    }
    int[] size = { Integer.MIN_VALUE, -1, 0, 112, 168, Integer.MAX_VALUE };
    KeyGenerator[] kgs = createKGs();
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kgs.length; i++) {
        for (int j = 0; j < size.length; j++) {
            try {
                kgs[i].init(size[j]);
                flag &= 0xE;
            } catch (InvalidParameterException ignore) {
                flag &= 0xD;
            }
            try {
                kgs[i].init(size[j], random);
                flag &= 0xB;
            } catch (InvalidParameterException ignore) {
                flag &= 0x7;
            }
        }
    }
    assertTrue(flag == 0);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 22 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class CipherInputStream1Test method test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100]);
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherInputStream cis = new CipherInputStream(bais, c);
    assertNotNull(cis);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) NullCipher(javax.crypto.NullCipher) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 23 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class SealedObjectTest method testGetObject1.

/**
     * getObject(Key key) method testing. Tests if the object sealed with
     * encryption algorithm and specified parameters can be retrieved by
     * specifying the cryptographic key.
     */
public void testGetObject1() throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    String secret = "secret string";
    Mock_SealedObject so = new Mock_SealedObject(secret, cipher);
    assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(key));
    assertTrue("The encodedParams field of SealedObject object " + "should contain the encoded algorithm parameters.", Arrays.equals(so.get_encodedParams(), cipher.getParameters().getEncoded()));
    try {
        so.getObject((Key) null);
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    } catch (NullPointerException e) {
    //also ok
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) InvalidKeyException(java.security.InvalidKeyException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 24 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class SealedObjectTest method testSealedObject1.

/**
     * SealedObject(Serializable object, Cipher c) method testing. Tests if the
     * NullPointerException is thrown in the case of null cipher.
     */
public void testSealedObject1() throws Exception {
    String secret = "secret string";
    try {
        new SealedObject(secret, null);
        fail("NullPointerException should be thrown in the case " + "of null cipher.");
    } catch (NullPointerException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    SealedObject so = new SealedObject(secret, cipher);
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    try {
        new SealedObject(secret, cipher);
        fail("IllegalBlockSizeException expected");
    } catch (IllegalBlockSizeException e) {
    //expected
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) SealedObject(javax.crypto.SealedObject) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 25 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class SealedObjectTest method testGetObject3.

/**
     * getObject(Key key, String provider) method testing. Tests if the proper
     * exception is thrown in the case of incorrect input parameters and if the
     * object sealed with encryption algorithm can be retrieved by specifying
     * the cryptographic key and provider name.
     */
public void testGetObject3() throws Exception {
    try {
        new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), null);
        fail("IllegalArgumentException should be thrown in the case of " + "null provider.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), "");
        fail("IllegalArgumentException should be thrown in the case of " + "empty provider.");
    } catch (IllegalArgumentException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance("DES");
    String provider = cipher.getProvider().getName();
    cipher.init(Cipher.ENCRYPT_MODE, key);
    String secret = "secret string";
    SealedObject so = new SealedObject(secret, cipher);
    cipher.init(Cipher.DECRYPT_MODE, key);
    assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(key, provider));
    kg = KeyGenerator.getInstance("DESede");
    key = kg.generateKey();
    try {
        so.getObject(key, provider);
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        so.getObject(key, "Wrong provider name");
        fail("NoSuchProviderException expected");
    } catch (NoSuchProviderException e) {
    //expected
    }
}
Also used : NullCipher(javax.crypto.NullCipher) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Aggregations

KeyGenerator (javax.crypto.KeyGenerator)166 SecretKey (javax.crypto.SecretKey)117 SecureRandom (java.security.SecureRandom)53 Cipher (javax.crypto.Cipher)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)32 Key (java.security.Key)31 Test (org.junit.Test)25 InvalidKeyException (java.security.InvalidKeyException)19 IvParameterSpec (javax.crypto.spec.IvParameterSpec)19 IOException (java.io.IOException)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 BadPaddingException (javax.crypto.BadPaddingException)13 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)13 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)13 File (java.io.File)12 FileOutputStream (java.io.FileOutputStream)12 RouteBuilder (org.apache.camel.builder.RouteBuilder)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Provider (java.security.Provider)11 SecretKeySpec (javax.crypto.spec.SecretKeySpec)10