Cryptogram


Your browser is ignoring the <APPLET> tag!

This program generates randomly shuffles characters to create a character substitution key. It then encrypts a string and displays it to the user. When the user clicks the mouse, the decrypted text is displayed.

import objectdraw.*;
import java.awt.*;
/**
 * Create a cryptogram for a string.
 * 
 * @author Barbara Lerner
 * @version April 7, 2008
 */
public class Cryptogram extends WindowController
{
    // Number of letters in the alphabet
    private static final int NUM_LETTERS = 26;
    
    // Which letters to substitute for the original letters.
    private char[] substitutions = new char[NUM_LETTERS];
    
    // The text before encryption
    private String unencryptedText;
    
    // Fixed width font so that unencrypted and encrypted text lines up nicely
    private Font textFont = new Font ("Courier", Font.PLAIN, 18);
    
    /**
     * Create and display a cryptogram
     */
    public void begin () {
        resize (900, 200);
        // Initialize the array to hold the characters A - Z
        char c = 'A';
        for (int i = 0; i < NUM_LETTERS; i++) {
            substitutions [i] = c;
            c++;
        }
        
        // Shuffle the order of the letters in the array randomly
        shuffle();
        
        // Create the cryptogram by replacing the letters in the original phrase
        // with the randomly ordered substitutions
        substitute();
    }
    
    /**
     * Randomly rearrange the order of the letters in the substitution array
     */
    private void shuffle() {
        RandomIntGenerator randomIndexGen = new RandomIntGenerator (0, NUM_LETTERS - 1);
        
        // Do 100 random swaps of letters
        for (int i = 0; i < 100; i++) {
            // Pick 2 array entries to swap
            int firstIndex = randomIndexGen.nextValue();
            int secondIndex = randomIndexGen.nextValue();
            
            // Swap the entries
            char temp = substitutions[firstIndex];
            substitutions[firstIndex] = substitutions[secondIndex];
            substitutions[secondIndex] = temp;
        }
    }
    
    /**
     * Encrypt a phrase using the substitutions array
     * @param unencryptedText the original text
     * @return the cryptogram
     */
    private String encrypt (String unencryptedText) {
        // Initially the cryptogram is empty
        String encryptedText = "";
        
        // Visit each character in the original phrase and encrypt it.
        for (int i = 0; i < unencryptedText.length(); i++) {
            char nextUnencryptedChar = unencryptedText.charAt (i);
            char nextEncryptedChar;
            
            // If the character is a capital letter, use the substitutions array to pick
            // its replacement.
            if (nextUnencryptedChar >= 'A' && nextUnencryptedChar <= 'Z') {
                nextEncryptedChar = substitutions[nextUnencryptedChar - 'A'];
            }
            
            // Not a capital letter.  Don't change the character
            else {
                nextEncryptedChar = nextUnencryptedChar;
            }
            
            // Add this newly-encrypted letter to the end of the cryptogram.
            encryptedText = encryptedText + nextEncryptedChar;
        }
        return encryptedText;
    }
    
    /**
     * Display the unencrypted text
     */
    public void onMouseClick (Location point) {
        new Text (unencryptedText, 20, 75, canvas).setFont(textFont);
    }
    
    /**
     * Do the substitutions to create the cryptogram.
     */
    private void substitute() {
        unencryptedText = "MT. HOLYOKE, A WOMEN'S SCHOOL WITHOUT BOYS, NOT A GIRLS' SCHOOL WITHOUT MEN";
        new Text (encrypt (unencryptedText), 20, 50, canvas).setFont (textFont);
    }
}