KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > ExtractJEF


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// 08.06.2001 NK faster by using buffer of bytes to read data
14
//
15
//
16
package org.jahia.tools;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.DataInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.security.Key JavaDoc;
27 import java.security.Security JavaDoc;
28 import java.util.zip.CRC32 JavaDoc;
29
30 import javax.crypto.Cipher;
31 import javax.crypto.SecretKeyFactory;
32 import javax.crypto.spec.DESKeySpec;
33
34
35 /**
36  * Used to extract a jef file.
37  *
38  * @deprecated ReadJEF with -e do the same !!!
39  * @see org.jahia.tools.ReadJEF
40  * @version 1.0
41  */

42 public class ExtractJEF
43 {
44
45     /** the help option **/
46     private static final String JavaDoc HELP_OPTION = "-help";
47
48     private static final String JavaDoc CHAR_ENC = "UTF-16";
49
50
51     //-------------------------------------------------------------------------
52
public static void main (String JavaDoc args[])
53     {
54         System.out.println ("\nJahia Encrypted File Extractor, version 1.0");
55         System.out.println ("(C) Jahia Ltd 2002\n\n");
56
57         // The source file
58
String JavaDoc srcFileName = "";
59         
60         /**
61          * Retrieve command line arguments
62          */

63
64         // if no parameters are specified, display the tool usage.
65
if ((args.length > 0) && (HELP_OPTION.equals (args[0]))) {
66             DisplayHelp ();
67             return;
68         }
69
70         srcFileName = args[0];
71         
72         // check if the file exists or not
73
File JavaDoc srcFile = new File JavaDoc(srcFileName);
74         if ( !fileExists(srcFile.getAbsolutePath()) ){
75             System.out.println ("Error: Source file "+srcFileName+
76                                 " not found !");
77             return;
78         }
79
80
81         try {
82
83             /**
84              * Decrypt data
85              */

86
87             FileInputStream JavaDoc fstream = new FileInputStream JavaDoc (srcFile);
88             DataInputStream JavaDoc stream = new DataInputStream JavaDoc (fstream);
89
90             // create the raw byte stream from the file size by removing the
91
// CRC32 checksum bytes ( long = 8 bytes ) ,the offset ( int = 4 bytes )
92
// and the Cipher secret key ( int = 4 bytes )
93
int streamSize = (new Long JavaDoc(srcFile.length() - 16)).intValue();
94             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
95             byte[] buff = null;
96             int crcOffset = stream.readInt ();
97
98             //System.out.println (" offset " + crcOffset);
99

100             // read the first part of data
101
buff = new byte[crcOffset];
102             stream.read(buff,0,crcOffset);
103             bos.write(buff);
104
105             // int the Cipher
106
Security.addProvider(
107                new com.sun.crypto.provider.SunJCE());
108
109
110             // get the secret key
111
int keyLength = stream.readInt();
112             byte[] keyAsBytes = new byte[keyLength];
113             stream.read(keyAsBytes);
114
115             DESKeySpec keySpec = new DESKeySpec(keyAsBytes);
116
117             Key JavaDoc myKey = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
118             keyAsBytes = myKey.getEncoded();
119             System.out.println (" Cipher secret key = "+ new String JavaDoc(keyAsBytes) );
120             
121             Cipher decCipher = Cipher.getInstance("DES");
122
123
124             decCipher.init(Cipher.DECRYPT_MODE, myKey);
125             
126             long storedChecksum = stream.readLong ();
127             System.out.println (" stored checksum = "+Long.toHexString (storedChecksum));
128
129             // read the remaining part of data
130
buff = new byte[streamSize-(crcOffset+keyLength)];
131             stream.read(buff,0,buff.length);
132             bos.write(buff);
133             
134             byte[] bytes = bos.toByteArray();
135
136             // free memory of unused objects
137
fstream.close();
138             stream = null;
139             fstream = null;
140             srcFile = null;
141             bos.close();
142             bos = null;
143             
144             
145             //DisplayBytes (bytes);
146

147             // compute the stream CRC
148
CRC32 JavaDoc crc = new CRC32 JavaDoc();
149             crc.update (bytes);
150
151             long streamChecksum = crc.getValue ();
152             System.out.println (" stream checksum = "+Long.toHexString (streamChecksum)+ "\n");
153
154             //System.out.println(" data stream length before Cipher decryption " + bytes.length );
155

156             byte[] decryptedBytes = decCipher.doFinal(bytes);
157
158             bytes = null;
159
160             //System.out.println(" data stream length after Cipher decryption " + decryptedBytes.length );
161

162             ByteArrayInputStream JavaDoc byteStream = new ByteArrayInputStream JavaDoc (decryptedBytes);
163             stream = new DataInputStream JavaDoc (byteStream);
164             short offset = 0;
165
166             System.out.println (" Jahia Encrypted File Info :");
167
168             byte[] stringAsBytes = null;
169             
170             // extract keys value
171
offset = stream.readShort ();
172             //System.out.println ("keys nb char: " + offset);
173

174             if ( offset>0 ){
175                 stringAsBytes = new byte[offset];
176                 stream.read(stringAsBytes);
177                 String JavaDoc keys = new String JavaDoc(stringAsBytes,CHAR_ENC);
178                 System.out.println (" keys : " + keys + "\n");
179             } else {
180                 System.out.println (" no keys values\n");
181             }
182                 
183             //Extract the file content
184
byte[] data = new byte[decryptedBytes.length-(offset+2)];
185             
186             stream.read(data,0,data.length);
187
188             // extract the encrypted files
189
byteStream = new ByteArrayInputStream JavaDoc (data);
190             stream = new DataInputStream JavaDoc (byteStream);
191
192             
193             int nbBytes = data.length;
194
195             //System.out.println(" files total bytes " + nbBytes + "\n");
196

197             int nbBytesRead = 0;
198             offset = 0;
199             String JavaDoc fileName = "";
200             Long JavaDoc fSize = null;
201             byte[] aFile = null;
202                         
203             while( nbBytesRead<nbBytes ){
204                 
205                 
206                 // extract the filename
207
offset = stream.readShort();
208                 nbBytesRead += 2;
209
210                 stringAsBytes = new byte[offset];
211                 stream.read(stringAsBytes);
212                 fileName = new String JavaDoc(stringAsBytes,CHAR_ENC);
213                 nbBytesRead += offset;
214                 System.out.println(" extract file " + fileName);
215                         
216                 // extract the file size
217
fSize = new Long JavaDoc(stream.readLong());
218                 aFile = new byte[fSize.intValue()];
219                 
220                 nbBytesRead += 8;
221                 
222                 stream.read(aFile,0,fSize.intValue());
223                 nbBytesRead += fSize.intValue();
224
225                 // write out the original file.
226
try {
227                     
228                     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc (fileName);
229         
230                     fos.write (aFile);
231         
232                     // flush the stream into the file and close the file
233
fos.flush();
234                     fos.close();
235                 }
236                 catch (FileNotFoundException JavaDoc ex) {
237                     System.out.println ("ERROR : Could not create the encrypted file");
238                     return;
239                 }
240                 catch (SecurityException JavaDoc ex) {
241                     System.out.println ("ERROR : No security permissions to create the file.");
242                     return;
243                 }
244                 catch (IOException JavaDoc ex) {
245                     System.out.println ("ERROR : I/O error.");
246                     return;
247                 }
248
249                 //System.out.println(" Nb bytes read " + nbBytesRead + "\n");
250

251             }
252
253         }
254         catch (IOException JavaDoc ex) {
255             //System.out.println ("ERROR : I/O exception while reading the file.");
256
return;
257         }
258         catch (Exception JavaDoc e) {
259             System.out.println ("ERROR : " + e.getMessage());
260             return;
261         }
262     }
263
264
265     //-------------------------------------------------------------------------
266
private static void DisplayBytes (byte[] bytes)
267     {
268         //System.out.print (" stream = ");
269

270         for (int i=0; i<bytes.length; i++) {
271             //System.out.print (bytes[i]+" ");
272
}
273
274         //System.out.print("\n");
275
}
276
277
278     //-------------------------------------------------------------------------
279
private static void DisplayHelp ()
280     {
281         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc ();
282
283         buffer.append (" usage : ExtractEPF [options] myFile\n\n");
284         buffer.append (" options :\n");
285
286         buffer.append (" source file \n"+
287                        " the source file name)\n");
288
289         buffer.append ("\n\n");
290
291         System.out.println (buffer.toString());
292     }
293
294
295     //-------------------------------------------------------------------------
296
/**
297      * check if a file or directory exists. The check is case sensitive
298      *
299      * @author NK
300      * @param String the absolute path
301      * @return boolean true if comparison is success
302      */

303     private static boolean fileExists(String JavaDoc path){
304         
305         File JavaDoc tmpFile = new File JavaDoc(path);
306         if ( tmpFile != null && tmpFile.isFile() ){
307             String JavaDoc name = tmpFile.getName();
308             if ( tmpFile.getParentFile() != null ){
309                 File JavaDoc[] files = tmpFile.getParentFile().listFiles();
310                 int nbFiles = files.length;
311                 for (int i=0 ; i<nbFiles ; i++){
312                     if ( files[i].getName().equals(name) ){
313                         return true;
314                     }
315                 }
316             }
317         }
318         return false;
319     }
320
321 }
322
Popular Tags