001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.console.command;
018
019import java.util.List;
020
021import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
022import org.jasypt.iv.RandomIvGenerator;
023
024public class DecryptCommand extends EncryptCommand {
025
026    protected String[] helpFile = new String[] {
027            "Task Usage: Main decrypt --password <password> --input <input>",
028            "Description: Decrypts given text.",
029            "", 
030            "Encrypt Options:",
031            "    --password <password>      Password to be used by the encryptor.  Defaults to",
032            "                               the value in the ACTIVEMQ_ENCRYPTION_PASSWORD env variable.",
033            "    --input <input>            Text to be encrypted.",
034            "    --algorithm <algorithm>    Algorithm to use.",
035            "    --version                  Display the version information.",
036            "    -h,-?,--help               Display the stop broker help information.",
037            ""
038        };    
039    
040    @Override
041    public String getName() {
042        return "decrypt";
043    }
044
045    @Override
046    public String getOneLineDescription() {
047        return "Decrypts given text";
048    }
049
050    @Override
051    protected void runTask(List<String> tokens) throws Exception {
052        if( password == null ) {
053            password = System.getenv("ACTIVEMQ_ENCRYPTION_PASSWORD");
054        }
055        if (password == null || input == null) {
056            context.printException(new IllegalArgumentException("input and password parameters are mandatory"));
057            return;
058        }
059        encryptor.setPassword(password);
060        if (algorithm != null) {
061            encryptor.setAlgorithm(algorithm);
062            // From Jasypt: for PBE-AES-based algorithms, the IV generator is MANDATORY"
063            if (algorithm.startsWith("PBE") && algorithm.contains("AES")) {
064                encryptor.setIvGenerator(new RandomIvGenerator());
065            }
066        }
067        try {
068            context.print("Decrypted text: " + encryptor.decrypt(input));
069        } catch (EncryptionOperationNotPossibleException e) {
070            context.print("ERROR: Text cannot be decrypted, check your input and password and try again!");
071        }
072    }
073
074    
075    
076}