Search in sources :

Example 76 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class CipherTest method test_doFinal$BI.

public void test_doFinal$BI() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 0, 10);
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 3, 8);
    int len = c.doFinal(b1, 0);
    assertEquals(0, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b1, 0, 24);
    try {
        c.doFinal(b, 0);
        fail();
    } catch (BadPaddingException expected) {
    }
    b1 = new byte[6];
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    c.update(b, 3, 6);
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 77 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class CipherTest method test_doFinal$BII$B.

public void test_doFinal$BII$B() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b, 0, 10, b1);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b, 0, 10, b1);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len = c.doFinal(b, 0, 16, b1);
    assertEquals(16, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b1, 0, 24, new byte[42]);
        fail();
    } catch (BadPaddingException expected) {
    }
    b1 = new byte[6];
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.doFinal(b, 3, 6, b1);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 78 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class CipherTest method test_initWithKeyAlgorithmParameters.

public void test_initWithKeyAlgorithmParameters() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    assertNotNull(c.getParameters());
    try {
        c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_3DES, ap);
        fail();
    } catch (InvalidKeyException expected) {
    }
    try {
        c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, (AlgorithmParameters) null);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 79 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class CipherTest method test_initWithAlgorithmParameterSpec.

/**
     * javax.crypto.Cipher#init(int, java.security.Key,
     *        java.security.spec.AlgorithmParameterSpec)
     */
public void test_initWithAlgorithmParameterSpec() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap);
    byte[] cipherIV = cipher.getIV();
    assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap);
        fail();
    } catch (InvalidKeyException expected) {
    }
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 80 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec 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)

Aggregations

IvParameterSpec (javax.crypto.spec.IvParameterSpec)229 Cipher (javax.crypto.Cipher)150 SecretKeySpec (javax.crypto.spec.SecretKeySpec)107 SecretKey (javax.crypto.SecretKey)49 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)46 InvalidKeyException (java.security.InvalidKeyException)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)42 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)39 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 BadPaddingException (javax.crypto.BadPaddingException)28 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 Key (java.security.Key)21 KeyGenerator (javax.crypto.KeyGenerator)21 IOException (java.io.IOException)19 SecureRandom (java.security.SecureRandom)17 GeneralSecurityException (java.security.GeneralSecurityException)15 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)15 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)14 MessageDigest (java.security.MessageDigest)13 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13