TizzyT

Programming Decipher Java Alternate Src -TizzyT

Feb 20th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. package decipher;
  2. import java.util.Scanner; // << IMPORT THIS
  3.  
  4. public class Decipher {
  5.     public static Scanner Input = new Scanner(System.in); // <<< ADD THIS
  6.     public static void main(String[] args) {
  7.         // Encrypted message we have to decrypt
  8.         char[] Message = Input.nextLine().toCharArray();  // <<< CHANGE THIS
  9.         // For loop to bruteforce the cipher and find a valid decryption key (key space = 0 to 100)
  10.         for (Byte i = 0; i <= 100; i++)
  11.         {
  12.             // Outputs the current attepted decryption using the key (i)
  13.             System.out.println("Using Key " + String.format("%3d", i) + ": " + Encrypt(i, Message));
  14.         }
  15.     }
  16.    
  17.     private static String Encrypt(byte Key, char[] Message)
  18.     {
  19.         // String used to store the individual characters being decrypted
  20.         String EncryptedMessage = "";
  21.         // for each character in the encrypted message attempt to decrypt using the Key
  22.         for (char L: Message)
  23.         {
  24.             // Check whether or not character with the added key is greater than 126, We need
  25.             // this check because your typical characters are under 126 (127 is the DEL control character)
  26.             if (L + Key > 126)
  27.             {
  28.                 // If the character added with the key is greater than 126 then increase it by
  29.                 // 32 and then minus 127 and append the character at that index 32 comes from
  30.                 // the fact that on the ascii table indexes 0 to 31 (inclusive) are all control
  31.                 // characters we minus 127 so that the resulting number reverts back to within
  32.                 // the range of valid typical characters
  33.                 EncryptedMessage += (char)(32 + (L + Key) - 127);
  34.             }
  35.             else
  36.             {
  37.                 // If the character added with the key isnt greater than 126 that means the
  38.                 // result is within the valid character range and append that character
  39.                 EncryptedMessage += (char)(L + Key);
  40.             }
  41.         }
  42.         // returns the decryption attempt
  43.         return EncryptedMessage;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment