use of javax.crypto.NullCipher 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.NullCipher 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.NullCipher in project robovm by robovm.
the class CipherOutputStream1Test method testWrite1.
/**
* write(int b) method testing. Tests that method writes correct values to
* the underlying output stream.
*/
public void testWrite1() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
for (int i = 0; i < data.length; i++) {
cos.write(data[i]);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
use of javax.crypto.NullCipher in project robovm by robovm.
the class CipherOutputStream1Test method testWrite5.
/**
* write(byte[] b, int off, int len)
*/
public void testWrite5() throws Exception {
//Regression for HARMONY-758
Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
NullCipher nc = new NullCipher();
CipherOutputStream stream1 = new CipherOutputStream(new BufferedOutputStream((OutputStream) null), nc);
CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
stream3.write(new byte[] { 0 }, 0, 0);
//no exception expected
}
use of javax.crypto.NullCipher in project robovm by robovm.
the class CipherOutputStream1Test method testWrite2.
/**
* write(byte[] b) method testing. Tests that method writes correct values
* to the underlying output stream.
*/
public void testWrite2() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
try {
cos.write(null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
//expected
}
}
Aggregations