Decrypt


Your browser is ignoring the <APPLET> tag!

This program uses a brute force approach to try all possible keys with a Caesar cipher in an attempt to decrypt some ciphertext. The decryptions are displayed to the user, who must look at them to find the one that is plaintext.

The output of this program goes into the browser's Java Console.

import objectdraw.*;

/**
 * @author Barbara Lerner
 * @version April 7, 2008
 *
 * Uses a shift cipher to produce all possible decryptions of some encrypted text.
 * User needs to figure out which is the actual decryption because that is probably
 * the only one that looks like words.
 */
public class Decrypt extends WindowController {
    // The highest character value 
    private static final char MAX_CHAR = '~';
    
    // The lowest character value
    private static final char MIN_CHAR = ' ';
    
    // The range of characters 
    private static final int RANGE = MAX_CHAR - MIN_CHAR + 1;
    
    // The maximum shift
    private static final int MAX_KEY = RANGE - 1; 
    
    // The letter substitutions
    private char[] substitutions = new char[RANGE];
    
    /**
     * Display the original text and the encrypted text.
     */
    public void begin () {
        resize (400, 600);
        String unencryptedText = "You can bomb the world to pieces; you can't bomb it into peace.";
        String encryptedText = encrypt (unencryptedText);
        System.out.println ("Encrypted text:  " + encryptedText);
        System.out.println ("Possible decrypted text:");
        for (int key = 1; key < MAX_KEY; key++) {
            System.out.println ("Key " + key + ":  " + decode (encryptedText, key));
        }
    }
    
    /**
     * Pick a random key and encrypt the text
     * @param unencryptedText the text to encrypt
     * @return the encrypted text
     */
    private String encrypt(String unencryptedText) {
        int key = new RandomIntGenerator (1, MAX_KEY).nextValue();
        System.out.println ("Using key " + key);
        return encode (unencryptedText, key);
    }
        
    private void createSubstitutionArray (int key) {
        // Create the substitutions array for this key
        char c = MIN_CHAR;
        
        for (int i = RANGE - key; i < RANGE; i++) {
            substitutions[i] = c;
            c++;
        }
        for (int i = 0; i < RANGE - key; i++) {
            substitutions[i] = c;
            c++;
        }
        for (int i = 0; i < 3; i++) {
            System.out.print (substitutions[i] + " ");
        }
        System.out.println();
    }
    
    /**
     * Encrypt the string using the Caesar cipher.  This shifts the characters by
     * 2 in the alphabet
     *
     * @param text the text to encrypt
     * @return the encrypted text
     */
    private String encode(String text, int key) {
        createSubstitutionArray (key);
        
        String encodedText = "";
        for (int i = 0; i < text.length(); i++) {
            char nextChar = text.charAt(i);
            char nextEncodedChar = substitutions[nextChar - MIN_CHAR];
            encodedText = encodedText + nextEncodedChar;
        }
        return encodedText;
    }

    /**
     * Encrypt the string using the Caesar cipher.  This shifts the characters by
     * 2 in the alphabet
     *
     * @param text the text to encrypt
     * @return the encrypted text
     */
    private String decode(String text, int key) {
        return encode (text, MAX_KEY - key);
    }

}