Exemplo n.º 1
0
 @Test
 public void testRc4WithEmptyData() throws IOException {
   Closer closer = Closer.create();
   try {
     // generate random input data
     byte[] randomData = TestUtils.randomBytes(0, true);
     InputStream randomInputStream = new ByteArrayInputStream(randomData);
     closer.register(randomInputStream);
     // generate cipher key
     byte[] keyBytes = Rc4Utils.generateKey();
     // encrypt and write
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     closer.register(byteArrayOutputStream);
     OutputStream encryptedOutputStream = Rc4Utils.encrypt(byteArrayOutputStream, keyBytes);
     closer.register(encryptedOutputStream);
     ByteStreams.copy(randomInputStream, encryptedOutputStream);
     byte[] encryptedBytes = byteArrayOutputStream.toByteArray();
     // read and decrypt
     InputStream encryptedInputStream = new ByteArrayInputStream(encryptedBytes);
     closer.register(encryptedInputStream);
     InputStream decryptedInputStream = Rc4Utils.decrypt(encryptedInputStream, keyBytes);
     closer.register(decryptedInputStream);
     byte[] decryptedData = ByteStreams.toByteArray(decryptedInputStream);
     // checking data
     Assert.assertArrayEquals(randomData, decryptedData);
     Assert.assertArrayEquals(Rc4Utils.encrypt(randomData, keyBytes), encryptedBytes);
     Assert.assertArrayEquals(Rc4Utils.decrypt(encryptedBytes, keyBytes), randomData);
   } catch (Throwable t) {
     closer.rethrow(t);
   } finally {
     closer.close();
   }
 }