// Zorah Fung, CSE 142 // Prompts a user for a message and a "key" and encrypts the message using // a Caesar cipher and the key as the shift value. import java.util.*; public class CaesarCipher { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Your secret message: "); String message = console.nextLine(); message = message.toLowerCase(); System.out.print("Your secret key: "); int key = console.nextInt(); System.out.print("Your encoded message is: "); encode(message, key); } // Encodes the given message using the key as the shift value public static void encode(String message, int key) { for (int i = 0; i < message.length(); i++) { char c = message.charAt(i); if (c == ' ') { System.out.print(" "); } else { System.out.print((char) (c + key)); } } System.out.println(); } // We can also write a decode method, which shifts each letter back // to its original letter, just by using subtraction! public static void decode(String message, int key) { for (int i = 0; i < message.length(); i++) { char c = message.charAt(i); if (c == ' ') { System.out.print(" "); } else { System.out.print((char) (c - key)); } } System.out.println(); } }