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) {
}
}
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) {
}
}
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) {
}
}
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) {
}
}
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
}
}
Aggregations