1 package org.apache.fulcrum.jce.crypto.cli;
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 import org.apache.fulcrum.jce.crypto.CryptoUtil;
29 import org.apache.fulcrum.jce.crypto.StreamUtil;
30
31
32
33
34
35
36
37
38
39
40 public class CLI
41 {
42
43
44
45
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
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
85
86
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
120
121
122
123
124
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
166
167
168
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 }