1 package org.apache.fulcrum.jce.crypto;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 /**
29 * Command line tool for encrypting/decrypting files
30 *
31 * file [enc|dec] passwd [file]*
32 * string [enc|dec] passwd plaintext
33 *
34 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
35 */
36
37 public class Main
38 {
39 /**
40 * Allows testing on the command lnie
41 * @param args the command line parameters
42 */
43 public static void main( String[] args )
44 {
45 try
46 {
47 if( args.length < 3 )
48 {
49 printHelp();
50 throw new IllegalArgumentException("Invalid command line");
51 }
52
53 String operationMode = args[0];
54
55 if( operationMode.equals("file") )
56 {
57 processFiles(args);
58 }
59 else if( operationMode.equals("string") )
60 {
61 processString(args);
62 }
63
64 }
65 catch (Exception e)
66 {
67 System.out.println(e.getMessage());
68 }
69 }
70
71 /**
72 * Prints usage information.
73 */
74 public static void printHelp()
75 {
76 System.out.println("Main file [enc|dec] passwd source [target]");
77 System.out.println("Main string [enc|dec] passwd ");
78 }
79
80 /**
81 * Decrypt/encrypt a list of files
82 * @param args the command line
83 * @throws Exception the operation failed
84 */
85 public static void processFiles(String[] args)
86 throws Exception
87 {
88 String cipherMode = args[1];
89 char[] password = args[2].toCharArray();
90 File sourceFile = new File(args[3]);
91 File targetFile = null;
92
93 if( args.length == 4 )
94 {
95 targetFile = sourceFile;
96 }
97 else
98 {
99 targetFile = new File(args[4]);
100 }
101
102 processFile(cipherMode,password,sourceFile,targetFile);
103 }
104
105 /**
106 * Decrypt/encrypt a single file
107 * @param cipherMode the mode
108 * @param password the passwors
109 * @param sourceFile the file to process
110 * @param targetFile the targetf file
111 * @throws Exception the operation failed
112 */
113 public static void processFile(String cipherMode, char[] password, File sourceFile, File targetFile)
114 throws Exception
115 {
116 FileInputStream fis = new FileInputStream(sourceFile);
117 ByteArrayOutputStream baos = new ByteArrayOutputStream();
118
119 if( cipherMode.equals("dec") )
120 {
121 System.out.println("Decrypting " + sourceFile.getAbsolutePath() );
122 CryptoUtil.decrypt( fis, baos, password );
123 fis.close();
124
125 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
126 FileOutputStream fos = new FileOutputStream(targetFile);
127 CryptoUtil.copy(bais,fos);
128 bais.close();
129 fos.close();
130 }
131 else if( cipherMode.equals("enc") )
132 {
133 System.out.println("Enrypting " + sourceFile.getAbsolutePath() );
134 CryptoUtil.encrypt( fis, baos, password );
135 fis.close();
136
137 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
138 FileOutputStream fos = new FileOutputStream(targetFile);
139 CryptoUtil.copy(bais,fos);
140 bais.close();
141 fos.close();
142 }
143 else
144 {
145 String msg = "Don't know what to do with : " + cipherMode;
146 throw new IllegalArgumentException(msg);
147 }
148 }
149
150 /**
151 * Decrypt/encrypt a string
152 * @param args the command line
153 * @throws Exception the operation failed
154 */
155 public static void processString(String[] args)
156 throws Exception
157 {
158 String cipherMode = args[1];
159 char[] password = args[2].toCharArray();
160 String value = args[3];
161 String result = null;
162
163 if( cipherMode.equals("dec") )
164 {
165 result = CryptoUtil.decryptString(value,password);
166 }
167 else
168 {
169 result = CryptoUtil.encryptString(value,password);
170 }
171
172 System.out.println( result );
173 }
174 }