/** Encrypt and decrypt data using the Caesar cipher. * For the purpose of this simple program, it's not necessary to keep * around both the plaintext and ciphertext Strings, but if we enhance * the program, they may be useful. * * The 'key' attribute is the number we need to add to each letter to * encrypt it. */ public class Caesar implements Encryption { // private String plaintext; // private String ciphertext; private static int key = 7; /** encrypt -- Go through each character of the string and add the key * value. Note that the cast is needed because the + operator returns * an integer result. */ public void encrypt(String plaintext) { String ciphertext = ""; for(int i = 0; i < plaintext.length(); ++i) ciphertext += (char) (plaintext.charAt(i) + key); System.out.println(ciphertext); } /** decrypt -- this is exactly the inverse of encrypt */ public void decrypt(String ciphertext) { String plaintext = ""; for (int i = 0; i < ciphertext.length(); ++i) plaintext += (char) (ciphertext.charAt(i) - key); System.out.println(plaintext); } }