📄 extractjef.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// 08.06.2001 NK faster by using buffer of bytes to read data////package org.jahia.tools;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.DataInputStream;import java.io.IOException;import java.io.File;import java.io.EOFException;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.zip.CRC32;import java.security.*;import java.security.spec.*;import javax.crypto.*;import javax.crypto.spec.*;/** * Used to extract a jef file. * * @deprecated ReadJEF with -e do the same !!! * @see org.jahia.tools.ReadJEF * @version 1.0 */public class ExtractJEF{ /** the help option **/ private static final String HELP_OPTION = "-help"; private static final String CHAR_ENC = "UTF-16"; //------------------------------------------------------------------------- public static void main (String args[]) { System.out.println ("\nJahia Encrypted File Extractor, version 1.0"); System.out.println ("(C) Jahia Ltd 2002\n\n"); // The source file String srcFileName = ""; /** * Retrieve command line arguments */ // if no parameters are specified, display the tool usage. if ((args.length > 0) && (HELP_OPTION.equals (args[0]))) { DisplayHelp (); return; } srcFileName = args[0]; // check if the file exists or not File srcFile = new File(srcFileName); if ( !fileExists(srcFile.getAbsolutePath()) ){ System.out.println ("Error: Source file "+srcFileName+ " not found !"); return; } try { /** * Decrypt data */ FileInputStream fstream = new FileInputStream (srcFile); DataInputStream stream = new DataInputStream (fstream); // create the raw byte stream from the file size by removing the // CRC32 checksum bytes ( long = 8 bytes ) ,the offset ( int = 4 bytes ) // and the Cipher secret key ( int = 4 bytes ) int streamSize = (new Long(srcFile.length() - 16)).intValue(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buff = null; int crcOffset = stream.readInt (); //System.out.println (" offset " + crcOffset); // read the first part of data buff = new byte[crcOffset]; stream.read(buff,0,crcOffset); bos.write(buff); // int the Cipher Security.addProvider( new com.sun.crypto.provider.SunJCE()); // get the secret key int keyLength = stream.readInt(); byte[] keyAsBytes = new byte[keyLength]; stream.read(keyAsBytes); DESKeySpec keySpec = new DESKeySpec(keyAsBytes); Key myKey = SecretKeyFactory.getInstance("DES").generateSecret(keySpec); keyAsBytes = myKey.getEncoded(); System.out.println (" Cipher secret key = "+ new String(keyAsBytes) ); Cipher decCipher = Cipher.getInstance("DES"); decCipher.init(Cipher.DECRYPT_MODE, myKey); long storedChecksum = stream.readLong (); System.out.println (" stored checksum = "+Long.toHexString (storedChecksum)); // read the remaining part of data buff = new byte[streamSize-(crcOffset+keyLength)]; stream.read(buff,0,buff.length); bos.write(buff); byte[] bytes = bos.toByteArray(); // free memory of unused objects fstream.close(); stream = null; fstream = null; srcFile = null; bos.close(); bos = null; //DisplayBytes (bytes); // compute the stream CRC CRC32 crc = new CRC32(); crc.update (bytes); long streamChecksum = crc.getValue (); System.out.println (" stream checksum = "+Long.toHexString (streamChecksum)+ "\n"); //System.out.println(" data stream length before Cipher decryption " + bytes.length ); byte[] decryptedBytes = decCipher.doFinal(bytes); bytes = null; //System.out.println(" data stream length after Cipher decryption " + decryptedBytes.length ); ByteArrayInputStream byteStream = new ByteArrayInputStream (decryptedBytes); stream = new DataInputStream (byteStream); short offset = 0; System.out.println (" Jahia Encrypted File Info :"); byte[] stringAsBytes = null; // extract keys value offset = stream.readShort (); //System.out.println ("keys nb char: " + offset); if ( offset>0 ){ stringAsBytes = new byte[offset]; stream.read(stringAsBytes); String keys = new String(stringAsBytes,CHAR_ENC); System.out.println (" keys : " + keys + "\n"); } else { System.out.println (" no keys values\n"); } //Extract the file content byte[] data = new byte[decryptedBytes.length-(offset+2)]; stream.read(data,0,data.length); // extract the encrypted files byteStream = new ByteArrayInputStream (data); stream = new DataInputStream (byteStream); int nbBytes = data.length; //System.out.println(" files total bytes " + nbBytes + "\n"); int nbBytesRead = 0; offset = 0; String fileName = ""; Long fSize = null; byte[] aFile = null; while( nbBytesRead<nbBytes ){ // extract the filename offset = stream.readShort(); nbBytesRead += 2; stringAsBytes = new byte[offset]; stream.read(stringAsBytes); fileName = new String(stringAsBytes,CHAR_ENC); nbBytesRead += offset; System.out.println(" extract file " + fileName); // extract the file size fSize = new Long(stream.readLong()); aFile = new byte[fSize.intValue()]; nbBytesRead += 8; stream.read(aFile,0,fSize.intValue()); nbBytesRead += fSize.intValue(); // write out the original file. try { FileOutputStream fos = new FileOutputStream (fileName); fos.write (aFile); // flush the stream into the file and close the file fos.flush(); fos.close(); } catch (FileNotFoundException ex) { System.out.println ("ERROR : Could not create the encrypted file"); return; } catch (SecurityException ex) { System.out.println ("ERROR : No security permissions to create the file."); return; } catch (IOException ex) { System.out.println ("ERROR : I/O error."); return; } //System.out.println(" Nb bytes read " + nbBytesRead + "\n"); } } catch (IOException ex) { //System.out.println ("ERROR : I/O exception while reading the file."); return; } catch (Exception e) { System.out.println ("ERROR : " + e.getMessage()); return; } } //------------------------------------------------------------------------- private static void DisplayBytes (byte[] bytes) { //System.out.print (" stream = "); for (int i=0; i<bytes.length; i++) { //System.out.print (bytes[i]+" "); } //System.out.print("\n"); } //------------------------------------------------------------------------- private static void DisplayHelp () { StringBuffer buffer = new StringBuffer (); buffer.append (" usage : ExtractEPF [options] myFile\n\n"); buffer.append (" options :\n"); buffer.append (" source file \n"+ " the source file name)\n"); buffer.append ("\n\n"); System.out.println (buffer.toString()); } //------------------------------------------------------------------------- /** * check if a file or directory exists. The check is case sensitive * * @author NK * @param String the absolute path * @return boolean true if comparison is success */ private static boolean fileExists(String path){ File tmpFile = new File(path); if ( tmpFile != null && tmpFile.isFile() ){ String name = tmpFile.getName(); if ( tmpFile.getParentFile() != null ){ File[] files = tmpFile.getParentFile().listFiles(); int nbFiles = files.length; for (int i=0 ; i<nbFiles ; i++){ if ( files[i].getName().equals(name) ){ return true; } } } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -