EncryptCommand extends AbstractBaseCommand { @Override int execute(OptionsAndArgs pOpts, Object pVm, VirtualMachineHandler pHandler) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { try { List<String> args = pOpts.getExtraArgs(); String password = args.size() == 0 ? getPasswordFromConsoleOrStdin(pOpts) : args.get(0); JolokiaCipher jolokiaCipher = keyHolder != null ? new JolokiaCipher(keyHolder) : new JolokiaCipher(); String encrypted = jolokiaCipher.encrypt(password); System.out.printf("[[%s]]%n", encrypted); return 0; } catch (GeneralSecurityException exp) { throw new InvocationTargetException(exp,"Can not encrypt password"); } } EncryptCommand();  EncryptCommand(JolokiaCipher.KeyHolder pKeyHolder);   }
@Test public void testEncryptDecrypt() throws Exception { String passwd = "test 123"; JCPPTest keyHolder = new JCPPTest(); EncryptCommand ec = new EncryptCommand(keyHolder); OptionsAndArgs oaa = new OptionsAndArgs(CommandDispatcher.getAvailableCommands(),"encrypt", passwd); PrintStream original = System.out; ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.setOut(new PrintStream(baos)); ec.execute(oaa, null, null); System.setOut(original); Matcher matcher = Pattern.compile("^\\[\\[(.*)]]").matcher(baos.toString()); matcher.find(); String encrypted = matcher.group(1); JolokiaCipher jolokiaCipher = new JolokiaCipher(keyHolder); String decrypted = jolokiaCipher.decrypt(encrypted); assertEquals(decrypted,passwd); }