📄 makejef.java
字号:
KeyGenerator keyG = KeyGenerator.getInstance("DES"); // this is the secret key use to encrypt/decrypt data with Cipher Key myKey = keyG.generateKey(); byte[] keyAsBytes = myKey.getEncoded(); System.out.println (" Cipher secret key = "+ new String(keyAsBytes) ); // set the Cipher object to encrypt mode encCipher.init(Cipher.ENCRYPT_MODE, myKey); byte[] encryptedBytes = encCipher.doFinal(bytes); System.out.println(" data stream length after Cipher encryption " + encryptedBytes.length); // compute the stream CRC CRC32 crc = new CRC32(); crc.update (encryptedBytes); long checksum = crc.getValue (); System.out.println (" CRC32 checksum = "+Long.toHexString (checksum)); /** * Compose the raw byte stream with encrypted data and checksum */ ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream outputStream = new DataOutputStream (byteStream); // add the CRC into the stream at a random position int offset = random.nextInt (encryptedBytes.length); System.out.println (" offset " + offset); outputStream.writeInt (offset); // write the first part of the original byte stream outputStream.write (encryptedBytes, 0, offset); // write the secret key length outputStream.writeInt (keyAsBytes.length); // write the secret key outputStream.write (keyAsBytes); // write the CRC outputStream.writeLong (checksum); // Write the remaining part of the original byte stream. outputStream.write (encryptedBytes, offset, encryptedBytes.length-offset); bytes = null; encryptedBytes = null; bytes = byteStream.toByteArray(); outputStream = null; byteStream = null; } catch (IOException ex) { System.out.println ("ERROR : I/O error while writing to byte stream."); return; } catch (Exception e) { System.out.println ("ERROR : " + e.getMessage()) ; return; } //DisplayBytes (bytes); System.out.println (" raw byte stream size = "+bytes.length); /********************************************************************** * Write the stream to the output file. */ try { // try to create the encrypted file File outFile = new File(outPutFileName); outFile.createNewFile(); FileOutputStream fos = new FileOutputStream (outFile); fos.write (bytes); // 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 ("Encrypted Package File successfully created.\n"); } //------------------------------------------------------------------------- /** * Check if the keys are valid * -keys key1=val1,key2=val2,key3=val3 * */ private static boolean checkKeys(String keysString){ if ( keysString == null || keysString.trim().equals("") ){ return false; } String[] tokens = getTokens(keysString,","); for ( int i=0 ; i<tokens.length ; i++ ){ if ( (tokens[i].indexOf("=") == -1) || tokens[i].indexOf("=") == (tokens[i].length()-1) ){ return false; } } return true; } //------------------------------------------------------------------------- /** * Append a key to the keys String * Keys String = key1=val1,key2=val2,key3=val3 * */ private static String appendKey(String keysString, String keyName, String keyVal){ if ( keysString == null || keysString.equals("") ){ keysString = keyName + "=" + keyVal ; } else { keysString +="," + keyName + "=" + keyVal; } return keysString; } //------------------------------------------------------------------------- private static void addJunkBytes (DataOutputStream stream, Random random) throws IOException { int amount = random.nextInt (200); // add between 0-199 junk bytes // add one to avoid 0 junk bytes. amount++; // add the offset in the stream stream.writeInt (amount); // add the junk bytes in the stream for (int i=0; i<amount; i++) { stream.writeByte (random.nextInt (255)); } } //------------------------------------------------------------------------- private static void DisplayHelp () { StringBuffer buffer = new StringBuffer (); buffer.append (" usage : MakeJEF [options] source files\n\n"); buffer.append (" options :\n"); buffer.append (" "+OUTPUT_FILE_OPTION+"\n"+ " the output file name \n"+ " default[untitled.jef]\n"); buffer.append (" "+LICENSE_TYPE_OPTION+"\n"+ " the license type\n"+ " default[-1] 1 Standard,2 Professional ,3 Enterprise, -1 All \n"); buffer.append (" "+LICENSE_TYPE_RELCOMP_OPTION+"\n"+ " the license type relational comparator\n"+ " default[4] 1 < ,2 <= ,3 = ,4 >= ,5 > ,-1 all \n"); buffer.append (" "+KEYS_OPTION+"\n"+ " the optinal values to encrypt\n"+ " key1=val1,key2=val2,key3=val3\n\n"); buffer.append (" finally [the list of source files to encrypt separated by ,]\n"); buffer.append (" or *.* or *.ext\n"); buffer.append ("\n\n"); System.out.println (buffer.toString()); } //------------------------------------------------------------------------- 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 int parseInteger (String option, String argument) { int item = 0; try { item = Integer.parseInt (argument); if ((item < 0) && (item != -1)) { System.out.println (argument + " is an invalid value for option [" + option + "]"); System.exit(0); } } catch (NumberFormatException ex) { System.out.println ("Error: option "+ option + " has not a valid integer value!"); System.exit(0); } return item; } //------------------------------------------------------------------------- private static double parseDouble (String option, String argument) { double item = 0; try { item = Double.parseDouble (argument); if ((item < 0.0) && (item != -1.0)) { System.out.println (argument + " is an invalid value for option [" + option + "]"); System.exit(0); } } catch (NumberFormatException ex) { System.out.println ("Error: option "+ option + " has not a valid float value!"); System.exit(0); } return item; } //------------------------------------------------------------------------- /** * 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; } //------------------------------------------------------------------------- /** * Return the file name but without the extension * * @author NK * @param (String) filename , the complete file name with extension * @param (String) ext , the extension to remove * @return(String) the filename without a gived extension */ private static String removeFileExtension(String filename){ String name = filename.toLowerCase(); int pos = -1; pos = filename.lastIndexOf(".") ; if ( pos != -1 ){ name = filename.substring(0,pos); removeFileExtension(name); } return name; } //------------------------------------------------------------------------- /** * Check if the String passed in parameter is Alpha valid. * * @author FH */ private static boolean isAlphaValid(String name) { if (name == null) { return false; } if (name.length() == 0) { return false; } String authorizedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789"; char[] chars = authorizedCharacters.toCharArray(); char[] nameBuffer = name.toCharArray(); boolean badCharFound = false; int i = 0; while ((i < nameBuffer.length) && (!badCharFound)) { int j = 0; boolean ok = false; while ((j < chars.length) && (!ok)) { if (chars[j] == nameBuffer[i]) { ok = true; } j++; } badCharFound = (!ok); if (badCharFound) { System.out.println("--/ Bad character found in ["+name+"] at position "+Integer.toString(i)); } i++; } return (!badCharFound); } // end isAlphaValid //------------------------------------------------------------------------- /** * Method getTokens : return an arrays of String tokens * * @param str the string to parse * @param sep the separator * @return an array of string values * @author NK */ static private String[] getTokens(String str, String sep) { if (str==null){ return null; } StringTokenizer st = new StringTokenizer(str,sep); String[] result = new String[st.countTokens()]; int count = 0; while ( st.hasMoreTokens() ){ result[count] = st.nextToken(); count++; } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -