📄 makejef.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 use Cipher to encrypt each bytespackage org.jahia.tools;import java.io.*;import java.util.*;import java.util.zip.*;import java.security.*;import javax.crypto.*;/** * Command line class used to create Jahia Encrypted File. * * @author Khue Nguyen * @version 1.0 */public class MakeJEF{ /** the optional key values * -keys key1=val1,keys2=val2,keys3=val3 */ private static final String KEYS_OPTION = "-keys"; /** the output file name option **/ private static final String OUTPUT_FILE_OPTION = "-out"; /** the license type option **/ private static final String LICENSE_TYPE_OPTION = "-licenseType"; /** the license type relational comparator option **/ private static final String LICENSE_TYPE_RELCOMP_OPTION = "-relComp"; /** 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 Package File Generator, version 1.0"); System.out.println ("(C) Jahia Ltd 2002\n\n"); /********************************************************************** * Set default values */ // The source files Vector files = new Vector(); // The optional key values String keysString = ""; // The output file name String outPutFileName = ""; // The License Type int licenseType = -1; // default -1 = all type // The License Type Relational Comparator int relComp = 4; // <(1),<=(2),=(3),>=(4),>(5), match all(-1) boolean addDefLicenseType = true; boolean addDefRelComp = true; /********************************************************************** * 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 OUTPUT FILE NAME if (OUTPUT_FILE_OPTION.equals (args[index])) { index++; if (index <= args.length-1) { if ( !isAlphaValid(removeFileExtension(args[index])) ){ System.out.println ("Error: the output file name contains invalid caracters "); return; } outPutFileName = args[index]; index++; } else { System.out.println ("Error: option "+OUTPUT_FILE_OPTION+ " without value!"); return; } } else if ( (index <= args.length-1) && KEYS_OPTION.equals (args[index]) ) { // GET THE KEYS OPTION index++; if (index <= args.length-1) { if (keysString == null || keysString.equals("") ){ keysString = args[index]; } else { keysString +="," + args[index]; } if ( !checkKeys(keysString) ){ System.out.println ("Error: option "+KEYS_OPTION+ " invalid format!\n"); System.out.println (keysString); return; } } else { System.out.println ("Error: option "+KEYS_OPTION+ " without values!"); return; } index++; } else if ( (index <= args.length-1) && LICENSE_TYPE_OPTION.equals (args[index]) ) { // GET THE LICENSE TYPE OPTION index++; if (index <= args.length-1) { int val = parseInteger(LICENSE_TYPE_OPTION,args[index]); if ( val < -1 ){ val = -1; } else if ( val > 5 ){ System.out.println ("Error: option "+LICENSE_TYPE_OPTION+ " invalid format! 1(OpenJoda),2(ProJoda),-1(all)\n"); System.out.println (args[index]); return; } keysString = appendKey(keysString,"licenseType",Integer.toString(val)); if ( !checkKeys(keysString) ){ System.out.println ("Error: option "+LICENSE_TYPE_OPTION+ " invalid format! 1(OpenJoda),2(ProJoda),-1(all)\n"); System.out.println (args[index]); return; } addDefLicenseType = false; } else { System.out.println ("Error: option "+LICENSE_TYPE_OPTION+ " without values!"); return; } index++; } else if ( (index <= args.length-1) && LICENSE_TYPE_RELCOMP_OPTION.equals (args[index]) ) { // GET THE LICENSE TYPE RELATIONAL COMP OPTION index++; if (index <= args.length-1) { int val = parseInteger(LICENSE_TYPE_RELCOMP_OPTION,args[index]); if ( val < -1 ){ val = -1; } else if ( val > 5 ){ System.out.println ("Error: option "+LICENSE_TYPE_RELCOMP_OPTION+ " invalid format! <(1),<=(2),=(3),>=(4),>(5),-1(all)\n"); System.out.println (args[index]); return; } keysString = appendKey(keysString,"relComp",Integer.toString(val)); if ( !checkKeys(keysString) ){ System.out.println ("Error: option "+LICENSE_TYPE_RELCOMP_OPTION+ " invalid format!\n"); System.out.println (args[index]); return; } addDefRelComp = false; } else { System.out.println ("Error: option "+LICENSE_TYPE_RELCOMP_OPTION+ " without values!"); return; } index++; // ALL OTHER OPTION ARE ERRORS :o) } else { System.out.println ("Error: ["+args[index]+"] is not a valid option.\n"); return; } } // GET THE LAST ARGUMENTS AS SOURCE FILES while (index < args.length){ //System.out.println (" arg["+index+"] " + args[index]); String name = args[index]; //System.out.println (" source file " + name); if ( name == null || name.trim().equals("") ){ System.out.println ("Error: no source file specified "); return; } File f = new File(name); files.add(f); index++; } /********************************************************************** * Should we use default values or input values */ if ( addDefLicenseType ){ keysString = appendKey(keysString,"licenseType","-1"); } if ( addDefRelComp ){ keysString = appendKey(keysString,"relComp","4"); } if ( (outPutFileName == null) || outPutFileName.equals("") ){ outPutFileName = "untitled.jef"; } /********************************************************************** * files to encrypt check */ if ( files.size() == 0 ){ System.out.println ("Error: 0 Source file found !"); return; } // check if the file names are valid long filesLength = 0; File f = null; for ( int i=0 ; i<files.size() ; i++ ){ f = (File)files.get(i); if ( !f.isFile() || !f.canRead() ){ System.out.println ("Error: cannot access file " + f.getName() ); return; } if ( !isAlphaValid(removeFileExtension(f.getName())) ){ System.out.println ("Error: invalid file name " + f.getName() ); return; } filesLength += f.length(); } System.out.println ("Arguments details :"); System.out.println (" nb files = " + files.size()); if ( keysString != null ){ System.out.println (" keys = " + keysString); } /********************************************************************** * Compose data to encrypt : both keys and files */ // create the random generator Random random = new Random(); Long lVal = null; byte[] bytes = null; byte[] stringAsBytes = null; try { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream outputStream = new DataOutputStream (byteStream); // add the keys if ( keysString != null && !keysString.equals("") ){ stringAsBytes = keysString.getBytes(CHAR_ENC); outputStream.writeShort (stringAsBytes.length); outputStream.write(stringAsBytes); } else { outputStream.writeShort (0); // 0 key } // read files into bytes array for ( int i=0 ; i<files.size() ; i++ ){ f = (File)files.get(i); lVal = new Long(f.length()); bytes = new byte[lVal.intValue()]; System.out.println(" file " + f.getName() + ", length = " + bytes.length); //add the original filename stringAsBytes = f.getName().getBytes(CHAR_ENC); outputStream.writeShort (stringAsBytes.length); outputStream.write(stringAsBytes); outputStream.writeLong (f.length()); // the file length // read file content FileInputStream fis = new FileInputStream(f); bytes = new byte[1024]; int i2 = fis.read(bytes); while ( i2 != -1 ){ outputStream.write (bytes,0,i2); i2 = fis.read(bytes); } fis.close(); } bytes = byteStream.toByteArray(); outputStream = null; byteStream = null; } catch (IOException ex) { System.out.println ("ERROR : I/O error while reading source file."); return; } System.out.println(" data stream ( both keys + files contents ) length before encryption " + bytes.length); /********************************************************************** * Create the encryted raw byte stream */ try { /** * Init the Cipher we use to really encrypt each byte */ Security.addProvider( new com.sun.crypto.provider.SunJCE()); Cipher encCipher = Cipher.getInstance("DES");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -