View Javadoc
1   package org.apache.fulcrum.jce.crypto.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileOutputStream;
27  
28  import org.apache.fulcrum.jce.crypto.CryptoUtil;
29  import org.apache.fulcrum.jce.crypto.StreamUtil;
30  
31  /**
32   * Command line tool for encrypting/decrypting files
33   *
34   * file [enc|dec] passwd [file]*
35   * string [enc|dec] passwd plaintext
36   *
37   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
38   */
39  
40  public class CLI
41  {
42      /**
43       * Allows testing on the command line.
44       * 
45       * @param args the command line parameters
46       */
47      public static void main( String[] args )
48      {
49          try
50          {
51              if( args.length < 3 )
52              {
53                  printHelp();
54                  throw new IllegalArgumentException("Invalid command line");
55              }
56  
57              String operationMode = args[0];
58  
59              if( operationMode.equals("file") )
60              {
61                  processFiles(args);
62              }
63              else if( operationMode.equals("string") )
64              {
65                  processString(args);
66              }
67          }
68          catch (Exception e)
69          {
70              System.out.println("Error : " + e.getMessage());
71          }
72      }
73  
74      /**
75       * Prints usage information.
76       */
77      public static void printHelp()
78      {
79          System.out.println("Main file [enc|dec] passwd source [target]");
80          System.out.println("Main string [enc|dec] passwd source");
81      }
82  
83      /**
84       * Decrypt/encrypt a list of files
85       * @param args the command line
86       * @throws Exception the operation failed
87       */
88      public static void processFiles(String[] args)
89          throws Exception
90      {
91          String cipherMode = args[1];
92          char[] password = args[2].toCharArray();
93          File sourceFile = new File(args[3]);
94          File targetFile = null;
95  
96          if( args.length == 4 )
97          {
98              targetFile = sourceFile;
99          }
100         else
101         {
102             targetFile = new File(args[4]);
103             File parentFile = targetFile.getParentFile(); 
104 
105             if (parentFile != null)
106             {
107                 boolean success = parentFile.mkdirs();
108                 if ( !success )
109                 {
110                 	System.err.println("Failed to create directory");
111                 }
112             }
113         }
114 
115         processFile(cipherMode,password,sourceFile,targetFile);
116     }
117 
118     /**
119      * Decrypt and encrypt a single file
120      * @param cipherMode the mode
121      * @param password the password
122      * @param sourceFile the file to process
123      * @param targetFile the target file
124      * @throws Exception the operation failed
125      */
126     public static void processFile(String cipherMode, char[] password, File sourceFile, File targetFile)
127         throws Exception
128     {
129         try (FileInputStream fis = new FileInputStream(sourceFile)) {
130             ByteArrayOutputStream baos = new ByteArrayOutputStream();
131     
132             if( cipherMode.equals("dec") )
133             {
134                 System.out.println("Decrypting " + sourceFile.getAbsolutePath() );
135                 CryptoUtil.getInstance().decrypt( fis, baos, password );
136                 fis.close();
137     
138                 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
139                 FileOutputStream fos = new FileOutputStream(targetFile);
140                 StreamUtil.copy(bais,fos);
141                 bais.close();
142                 fos.close();
143             }
144             else if( cipherMode.equals("enc") )
145             {
146                 System.out.println("Encrypting " + sourceFile.getAbsolutePath() );
147                 CryptoUtil.getInstance().encrypt( fis, baos, password );
148                 fis.close();
149     
150                 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
151                 FileOutputStream fos = new FileOutputStream(targetFile);
152                 StreamUtil.copy(bais,fos);
153                 bais.close();
154                 fos.close();
155             }
156             else
157             {
158                 String msg = "Don't know what to do with : " + cipherMode;
159                 throw new IllegalArgumentException(msg);
160             }
161         }
162     }
163 
164     /**
165      * Decrypt/encrypt a string.
166      * 
167      * @param args the command line
168      * @throws Exception the operation failed
169      */
170     public static void processString(String[] args)
171         throws Exception
172     {
173         String cipherMode = args[1];
174         char[] password = args[2].toCharArray();
175         String value = args[3];
176         String result = null;
177 
178         if( cipherMode.equals("dec") )
179         {
180             result = CryptoUtil.getInstance().decryptString(value,password);
181         }
182         else
183         {
184             result = CryptoUtil.getInstance().encryptString(value,password);
185         }
186 
187         System.out.println( result );
188     }
189 }