/* * CodeHelp * last revision: 25 Nov 2004 * prgmr: T Feil * */ import java.awt.*; import java.text.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class CodeHelp extends JFrame{ // ************** private JMenuItem openItem, exitItem; private JFileChooser chooser; private char c; private JButton myButton; private JLabel promptLabel1, promptLabel2; private JTextField inputField1, inputField2; private JButton subButton, clearButton, distButton, shiftButton, digramButton, affineButton; //buttons on monoCard private JButton vigSelectSubalphaButton, vigICButton, vigDistButton, vigMakeShiftButton, vigCompareButton, vigClearButton; //buttons on vigCard private JButton diDigramButton, digramSubButton; //buttons on diCard; private JTextArea vigDisplayArea; //displayarea for vigCard private JTextArea colDisplayArea; //display area for colCard private JTextArea diDisplayArea; //display area for diCard private JTextArea outputArea, displayArea; //display areas for monoCard private StringBuffer subString = new StringBuffer(" "); private StringBuffer alphaString = new StringBuffer( "a b c d e f g h i j k l m n o p q r s t u v w x y z"); private StringBuffer cipherText = new StringBuffer(""); private StringBuffer subText = new StringBuffer(""); private StringBuffer diSubText = new StringBuffer(""); private StringBuffer vigSubText = new StringBuffer(""); private StringBuffer colText = new StringBuffer(""); private int columns = 0; private int vigSubalphabets = 1; private JRadioButtonMenuItem monoButton, vigButton, diButton, colButton; private ButtonGroup modeGroup; private CardLayout cardManager = new CardLayout(); private JPanel deck, monoCard, vigCard, diCard, colCard; private Container container; private JScrollPane monoScrollPane, vigScrollPane; private JTextField diInputField1,diInputField2,diInputField3,diInputField4; private int mode = 0; //0 = mono, 1= vig, 2 = digram, 3 = col private File openDirectory = null, saveDirectory = null; Color monoColor = new Color(127,255,255); //color for mono card - aqua Color vigColor = new Color(255,255,224); //color for vigenere card - cream Color diColor = new Color(190,255, 190); //color for digram card - light green Color colColor = new Color(255,255,127); //color for columnar card - light yellow // constructor ************************************************** public CodeHelp() { super("CodeHelp - Mono Substitution"); container = getContentPane(); //set up menus JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); //set up file menu *********************************************** JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); //set up file opening menu option openItem = new JMenuItem("Open File"); openItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event) { chooser = new JFileChooser(openDirectory); //open with current directory int result = chooser.showOpenDialog(container); if(result == JFileChooser.APPROVE_OPTION){ try { readTheFile(chooser.getSelectedFile().getAbsolutePath()); openDirectory = chooser.getCurrentDirectory(); } catch (IOException e) {System.err.println(e.getMessage());} } } } ); //setup save plaintext menu option JMenuItem saveItem = new JMenuItem("Save Plaintext"); saveItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event) { chooser = new JFileChooser(saveDirectory); //open with current directory int result = chooser.showSaveDialog(container); if(result == JFileChooser.APPROVE_OPTION){ try { saveTheFile(chooser.getSelectedFile().getAbsolutePath()); saveDirectory = chooser.getCurrentDirectory(); } catch (IOException e) {System.err.println(e.getMessage());} } } } ); //exit menu option exitItem = new JMenuItem("Exit"); exitItem.setEnabled(true); exitItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event) { System.exit(0); } } ); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.add(exitItem); // set up mode menu ********************************************** JMenu modeMenu = new JMenu("Mode"); menuBar.add(modeMenu); monoButton = new JRadioButtonMenuItem("Mono Substituion"); vigButton = new JRadioButtonMenuItem("Vigenere"); diButton = new JRadioButtonMenuItem("Digram"); colButton = new JRadioButtonMenuItem("Columnar"); modeGroup = new ButtonGroup(); ModeHandler modeHandler = new ModeHandler(); modeMenu.add(monoButton); modeMenu.add(vigButton); modeMenu.add(diButton); modeMenu.add(colButton); modeGroup.add(monoButton); modeGroup.add(vigButton); modeGroup.add(diButton); modeGroup.add(colButton); monoButton.addActionListener(modeHandler); vigButton.addActionListener(modeHandler); diButton.addActionListener(modeHandler); colButton.addActionListener(modeHandler); monoButton.setSelected(true); //end of menu set up ******************************************* //************************************************************** //setup deck layout for the 4 different modes deck = new JPanel(); deck.setLayout(cardManager); JPanel monoCard = new JPanel(); monoCard.setBackground(monoColor); deck.add(monoCard,"mono"); JPanel vigCard = new JPanel(); vigCard.setBackground(vigColor); deck.add(vigCard,"vigenere"); JPanel diCard = new JPanel(); diCard.setBackground(diColor); deck.add(diCard,"digram"); JPanel colCard = new JPanel(); colCard.setBackground(colColor); deck.add(colCard,"columnar"); //setup monoCard ************************************* // set up panel for character substitution on monoCard - goes below the text area JPanel subPanel = new JPanel(); subPanel.setLayout(new FlowLayout()); subPanel.setBackground(monoColor); promptLabel1 = new JLabel("Cipher char:"); inputField1 = new JTextField(1); promptLabel2 = new JLabel(" Plain char:"); inputField2 = new JTextField(1); subButton = new JButton("Make Substitution"); subButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String s1, s2; char pchar, cchar; s1 = inputField1.getText(); s2 = inputField2.getText(); //only make sub if data entered if(s1.length()>0 && s2.length()>0){ pchar = Character.toLowerCase(s1.charAt(0)); cchar = Character.toLowerCase(s2.charAt(0)); makeSub(pchar,cchar); } } } ); //make subPanel subPanel.add(promptLabel1); subPanel.add(inputField1); subPanel.add(promptLabel2); subPanel.add(inputField2); subPanel.add(subButton); //now setup the display area for ciphertext displayArea = new JTextArea(30,100); displayArea.setFont(new Font("monospaced",Font.PLAIN,12)); displayArea.setText("No cipher text loaded."); //setup area to show substitutions outputArea = new JTextArea(2,51); outputArea.setFont(new Font("monospaced",Font.PLAIN,12)); outputArea.setText("\n"+alphaString.toString()); //clear button clearButton = new JButton("Clear Guesses"); clearButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ for(int i=0; i<26;i++) subString.setCharAt(i,' '); ShowSubstitution(); for(int i=0;i0){ vigSubalphabets = n; vigShowText(); } } } ); vigICButton = new JButton("Show I.C. for Subalphabets"); VigICHandler vigICHandler = new VigICHandler(); vigICButton.addActionListener(vigICHandler); vigMakeShiftButton = new JButton("Make a Shift Substitution for a Subalphabet"); VigShiftHandler vigShiftHandler = new VigShiftHandler(); vigMakeShiftButton.addActionListener(vigShiftHandler); vigCompareButton = new JButton("Compare Subalphabets"); VigCompareHandler vigCompareHandler = new VigCompareHandler(); vigCompareButton.addActionListener(vigCompareHandler); vigDistButton = new JButton("Show Subalphabet Distribution"); VigDistHandler vigDistHandler = new VigDistHandler(); vigDistButton.addActionListener(vigDistHandler); vigClearButton = new JButton("Clear Guesses"); VigClearHandler vigClearHandler = new VigClearHandler(); vigClearButton.addActionListener(vigClearHandler); vigScrollPane = new JScrollPane(vigDisplayArea); vigCard.add(vigScrollPane); vigCard.add(vigSelectSubalphaButton); vigCard.add(vigDistButton); vigCard.add(vigICButton); vigCard.add(vigMakeShiftButton); vigCard.add(vigCompareButton); vigCard.add(vigClearButton); vigShowText(); //end of vigCard setup ************************** //********************************************************************* //setup diCard ********************************** diDisplayArea = new JTextArea(30,100); diDisplayArea.setFont(new Font("monospaced",Font.PLAIN,12)); diDigramButton = new JButton("Digram Distribution"); diDigramButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ int[][] diCount = new int[26][26]; for(int i=0;i<26;i++) for(int j=0;j<26;j++) diCount[i][j] = 0; int i=0; while(i0 && s2.length()>0){ a = Integer.parseInt(s1); b = Integer.parseInt(s2); makeDiSubab(a,b); diShowText(); diInputField1.setText(""); diInputField2.setText(""); } if(s3.length()>0 && s4.length()>0){ c = Integer.parseInt(s3); d = Integer.parseInt(s4); makeDiSubcd(c,d); diShowText(); diInputField3.setText(""); diInputField4.setText(""); } } } ); JButton diClearButton = new JButton("Clear Quesses"); diClearButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ for(int i=0;i0){ if(k==columns){ JOptionPane.showMessageDialog(null,"Too many numbers in the permutation "+s); return; } perm[k] = num; k++; num=0; } else JOptionPane.showMessageDialog(null,"Illegal permutation: "+s); } } //finish off last number if(k==columns){ JOptionPane.showMessageDialog(null,"Too many numbers in the permutation "+s); return; } if(num>0){ perm[k]=num; k++; } if(k!=columns){ JOptionPane.showMessageDialog(null,"Too few numbers in the permutation "+s); return; } //check that all numbers within range for(int i=0;icolumns){ JOptionPane.showMessageDialog(null,"Numbers in permutation out of range "+s); return; } //now check that no column used more than once for(int i=0;ivigSubalphabets){ String s = JOptionPane.showInputDialog( "Enter which subalphabet to shift (1-"+vigSubalphabets+"):"); if(s==null) return; n = Integer.parseInt(s); } String s = JOptionPane.showInputDialog( "Ciphertext letter A should shift to letter"); if (s != null){ char c = s.charAt(0); int shift = c-'a'; for(int i=n-1;ivigSubalphabets){ String s = JOptionPane.showInputDialog( "Enter first subalphabet (1-"+vigSubalphabets+"):"); if(s==null) return; n = Integer.parseInt(s); } int m=0; while (m<1 || m>vigSubalphabets){ String s = JOptionPane.showInputDialog( "Enter second subalphabet (1-"+vigSubalphabets+"):"); if(s==null) return; m = Integer.parseInt(s); } //now calculate letter counts for these 2 subalphabets int[] count1 = new int[26]; int[] count2 = new int[26]; for(int i=0;i<26;i++) count1[i]=count2[i]=0; for(int i=n-1;ivigSubalphabets){ String s = JOptionPane.showInputDialog( "Show distribution for which subalphabet (1 -"+vigSubalphabets+")"); if(s==null) return; n = Integer.parseInt(s); } int[] count = new int[26]; for(int i=0;i<26; i++) count[i]=0; for(int i=n-1; i999) return(" "); if(x>99) return(" "); if(x>9) return(" "); return(" "); } //this displays the ciphertext (cipherText) and the corresponding plaintext (subText) in displayArea private void diShowText(){ int textWidth = 100, j=0, i=0, lineLength=0, charLength=0; diDisplayArea.setText(""); for(i=0;i