TokenEncryptor { public String encrypt(Token token) throws TokenEncryptionException { if (token == null) { throw new NullPointerException("token is null!"); } try { ByteArrayOutputStream memOutputStream = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(memOutputStream); out.writeObject(token); out.close(); byte[] serializedToken = memOutputStream.toByteArray(); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParamSpec); byte[] encryptedTokenBytes = cipher.doFinal(serializedToken); return Base64.encodeBase64String(encryptedTokenBytes); } catch (Exception e) { log.error("Unable to encrypt token!", e); throw new TokenEncryptionException(e); } } TokenEncryptor(String secretKey); String generate(); String encrypt(Token token); Token decrypt(String encryptedToken);  }
@Test public void testEncrypt() throws TokenEncryptionException { Token token = new Token(1L); String res = tokenEncryptor.encrypt(token); System.out.println("res=" + res); }