Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package decipher;
- import java.util.Scanner; // << IMPORT THIS
- public class Decipher {
- public static Scanner Input = new Scanner(System.in); // <<< ADD THIS
- public static void main(String[] args) {
- // Encrypted message we have to decrypt
- char[] Message = Input.nextLine().toCharArray(); // <<< CHANGE THIS
- // For loop to bruteforce the cipher and find a valid decryption key (key space = 0 to 100)
- for (Byte i = 0; i <= 100; i++)
- {
- // Outputs the current attepted decryption using the key (i)
- System.out.println("Using Key " + String.format("%3d", i) + ": " + Encrypt(i, Message));
- }
- }
- private static String Encrypt(byte Key, char[] Message)
- {
- // String used to store the individual characters being decrypted
- String EncryptedMessage = "";
- // for each character in the encrypted message attempt to decrypt using the Key
- for (char L: Message)
- {
- // Check whether or not character with the added key is greater than 126, We need
- // this check because your typical characters are under 126 (127 is the DEL control character)
- if (L + Key > 126)
- {
- // If the character added with the key is greater than 126 then increase it by
- // 32 and then minus 127 and append the character at that index 32 comes from
- // the fact that on the ascii table indexes 0 to 31 (inclusive) are all control
- // characters we minus 127 so that the resulting number reverts back to within
- // the range of valid typical characters
- EncryptedMessage += (char)(32 + (L + Key) - 127);
- }
- else
- {
- // If the character added with the key isnt greater than 126 that means the
- // result is within the valid character range and append that character
- EncryptedMessage += (char)(L + Key);
- }
- }
- // returns the decryption attempt
- return EncryptedMessage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment