use of javax.crypto.SealedObject in project robovm by robovm.
the class SealedObjectTest method testDeserialization.
// http://code.google.com/p/android/issues/detail?id=4834
public void testDeserialization() throws Exception {
// (Boilerplate so we can create SealedObject instances.)
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Incorrect use of readUnshared meant you couldn't have two SealedObjects
// with the same algorithm or parameters algorithm...
ArrayList<SealedObject> sealedObjects = new ArrayList<SealedObject>();
for (int i = 0; i < 10; ++i) {
sealedObjects.add(new SealedObject("hello", cipher));
}
String serializedForm = SerializationTester.serializeHex(sealedObjects);
// ...so this would throw "java.io.InvalidObjectException: Unshared read of back reference".
SerializationTester.deserializeHex(serializedForm);
}
use of javax.crypto.SealedObject in project robovm by robovm.
the class SealedObjectTest method testGetObject2.
/**
* getObject(Cipher c) method testing. Tests if the proper exception is
* thrown in the case of incorrect input parameters and if the object sealed
* with encryption algorithm and specified parameters can be retrieved by
* specifying the initialized Cipher object.
*/
public void testGetObject2() throws Exception {
try {
new SealedObject("secret string", new NullCipher()).getObject((Cipher) 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);
String secret = "secret string";
SealedObject so = new SealedObject(secret, cipher);
cipher.init(Cipher.DECRYPT_MODE, key, ips);
assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(cipher));
try {
so.getObject((Cipher) null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
//expected
}
}
use of javax.crypto.SealedObject in project robovm by robovm.
the class SealedObjectTest method testGetAlgorithm.
/**
* getAlgorithm() method testing. Tests if the returned value equals to the
* corresponding value of Cipher object.
*/
public void testGetAlgorithm() throws Exception {
String secret = "secret string";
String algorithm = "DES";
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
SealedObject so = new SealedObject(secret, cipher);
assertEquals("The algorithm name should be the same as used " + "in cipher.", algorithm, so.getAlgorithm());
}
use of javax.crypto.SealedObject in project jdk8u_jdk by JetBrains.
the class PBESealedObject method runTest.
// Have a generic throws Exception as it can throw many different exceptions
public boolean runTest(Provider p, String algo, PrintStream out) throws Exception {
byte[] salt = new byte[8];
int ITERATION_COUNT = 1000;
AlgorithmParameters pbeParams = null;
String baseAlgo = new StringTokenizer(algo, "/").nextToken().toUpperCase();
boolean isAES = baseAlgo.contains("AES");
try {
// Initialization
Cipher ci = Cipher.getInstance(algo, p);
new Random().nextBytes(salt);
AlgorithmParameterSpec aps = new PBEParameterSpec(salt, ITERATION_COUNT);
SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Lover".toCharArray()));
// Seal
if (isAES) {
ci.init(Cipher.ENCRYPT_MODE, key);
pbeParams = ci.getParameters();
} else {
ci.init(Cipher.ENCRYPT_MODE, key, aps);
}
SealedObject so = new SealedObject(key, ci);
// Unseal and compare
if (isAES) {
ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
} else {
ci.init(Cipher.DECRYPT_MODE, key, aps);
}
SecretKey unsealedKey;
unsealedKey = (SecretKey) so.getObject(ci);
if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {
return false;
}
unsealedKey = (SecretKey) so.getObject(key);
if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {
return false;
}
unsealedKey = (SecretKey) so.getObject(key, "SunJCE");
return Arrays.equals(unsealedKey.getEncoded(), key.getEncoded());
} catch (InvalidKeyException ex) {
if (baseAlgo.endsWith("TRIPLEDES") || baseAlgo.endsWith("AES_256")) {
out.println("Expected exception , keyStrength > 128 within" + algo);
return true;
}
throw ex;
}
}
use of javax.crypto.SealedObject in project jdk8u_jdk by JetBrains.
the class SealedObjectTest method doTest.
/*
* Run the test:
* - init a cipher with AES/GCM/NoPadding transformation
* - seal an object
* - check if we can't seal it again with the same key/IV
* - unseal the object using different methods of SealedObject class
* - check if the original and sealed objects are equal
*/
static void doTest() throws Exception {
// init a secret Key
KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
kg.init(KEY_LENGTH);
SecretKey key = kg.generateKey();
// initialization
Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key);
AlgorithmParameters params = cipher.getParameters();
// seal an object
SealedObject so = new SealedObject(key, cipher);
try {
// check if we can't seal it again with the same key/IV
so = new SealedObject(key, cipher);
throw new RuntimeException("FAILED: expected IllegalStateException hasn't " + "been thrown");
} catch (IllegalStateException ise) {
System.out.println("Expected exception when seal it again with" + " the same key/IV: " + ise);
}
// unseal the object using getObject(Cipher) and compare
cipher.init(Cipher.DECRYPT_MODE, key, params);
SecretKey unsealedKey = (SecretKey) so.getObject(cipher);
assertKeysSame(unsealedKey, key, "SealedObject.getObject(Cipher)");
// unseal the object using getObject(Key) and compare
unsealedKey = (SecretKey) so.getObject(key);
assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key)");
// unseal the object using getObject(Key, String) and compare
unsealedKey = (SecretKey) so.getObject(key, PROVIDER);
assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key, String)");
}
Aggregations