tmck-code
1/10/2015 - 12:57 PM

Java implementations of simple substitution ciphers

Java implementations of simple substitution ciphers

/*
 * Class Name:    Vigenere
 *
 * Author:        Thomas McKeesick
 * Creation Date: Wednesday, September 24 2014, 14:33 
 * Last Modified:     Saturday, January 10 2015, 23:24
 * 
 * Class Description: Java implementation of the Vigenere cipher
 *
 */

import java.lang.Character;
import java.util.Scanner;

public class Vigenere {

    /** Variable representing the number of letters in the English Alphabet  */
    public static final int ALPHABET_SIZE = 26;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("**********************************************");
        System.out.println("Welcome to the Vigenere decrypter\n" + 
                "Decrypts an encoded message with a key");
        System.out.println("**********************************************");
        System.out.println("Enter the encoded message: ");
        String message = keyboard.nextLine();
        System.out.println("Enter the key: ");
        String key = keyboard.nextLine();
        String decrypted = decrypt(message, key);
        System.out.println("DECRYPTED MESSGAE: " + decrypted);
    }

    /**
     * Decrypts a string via the Vigenere cipher
     * @param str The encrypted string
     * @param key The key to decode the encrypted string with
     * @return The decrypted string
     */
    public static String decrypt(String str, String key) {
        String decrypted = "";
        key = key.toUpperCase();
        int keyIndex = 0;
        
        for( char c : str.toUpperCase().toCharArray() ) {

            if(Character.isLetter(c)) {
                decrypted += 
                    (char) (
                        (c - key.charAt(keyIndex) + ALPHABET_SIZE) 
                        % ALPHABET_SIZE + 'A');
                keyIndex = ++keyIndex % key.length();
            } else {
                decrypted += c;
            }
        }
        return decrypted;
    }
}

This is the public gist for use primarily at tmck-code.net This contains the repository located at https://github.com/tmck-code/Ciphers

Contains Java implementations for the Atbash, Caesar & Vigenère ciphers.

/*
 * Class Name:    Caesar
 *
 * @author Thomas McKeesick
 * @version 1.1.0
 *
 * Author:        Thomas McKeesick
 * Creation Date: Sunday, September 21 2014, 16:50 
 * Last Modified:     Saturday, January 10 2015, 21:45
 * 
 * Class Description: This is a program that decodes a string
 *    via Caesar shifts. The program runs through shifts 1-25.
 */

import java.io.IOException;
import java.lang.String;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.Exception;

import java.util.ArrayList;

public class Caesar {
    
    /** Variable representing the number of letters in the English Alphabet. */
    private static final int ALPHABET_SIZE = 26;
    
    /**
     * Main method of the program, takes an input string from the user and
     * then decodes it, printing the output to the user.
     */
    public static void main(String[] args) {

        System.out.println("****************************************");
        System.out.println("Decrypts via caesar, tries all shifts");
        System.out.println("****************************************");
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter string to decrypt: ");
        String encrypted = keyboard.nextLine();
        System.out.println("Decrypted text: ");

        ArrayList<String> result = decrypt(encrypted);
        System.out.println(result);
   }

    /**
     * The decryption method, tries every Caesar shift from 1 - 25
     * @param decrypted
     * @return An ArrayList of Strings containing all possible solutions
     */
    public static ArrayList<String> decrypt( String encrypted ) {
        ArrayList<String> result = new ArrayList<String>();
		
        for(int shift = 0; shift < ALPHABET_SIZE; shift++) {
            String line = caesarEncrypt( encrypted, shift );
            result.add(line);
        }
        return result;
   }

    /**
     * The encryption method, moves every letter in the string along by the 
     * shift number provided
     * @param str The string to encrypt
     * @param shift The number that each letter is shifted by
     * @return The encrypted string
     */
    public static String caesarEncrypt( String str, int shift ) {
        StringBuilder encoded = new StringBuilder();

        for (char c : str.toUpperCase().toCharArray()) {
            if (Character.isLetter(c)) {
                char newChar = (char) (((c + shift) % ALPHABET_SIZE) + 'A' );
                encoded.append(newChar);
            } else {
                encoded.append(c);
            }
        }
        encoded.append("\n");
        return encoded.toString();
    }
}
/*
 * Class Name:    Atbash
 *
 * @author Thomas McKeesick
 * @version 1
 *
 * Creation Date: Wednesday, September 24 2014, 11:54 
 * Last Modified:     Friday, January 09 2015, 13:01
 * 
 * Class Description: This is program that decodes the
 *       Atbash cipher, a simple substitution where a letter's
 *       position in the alphabet is essentially mirrored.
 *       This class is not abstract as it is intended to be able
 *       to be used as a standalone program and also by others.
 */

import java.util.Scanner;

public class Atbash
{
    /**
     * Main method, simply accepts input from the user for decoding,
     * then prints the result to screen
     *
     * @param args The string to decode
     */
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("********************************\n" +
                           "Welcome to the Atbash decoder!\n" +
                           "********************************");
        System.out.print("Enter a message to decode: ");
        String message = keyboard.nextLine();
        System.out.println("\nDECRYPTED: " + decrypt(message).toUpperCase());
    }

    /**
     * Public static method to decrypt an Atbash-encoded message. This
     * method still adds non-alphabet characters to the final string,
     * in case the message is encoded with grammatical characters
     *
     * @param message The string to decrypt
     * @return The decrypted string in upper case
     */
    public static String decrypt( String message ) {
        StringBuilder decoded = new StringBuilder();

        for( char c : message.toUpperCase().toCharArray()) {
            if(Character.isLetter(c)) {
                int newChar = ('Z' - c) + 'A';
                decoded.append((char) newChar);
            } else {
                decoded.append(c);
            }
        }
        return decoded.toString();
    }
}