| 66 | |
| 67 | |
| 68 | public static void ceaserCipherDecryption() { |
| 69 | Scanner textInput = new Scanner(System.in); |
| 70 | System.out.print("Encrypted Text: "); |
| 71 | String textToEncrypt = textInput.nextLine(); |
| 72 | System.out.print("shifting key: "); |
| 73 | int keyToUse = textInput.nextInt(); |
| 74 | String cipher = ""; |
| 75 | for (int i = 0; true; i++) { |
| 76 | if (keyToUse > 50 && keyToUse <= 0) { |
| 77 | System.out.print("Please Enter key value <= 50: "); |
| 78 | keyToUse = textInput.nextInt(); |
| 79 | } else { |
| 80 | for (int j = 0; j < textToEncrypt.length(); j++) { |
| 81 | char temp = textToEncrypt.charAt(j); |
| 82 | if (temp >= 'A' && temp <= 'Z') { |
| 83 | temp = (char) (temp - keyToUse); |
| 84 | if (temp < 'A') { // go back to A in Ascii |
| 85 | temp = (char) (temp - 'A' + 'Z' + 1); |
| 86 | |
| 87 | } |
| 88 | cipher += temp; |
| 89 | } else if (temp >= 'a' && temp <= 'z') { |
| 90 | temp = (char) (temp - keyToUse); |
| 91 | if (temp < 'a') { // go back to a in Ascii |
| 92 | temp = (char) (temp - 'a' + 'z' + 1); |
| 93 | } |
| 94 | cipher += temp; |
| 95 | } else |
| 96 | cipher += temp; |
| 97 | } |
| 98 | } |
| 99 | System.out.println("Decrypted text is: " + cipher); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } |