use of javax.crypto.SealedObject in project jdk8u_jdk by JetBrains.
the class TestSealedObjectNull method main.
public static void main(String[] args) throws IOException, IllegalBlockSizeException, ClassNotFoundException, BadPaddingException {
Cipher nullCipher = new NullCipher();
// Seal
SealedObject so = new SealedObject(SEAL_STR, nullCipher);
// Unseal and compare
if (!(SEAL_STR.equals(so.getObject(nullCipher)))) {
throw new RuntimeException("Unseal and compare failed.");
}
System.out.println("Test passed.");
}
use of javax.crypto.SealedObject 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.SealedObject in project robovm by robovm.
the class SealedObjectTest method testSealedObject2.
/**
* SealedObject(SealedObject so) method testing. Tests if the
* NullPointerException is thrown in the case of null SealedObject.
*/
public void testSealedObject2() throws Exception {
try {
new SealedObject(null) {
};
fail("NullPointerException should be thrown in the case " + "of null SealedObject.");
} catch (NullPointerException e) {
}
String secret = "secret string";
Cipher cipher = new NullCipher();
SealedObject so1 = new SealedObject(secret, cipher);
SealedObject so2 = new SealedObject(so1) {
};
assertEquals("The secret content of the object should equals " + "to the secret content of initial object.", secret, so2.getObject(cipher));
assertEquals("The algorithm which was used to seal the object " + "should be the same as the algorithm used to seal the " + "initial object", so1.getAlgorithm(), so2.getAlgorithm());
}
use of javax.crypto.SealedObject 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
}
}
use of javax.crypto.SealedObject in project teiid by teiid.
the class BasicCryptor method unsealObject.
public synchronized Object unsealObject(Object object) throws CryptoException {
if (useSealedObject) {
if (!(object instanceof SealedObject)) {
return object;
}
SealedObject so = (SealedObject) object;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
if (cl != classLoader) {
Thread.currentThread().setContextClassLoader(BasicCryptor.class.getClassLoader());
}
return so.getObject(decryptCipher);
} catch (Exception e) {
try {
initDecryptCipher();
} catch (CryptoException err) {
// shouldn't happen
}
throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage()));
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
if (!(object instanceof byte[])) {
return object;
}
byte[] bytes = (byte[]) object;
bytes = decrypt(bytes);
try {
ObjectInputStream ois = new ObjectInputStreamWithClassloader(new ByteArrayInputStream(bytes), classLoader);
return ois.readObject();
} catch (Exception e) {
throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage()));
}
}
Aggregations