// Encrypt.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-09 File created. // 2005-11-19 Last modification. // The encryption/decryption algorithm used by this program is // explained in "A Natural Introduction to Computer Programming // with C++" // The best way to execute this program is to run it in a // command prompt window where command line parameters can // be given. It may be difficult, and even impossible, to // supply the command line parameters if this program is // executed with a modern development tool such as JCreator // or Eclipse. import java.util.* ; import java.io.* ; // Classes for file handling. class Encrypt { public static void main( String[] command_line_parameters ) { if ( command_line_parameters.length == 2 ) { try { FileInputStream original_file = new FileInputStream( command_line_parameters[ 0 ] ) ; PrintWriter encrypted_file = new PrintWriter( new FileWriter( command_line_parameters[ 1 ] ) ) ; int number_of_blocks_encrypted = 0 ; byte[] bytes_from_file = new byte[ 30 ] ; int number_of_bytes_read ; while (( number_of_bytes_read = original_file.read( bytes_from_file, 0, bytes_from_file.length )) > 0 ) { StringBuilder encrypted_bytes = new StringBuilder() ; for ( int byte_index = 0 ; byte_index < number_of_bytes_read ; byte_index ++ ) { encrypted_bytes.append( (char) ( ( bytes_from_file[ byte_index ] & 0xF ) + 0x20 + byte_index ) ) ; encrypted_bytes.append( (char) ( ( bytes_from_file[ byte_index ] >> 4 ) + 0x20 + byte_index ) ) ; } encrypted_file.println( encrypted_bytes ) ; number_of_blocks_encrypted ++ ; } original_file.close() ; encrypted_file.close() ; System.out.print( "\n " + number_of_blocks_encrypted + " blocks were encrypted. \n" ) ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print( "\n Cannot open file " + command_line_parameters[ 0 ] ) ; } catch ( Exception caught_exception ) { System.out.print( "\n File error. Probably cannot write to " + command_line_parameters[ 1 ] ) ; } } else { System.out.print( "\n You have to command this program as: \n" + "\n java Encrypt originalfile.ext ecryptedfile.ext\n") ; } } }