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