// Decrypt.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 Decrypt { public static void main( String[] command_line_parameters ) { if ( command_line_parameters.length == 2 ) { try { BufferedReader encrypted_file = new BufferedReader( new FileReader( command_line_parameters[ 0 ] ) ) ; FileOutputStream decrypted_file = new FileOutputStream( command_line_parameters[ 1 ] ) ; int number_of_blocks_decrypted = 0 ; byte[] decrypted_bytes = new byte[ 30 ] ; String encrypted_bytes_from_file = encrypted_file.readLine() ; while ( encrypted_bytes_from_file != null ) { int number_of_decrypted_bytes = encrypted_bytes_from_file.length() / 2 ; for ( int byte_index = 0 ; byte_index < number_of_decrypted_bytes ; byte_index ++ ) { decrypted_bytes[ byte_index ] = (byte) ( ( encrypted_bytes_from_file.charAt( byte_index * 2 ) - 0x20 - byte_index ) + ( ( encrypted_bytes_from_file.charAt( byte_index * 2 + 1 ) - 0x20 - byte_index ) << 4 ) ) ; } decrypted_file.write( decrypted_bytes, 0, number_of_decrypted_bytes ) ; number_of_blocks_decrypted ++ ; encrypted_bytes_from_file = encrypted_file.readLine() ; } encrypted_file.close() ; decrypted_file.close() ; System.out.print( "\n " + number_of_blocks_decrypted + " blocks were decrypted. \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 Decrypt ecryptedfile.ext decryptedfile.ext\n") ; } } }