by Neal R. Wagner
Copyright © 2001 by Neal R. Wagner. All rights reserved.
NOTE: This site is obsolete. See book draft (in PDF):
Here is a Java program to create standard cryptograms, as they are found in newspapers. The program will read the quotation to be scrambled into a cryptogram from the standard input. In Unix this file can just be directed into the program, as shown in the commands below. Each time it is executed, the program will create a new and unique translation table to create the cryptogram. The resulting table and cryptogram itself are output on the standard output file, which might be redirected into a real file.
// Cryptogram: create a cryptogram as in a newspaper
import java.io.*;
public class Cryptogram {
private char[] alf = new char[26]; // translation vector
public Cryptogram() {
for (int i = 0; i < alf.length; i++) alf[i] = (char)('A' + i);
randomize();
}
private int rand(int r, int s) { // r <= rand <= s
return (int)((s - r + 1)*Math.random() + r);
}
private void randomize() {
for (int i = 0; i < alf.length - 1; i++) {
// Note: for a random permutation, replace "i+1" by "i" below
// However, we want no letter to remain in its original spot
int ind = rand(i+1, alf.length - 1);
char t = alf[i];
alf[i] = alf[ind];
alf[ind] = t;
}
}
public void printArray() {
System.out.print("Alphabet: ");
for (int i = 0; i < alf.length; i++)
System.out.print((char)('A' + i));
System.out.println();
System.out.print("Translated to: ");
for (int i = 0; i < alf.length; i++)
System.out.print(alf[i]);
System.out.println("\n");
}
// getNextChar: fetch next char.
public char getNextChar() {
char ch = ' '; // = ' ' to keep compiler happy
try {
ch = (char)System.in.read();
} catch (IOException e) {
System.out.println("Exception reading character");
}
return ch;
}
public void createCryptogram() {
char ch;
while ((byte)(ch = getNextChar()) != -1) {
if (Character.isUpperCase(ch)) ch = alf[ch - 'A'];
System.out.print(ch);
}
}
// main: for cryptogram program
public static void main(String[] args) {
Cryptogram crypto = new Cryptogram();
crypto.printArray();
crypto.createCryptogram();
}
}
Here is a run of the program, first showing the quotation to be
translated, and then the translated version, that is, the cryptogram:
% cat quote.text AND WE ARE HERE AS ON A DARKLING PLAIN SWEPT WITH CONFUSED ALARMS OF STRUGGLE AND FLIGHT, WHERE IGNORANT ARMIES CLASH BY NIGHT. DOVER BEACH, MATHEW ARNOLD % java Cryptogram < quote.text Alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ Translated to: ZUWYMPILBDJRVFHQSGAXNCTKOE ZFY TM ZGM LMGM ZA HF Z YZGJRBFI QRZBF ATMQX TBXL WHFPNAMY ZRZGVA HP AXGNIIRM ZFY PRBILX, TLMGM BIFHGZFX ZGVBMA WRZAL UO FBILX. YHCMG UMZWL, VZXLMT ZGFHRY
public void createCryptogram() {
char ch;
while ((byte)(ch = getNextChar()) != -1) {
if (Character.isUpperCase(ch)) {
ch = alf[ch - 'A'];
System.out.print(ch);
}
}
System.out.println();
}
Here is the output of this program:
% java Cryptogram2 < quote.text Alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ Translated to: OXKQFDZGACIVLHEJSMBWPNURTY OHQUFOMFGFMFOBEHOQOMIVAHZJVOAHBUFJWUAWGKEHDPBFQOVOMLBEDBWMPZZVFOHQDVAZGWUGFMFAZHEMOHWOMLAFBKVOBGXTHAZGWQENFMXFOKGLOWGFUOMHEVQ
Here is what the message looks like after decrypting:
ANDWEAREHEREASONADARKLINGPLAINSWEPTWITHCONFUSEDALARMSOFSTRUGGLEANDFLIGHTWHEREIGNORANTARMIESCLASHBYNIGHTDOVERBEACHMATHEWARNOLD