|
| 1 | +/* |
| 2 | + * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser |
| 3 | + * |
| 4 | + * Developed for use with the book: |
| 5 | + * |
| 6 | + * Data Structures and Algorithms in Java, Sixth Edition |
| 7 | + * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser |
| 8 | + * John Wiley & Sons, 2014 |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation, either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package dsaj.arrays; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | + |
| 27 | +/** Class for doing encryption and decryption using the Caesar Cipher. */ |
| 28 | +public class CaesarCipher { |
| 29 | + protected char[] encoder = new char[26]; // Encryption array |
| 30 | + protected char[] decoder = new char[26]; // Decryption array |
| 31 | + |
| 32 | + /** Constructor that initializes the encryption and decryption arrays */ |
| 33 | + public CaesarCipher(int rotation) { |
| 34 | + for (int k=0; k < 26; k++) { |
| 35 | + encoder[k] = (char) ('A' + (k + rotation) % 26); |
| 36 | + decoder[k] = (char) ('A' + (k - rotation + 26) % 26); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** Returns String representing encrypted message. */ |
| 41 | + public String encrypt(String message) { |
| 42 | + return transform(message, encoder); // use encoder array |
| 43 | + } |
| 44 | + |
| 45 | + /** Returns decrypted message given encrypted secret. */ |
| 46 | + public String decrypt(String secret) { |
| 47 | + return transform(secret, decoder); // use decoder array |
| 48 | + } |
| 49 | + |
| 50 | + /** Returns transformation of original String using given code. */ |
| 51 | + private String transform(String original, char[] code) { |
| 52 | + char[] msg = original.toCharArray(); |
| 53 | + for (int k=0; k < msg.length; k++) |
| 54 | + if (Character.isUpperCase(msg[k])) { // we have a letter to change |
| 55 | + int j = msg[k] - 'A'; // will be value from 0 to 25 |
| 56 | + msg[k] = code[j]; // replace the character |
| 57 | + } |
| 58 | + return new String(msg); |
| 59 | + } |
| 60 | + |
| 61 | + /** Simple main method for testing the Caesar cipher */ |
| 62 | + public static void main(String[] args) { |
| 63 | + CaesarCipher cipher = new CaesarCipher(3); |
| 64 | + System.out.println("Encryption code = " + new String(cipher.encoder)); |
| 65 | + System.out.println("Decryption code = " + new String(cipher.decoder)); |
| 66 | + String message = "THE EAGLE IS IN PLAY; MEET AT JOE'S."; |
| 67 | + String coded = cipher.encrypt(message); |
| 68 | + System.out.println("Secret: " + coded); |
| 69 | + String answer = cipher.decrypt(coded); |
| 70 | + System.out.println("Message: " + answer); // should be plaintext again |
| 71 | + } |
| 72 | +} |
0 commit comments