import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class MonoAlphaSubCrack extends Applet implements TextListener, ActionListener, WindowListener { static String title = "Monoalphabetic Substition Tool"; Panel panDisplay = new Panel(); Panel panKey = new Panel(); Label labTitle = new Label( title ); TextArea taDisplay = new TextArea( 25, 80 ); Button btnMakeKey = new Button( "make key" ); Button btnDumpKey = new Button( "dump key" ); Button btnDumpPlainKey = new Button( "dump key (p-sort)" ); int numChars = 36; Label[] label = new Label[numChars]; TextField[] pchar = new TextField[numChars]; Label[] count = new Label[numChars]; String original_ciphertext=null; class CipherChar { public char pchar; public int count; public char guess; public CipherChar( char p ) { pchar = p; count = 1; } public boolean equals( Object obj ) { CipherChar cmp = (CipherChar)obj; if (cmp.pchar == pchar) { return true; } else { return false; } } } Vector cipherChars = new Vector( numChars ); //*************************************************************************** // Called by the browser or applet viewer to inform this applet that it has // been loaded into the system. It is always called before the first time that // the start method is called. public void init() { //setFont( new Font( "Courier", Font.PLAIN, 12 ) ); labTitle.setFont( new Font( "Helvetica", Font.BOLD, 14 ) ); setFont( new Font( "Courier", Font.PLAIN, 10 ) ); taDisplay.setFont( new Font( "Courier", Font.PLAIN, 12 ) ); // setup Panel One panDisplay.setBackground( Color.lightGray ); GridBagConstraints c1 = new GridBagConstraints(); GridBagLayout gridbag1 = new GridBagLayout(); panDisplay.setLayout( gridbag1 ); c1.fill = GridBagConstraints.BOTH; c1.gridwidth = GridBagConstraints.REMAINDER; c1.anchor = GridBagConstraints.NORTHWEST; gridbag1.setConstraints( taDisplay, c1 ); panDisplay.add( taDisplay ); c1.fill = GridBagConstraints.NONE; c1.gridwidth = 1; c1.anchor = GridBagConstraints.NORTHWEST; panDisplay.add( btnMakeKey ); panDisplay.add( btnDumpPlainKey ); c1.fill = GridBagConstraints.NONE; c1.gridwidth = GridBagConstraints.REMAINDER; c1.anchor = GridBagConstraints.NORTHWEST; panDisplay.add( btnDumpKey ); btnMakeKey.addActionListener( this ); btnDumpKey.addActionListener( this ); btnDumpPlainKey.addActionListener( this ); //btnMakeKey.setEnabled(false); // setup Key Panel Panel pans[] = new Panel[numChars]; panKey.setBackground( Color.yellow ); int scols = 8; int srows = (numChars/scols); if (numChars%scols>0) srows++; panKey.setLayout( new GridLayout(srows, scols*3) ); for (int i=0; i\n" +"URL: http://www.danger-island.com/~dav/\n"; } //***************************************************************** public void actionPerformed(ActionEvent evt) { if ( evt.getSource() == btnMakeKey ) { makeKeyPanel(); } else if ( evt.getSource() == btnDumpKey ) { dumpKey(); } else if ( evt.getSource() == btnDumpPlainKey ) { dumpPlainKey(); } } //***************************************************************** public void textValueChanged(TextEvent e) { //System.out.println( e ); if (original_ciphertext==null) { return; } for (int i=0; i0) { newchar = newtext.charAt( 0 ); } //System.out.println( "**** match!" ); String ciphertext = taDisplay.getText(); int idx=-1; while ((idx = original_ciphertext.indexOf((int)cipherchar, idx+1))!=-1) { taDisplay.replaceRange( String.valueOf(newchar), idx, idx+1 ); } } } } //*************************************************************************** public void makeKeyPanel() { String ciphertext = taDisplay.getText(); char c; int cipherCount = ciphertext.length(); original_ciphertext = ciphertext; CipherChar cc = new CipherChar( '?' ); int idx; for (int i=0; ihigh) { ptr = i; high = cc.count; } } if (ptr>-1) { cc = (CipherChar)cipherChars.elementAt(ptr); // add to newVec newVec.addElement((Object)cc); // remove from cipherChars cipherChars.removeElementAt(ptr); } en = cipherChars.elements(); } cipherChars = newVec; } //*************************************************************************** public void dumpKey() { char cipherchar, guesschar; System.out.println( "KEY DUMP:" ); for ( int i=0; i0) { guesschar=pchar[i].getText().charAt(0); } else { guesschar=' '; } System.out.println( ""+guesschar+'='+cipherchar ); } } } //*************************************************************************** public void dumpPlainKey() { char cipherchar, guesschar, plainchar; String alphabet = "abcdefghijklmnopqrstuvwxyz"; int count = alphabet.length(); System.out.println( "KEY DUMP:" ); for ( int i=0; i0) { guesschar=pchar[c].getText().charAt(0); if (guesschar==plainchar) { System.out.print( " "+cipherchar ); break; } } } } System.out.println(); } } //*************************************************************************** //*************************************************************************** //*************************************************************************** static public void main( String[] args ){ MonoAlphaSubCrack me = new MonoAlphaSubCrack(); me.init(); Frame f = new Frame( title ); f.add( me ); f.pack(); f.addWindowListener( me ); f.show(); } public void windowClosing(WindowEvent event) { System.exit(0); } public void windowClosed(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowIconified(WindowEvent event) {} public void windowActivated(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} public void windowOpened(WindowEvent event) {} }