1 package org.jahia.tools; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.DataInputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.security.Key ; 27 import java.security.Security ; 28 import java.util.zip.CRC32 ; 29 30 import javax.crypto.Cipher; 31 import javax.crypto.SecretKeyFactory; 32 import javax.crypto.spec.DESKeySpec; 33 34 35 42 public class ExtractJEF 43 { 44 45 46 private static final String HELP_OPTION = "-help"; 47 48 private static final String CHAR_ENC = "UTF-16"; 49 50 51 public static void main (String 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 String srcFileName = ""; 59 60 63 64 if ((args.length > 0) && (HELP_OPTION.equals (args[0]))) { 66 DisplayHelp (); 67 return; 68 } 69 70 srcFileName = args[0]; 71 72 File srcFile = new File (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 86 87 FileInputStream fstream = new FileInputStream (srcFile); 88 DataInputStream stream = new DataInputStream (fstream); 89 90 int streamSize = (new Long (srcFile.length() - 16)).intValue(); 94 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 95 byte[] buff = null; 96 int crcOffset = stream.readInt (); 97 98 100 buff = new byte[crcOffset]; 102 stream.read(buff,0,crcOffset); 103 bos.write(buff); 104 105 Security.addProvider( 107 new com.sun.crypto.provider.SunJCE()); 108 109 110 int keyLength = stream.readInt(); 112 byte[] keyAsBytes = new byte[keyLength]; 113 stream.read(keyAsBytes); 114 115 DESKeySpec keySpec = new DESKeySpec(keyAsBytes); 116 117 Key myKey = SecretKeyFactory.getInstance("DES").generateSecret(keySpec); 118 keyAsBytes = myKey.getEncoded(); 119 System.out.println (" Cipher secret key = "+ new String (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 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 fstream.close(); 138 stream = null; 139 fstream = null; 140 srcFile = null; 141 bos.close(); 142 bos = null; 143 144 145 147 CRC32 crc = new CRC32 (); 149 crc.update (bytes); 150 151 long streamChecksum = crc.getValue (); 152 System.out.println (" stream checksum = "+Long.toHexString (streamChecksum)+ "\n"); 153 154 156 byte[] decryptedBytes = decCipher.doFinal(bytes); 157 158 bytes = null; 159 160 162 ByteArrayInputStream byteStream = new ByteArrayInputStream (decryptedBytes); 163 stream = new DataInputStream (byteStream); 164 short offset = 0; 165 166 System.out.println (" Jahia Encrypted File Info :"); 167 168 byte[] stringAsBytes = null; 169 170 offset = stream.readShort (); 172 174 if ( offset>0 ){ 175 stringAsBytes = new byte[offset]; 176 stream.read(stringAsBytes); 177 String keys = new String (stringAsBytes,CHAR_ENC); 178 System.out.println (" keys : " + keys + "\n"); 179 } else { 180 System.out.println (" no keys values\n"); 181 } 182 183 byte[] data = new byte[decryptedBytes.length-(offset+2)]; 185 186 stream.read(data,0,data.length); 187 188 byteStream = new ByteArrayInputStream (data); 190 stream = new DataInputStream (byteStream); 191 192 193 int nbBytes = data.length; 194 195 197 int nbBytesRead = 0; 198 offset = 0; 199 String fileName = ""; 200 Long fSize = null; 201 byte[] aFile = null; 202 203 while( nbBytesRead<nbBytes ){ 204 205 206 offset = stream.readShort(); 208 nbBytesRead += 2; 209 210 stringAsBytes = new byte[offset]; 211 stream.read(stringAsBytes); 212 fileName = new String (stringAsBytes,CHAR_ENC); 213 nbBytesRead += offset; 214 System.out.println(" extract file " + fileName); 215 216 fSize = new Long (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 try { 227 228 FileOutputStream fos = new FileOutputStream (fileName); 229 230 fos.write (aFile); 231 232 fos.flush(); 234 fos.close(); 235 } 236 catch (FileNotFoundException ex) { 237 System.out.println ("ERROR : Could not create the encrypted file"); 238 return; 239 } 240 catch (SecurityException ex) { 241 System.out.println ("ERROR : No security permissions to create the file."); 242 return; 243 } 244 catch (IOException ex) { 245 System.out.println ("ERROR : I/O error."); 246 return; 247 } 248 249 251 } 252 253 } 254 catch (IOException ex) { 255 return; 257 } 258 catch (Exception e) { 259 System.out.println ("ERROR : " + e.getMessage()); 260 return; 261 } 262 } 263 264 265 private static void DisplayBytes (byte[] bytes) 267 { 268 270 for (int i=0; i<bytes.length; i++) { 271 } 273 274 } 276 277 278 private static void DisplayHelp () 280 { 281 StringBuffer buffer = new StringBuffer (); 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 303 private static boolean fileExists(String path){ 304 305 File tmpFile = new File (path); 306 if ( tmpFile != null && tmpFile.isFile() ){ 307 String name = tmpFile.getName(); 308 if ( tmpFile.getParentFile() != null ){ 309 File [] 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 |