TizzyT

Programming Decipher Java Src -TizzyT

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