⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readjef.java

📁 java 写的一个新闻发布系统
💻 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// 29.06.2001 NK added option -e to force extract.////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 read data in a jef file without extracting it. * * @version 1.0 */public class ReadJEF{    /** the help option **/    private static final String HELP_OPTION = "-help";    private static final String CHAR_ENC = "UTF-16";    /** the extract option **/    private static final String EXTRACT_OPTION 				= "-e";    //-------------------------------------------------------------------------    public static void main (String args[])    {        System.out.println ("\nJahia Encrypted File Reader, version 1.0");        System.out.println ("(C) Jahia Ltd 2002\n\n");		// The source file		String srcFileName = "";		// by default, do not extract, just read		boolean extract = false;				/**		 * Retrieve command line arguments		 */        // if no parameters are specified, display the tool usage.        if ( (args.length==0) || (args.length > 0) && (HELP_OPTION.equals (args[0]))) {            DisplayHelp ();            return;        }        // parse the parameters        int index = 0;        while ( (args[index]).startsWith("-") ){        		        // GET THE EXTRACT OPTION	        if (EXTRACT_OPTION.equals (args[index]))	        {	            index++;				extract = true;            // ALL OTHER OPTION ARE ERRORS :o)            } else {                System.out.println ("Error: ["+args[index]+"] is not a valid option.\n");                return;            }		}            				// GET THE FILE NAME        srcFileName = args[index];                // 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];			System.out.println("key length=" + 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");						crc = null;						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;		        if ( !extract ){					System.out.println("  read encrypted file " + fileName);				} else {					System.out.println("  extract encrypted 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();		        if ( extract ){			        // 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;			        }				}			}        }        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 : ReadEPF [options] myfile\n\n");        buffer.append ("    options :\n");        buffer.append ("      "+EXTRACT_OPTION+"\n"+                       "        read and extract \n"+                       "        default[read only]\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 + -