EncryptingSerializerDeserializer implements SerializerDeserializer { @Override public Object deserialize(byte[] data) { byte[] decrypted; try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[16]; System.arraycopy(data, 0, iv, 0, iv.length); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); decrypted = cipher.doFinal(data, iv.length, data.length-iv.length); } catch (Exception e) { throw new IllegalStateException(e); } return wrapped.deserialize(decrypted); } EncryptingSerializerDeserializer();  EncryptingSerializerDeserializer(SerializerDeserializer wrapped); @Override byte[] serialize(Object value); @Override Object deserialize(byte[] data); @Override void setSessionManager(SessionManager sessionManager);  }
@Test public void testDeserialize() { EncryptingSerializerDeserializer serializer = new EncryptingSerializerDeserializer(); SessionManager sessionManager = mock(SessionManager.class); SessionConfiguration configuration = new SessionConfiguration(); configuration.setUsingEncryption(true); configuration.setEncryptionKey("test"); when(sessionManager.getMetrics()).thenReturn(new MetricRegistry()); when(sessionManager.getConfiguration()).thenReturn(configuration); serializer.setSessionManager(sessionManager); EncryptingSerializerDeserializer tempSerializer = new EncryptingSerializerDeserializer(); SessionManager sm = mock(SessionManager.class); when(sm.getMetrics()).thenReturn(new MetricRegistry()); when(sm.getConfiguration()).thenReturn(configuration); tempSerializer.setSessionManager(sm); Date now = new Date(); byte[] serializedString = serializer.serialize("String"); byte[] serializedDate = serializer.serialize(now); byte[] serializedNull = serializer.serialize(null); assertEquals(null, serializer.deserialize(serializedNull)); assertEquals("String", serializer.deserialize(serializedString)); assertEquals(now, serializer.deserialize(serializedDate)); }