| 3 | import java.util.Scanner; |
| 4 | |
| 5 | public class CeaserCipher { |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println(""" |
| 8 | -------------------------------- |
| 9 | CEASER CIPHER ENCRYPT/ DECRYPT |
| 10 | -------------------------------- |
| 11 | """); |
| 12 | Scanner sc = new Scanner(System.in); |
| 13 | System.out.println("Encrypt or Decrypt:\n 1- Encryption\n 2- Decryption"); |
| 14 | int type = sc.nextInt(); |
| 15 | for (int j = 1; j > 0; j++) { |
| 16 | if (type == 1) { |
| 17 | ceaserCipherEncryption(); |
| 18 | break; |
| 19 | } else if (type == 2) { |
| 20 | ceaserCipherDecryption(); |
| 21 | break; |
| 22 | } else { |
| 23 | System.out.print("Please enter a valid number (1 or 2): "); |
| 24 | type = sc.nextInt(); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | public static void ceaserCipherEncryption() { |
| 29 | Scanner textInput = new Scanner(System.in); |
| 30 | System.out.print("PlainText: "); |
| 31 | String textToEncrypt = textInput.nextLine(); |
| 32 | System.out.print("shifting key: "); |
| 33 | int keyToUse = textInput.nextInt(); |
| 34 | String cipher = ""; |
| 35 | for (int i = 0; true; i++) { |
| 36 | if (keyToUse > 50 && keyToUse <= 0) { |
| 37 | System.out.print("Please Enter key value <= 50: "); |
| 38 | keyToUse = textInput.nextInt(); |
| 39 | } else { |
| 40 | for (int j = 0; j < textToEncrypt.length(); j++) { |
| 41 | char temp = textToEncrypt.charAt(j); |
| 42 | if (temp >= 'A' && temp <= 'Z') { |
| 43 | temp = (char) (temp + keyToUse); |
| 44 | if (temp > 'Z') { // go back to A in Ascii |
| 45 | temp = (char) (temp + 'A' - 'Z' - 1); |
| 46 | |
| 47 | } |
| 48 | cipher += temp; |
| 49 | } else if (temp >= 'a' && temp <= 'z') { |
| 50 | temp = (char) (temp + keyToUse); |
| 51 | if (temp > 'z') { // go back to a in Ascii |
| 52 | temp = (char) (temp + 'a' - 'z' - 1); |
| 53 | |
| 54 | } |
| 55 | cipher += temp; |
| 56 | } else |
| 57 | cipher += temp; |
| 58 | |
| 59 | } |
| 60 | } |
| 61 | System.out.println("Enrypted text is: " + cipher); |
| 62 | break; |
nothing calls this directly
no outgoing calls
no test coverage detected