Insight into something.
/* * MainFrame.java * * Created on Jul 30, 2011, 12:05:21 AM */ package gui; import java.awt.Color; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; import javax.swing.JFileChooser; import javax.swing.JOptionPane; /** * Customizes the FileChooser to only look for .txt files. */ class TextFilter extends javax.swing.filechooser.FileFilter { /** * Makes the FileChooser only view folders or .txt documents. * * @param file * * @return */ @Override public boolean accept(File file) { return file.isDirectory() || file.getAbsolutePath().endsWith(".txt"); } /** * Changes what is displayed so the user knows what she is looking at. * * @return The String that you want as the "label" */ @Override public String getDescription() { return "Text documents (*.txt)"; } } /** * * @author Nicholas Guthrie */ public class MainFrame extends javax.swing.JFrame { private static char cbc[] = {'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'}; int init[]; String encrypted_message; String decrypted_message; String Checksum; /** Creates new form MainFrame */ public MainFrame() { initComponents(); } private int Search(char c) { for (int i = 0; i < 26; i++) { if (MainFrame.cbc[i] == c) { return i; } } return -1; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") ////GEN-BEGIN:initComponents private void initComponents() { buttonGroup_mode = new javax.swing.ButtonGroup(); buttonGroup_checksum = new javax.swing.ButtonGroup(); jFileChooser = new javax.swing.JFileChooser(); jDialog_error = new javax.swing.JDialog(); jScrollPane2 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jPanel1 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jPanel4 = new javax.swing.JPanel(); jLabel4 = new javax.swing.JLabel(); jScrollPane_plain = new javax.swing.JScrollPane(); jEditorPane_plain = new javax.swing.JEditorPane(); jPanel5 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jScrollPane3 = new javax.swing.JScrollPane(); jEditorPane_encrypted = new javax.swing.JEditorPane(); jPanel2 = new javax.swing.JPanel(); jLabel3 = new javax.swing.JLabel(); jTextField_init = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jTextField_key = new javax.swing.JTextField(); jPanel7 = new javax.swing.JPanel(); jRadioButton_encrypt = new javax.swing.JRadioButton(); jRadioButton_decrypt = new javax.swing.JRadioButton(); jPanel8 = new javax.swing.JPanel(); jRadioButton_no = new javax.swing.JRadioButton(); jRadioButton_yes = new javax.swing.JRadioButton(); jTextField_checksum = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); jTextPane_instructions = new javax.swing.JTextPane(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); jMenuItem4 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuItem3 = new javax.swing.JMenuItem(); jFileChooser.setFileFilter(new TextFilter()); jFileChooser.setForeground(java.awt.Color.black); jDialog_error.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jDialog_error.setTitle("Invalid Input"); jDialog_error.setAlwaysOnTop(true); jDialog_error.setBackground(java.awt.Color.gray); jDialog_error.setResizable(false); jTextArea1.setColumns(20); jTextArea1.setRows(5); jTextArea1.setText("Input that is not alphanumeric has been detected.\nAll input has been erased been reset.\n\nValid input can be:\nA 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\na 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"); jScrollPane2.setViewportView(jTextArea1); javax.swing.GroupLayout jDialog_errorLayout = new javax.swing.GroupLayout(jDialog_error.getContentPane()); jDialog_error.getContentPane().setLayout(jDialog_errorLayout); jDialog_errorLayout.setHorizontalGroup( jDialog_errorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jDialog_errorLayout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE) .addContainerGap()) ); jDialog_errorLayout.setVerticalGroup( jDialog_errorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jDialog_errorLayout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setForeground(java.awt.Color.darkGray); setResizable(false); jPanel1.setBackground(java.awt.Color.black); jPanel3.setBackground(java.awt.Color.darkGray); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Encryption / Decryption", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 14), java.awt.Color.orange)); // NOI18N jPanel4.setBackground(java.awt.Color.darkGray); jPanel4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); jLabel4.setFont(new java.awt.Font("Consolas", 1, 12)); jLabel4.setForeground(java.awt.Color.green); jLabel4.setText("Plain Text"); jScrollPane_plain.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane_plain.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jEditorPane_plain.setBackground(java.awt.Color.lightGray); jEditorPane_plain.setFont(new java.awt.Font("Consolas", 0, 13)); jEditorPane_plain.setAutoscrolls(false); jEditorPane_plain.setDisabledTextColor(new java.awt.Color(255, 255, 255)); jEditorPane_plain.setEnabled(false); jEditorPane_plain.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jEditorPane_plainKeyReleased(evt); } }); jScrollPane_plain.setViewportView(jEditorPane_plain); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane_plain, javax.swing.GroupLayout.DEFAULT_SIZE, 757, Short.MAX_VALUE) .addComponent(jLabel4)) .addContainerGap()) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane_plain, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE) .addContainerGap()) ); jPanel5.setBackground(java.awt.Color.darkGray); jPanel5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); jLabel5.setFont(new java.awt.Font("Consolas", 1, 12)); jLabel5.setForeground(java.awt.Color.green); jLabel5.setText("Encrypted Text"); jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jEditorPane_encrypted.setBackground(java.awt.Color.lightGray); jEditorPane_encrypted.setEditable(false); jEditorPane_encrypted.setFont(new java.awt.Font("Consolas", 0, 13)); jEditorPane_encrypted.setForeground(new java.awt.Color(255, 255, 255)); jEditorPane_encrypted.setAutoscrolls(false); jEditorPane_encrypted.setDisabledTextColor(new java.awt.Color(255, 255, 255)); jEditorPane_encrypted.setEnabled(false); jEditorPane_encrypted.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jEditorPane_encryptedKeyReleased(evt); } }); jScrollPane3.setViewportView(jEditorPane_encrypted); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup( jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 759, Short.MAX_VALUE) .addComponent(jLabel5)) .addContainerGap()) ); jPanel5Layout.setVerticalGroup( jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE) .addContainerGap()) ); jPanel2.setBackground(java.awt.Color.darkGray); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Enter Keys", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 12), java.awt.Color.green)); // NOI18N jLabel3.setFont(new java.awt.Font("Consolas", 1, 12)); jLabel3.setForeground(java.awt.Color.green); jLabel3.setText("INIT:"); jTextField_init.setBackground(java.awt.Color.cyan); jTextField_init.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jTextField_initKeyReleased(evt); } }); jLabel1.setFont(new java.awt.Font("Consolas", 1, 12)); jLabel1.setForeground(java.awt.Color.green); jLabel1.setText("Key:"); jTextField_key.setBackground(java.awt.Color.cyan); jTextField_key.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jTextField_keyKeyReleased(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField_init, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField_key, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField_init, jTextField_key}); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField_init, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(jTextField_key, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); jPanel7.setBackground(java.awt.Color.darkGray); jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Mode:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 12), java.awt.Color.green)); // NOI18N jRadioButton_encrypt.setBackground(java.awt.Color.darkGray); buttonGroup_mode.add(jRadioButton_encrypt); jRadioButton_encrypt.setForeground(java.awt.Color.cyan); jRadioButton_encrypt.setText("Encryption"); jRadioButton_encrypt.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { jRadioButton_encryptStateChanged(evt); } }); jRadioButton_encrypt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton_encryptActionPerformed(evt); } }); jRadioButton_encrypt.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { jRadioButton_encryptPropertyChange(evt); } }); jRadioButton_decrypt.setBackground(java.awt.Color.darkGray); buttonGroup_mode.add(jRadioButton_decrypt); jRadioButton_decrypt.setForeground(java.awt.Color.cyan); jRadioButton_decrypt.setText("Decryption"); jRadioButton_decrypt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton_decryptActionPerformed(evt); } }); javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7); jPanel7.setLayout(jPanel7Layout); jPanel7Layout.setHorizontalGroup( jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup() .addComponent(jRadioButton_encrypt) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton_decrypt) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel7Layout.setVerticalGroup( jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup() .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton_encrypt) .addComponent(jRadioButton_decrypt)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel8.setBackground(java.awt.Color.darkGray); jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Use Check Sum?", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 12), java.awt.Color.green)); // NOI18N jRadioButton_no.setBackground(java.awt.Color.darkGray); buttonGroup_checksum.add(jRadioButton_no); jRadioButton_no.setForeground(java.awt.Color.cyan); jRadioButton_no.setText("No"); jRadioButton_no.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton_noActionPerformed(evt); } }); jRadioButton_yes.setBackground(java.awt.Color.darkGray); buttonGroup_checksum.add(jRadioButton_yes); jRadioButton_yes.setForeground(java.awt.Color.cyan); jRadioButton_yes.setText("Yes"); jRadioButton_yes.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton_yesActionPerformed(evt); } }); javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8); jPanel8.setLayout(jPanel8Layout); jPanel8Layout.setHorizontalGroup( jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup() .addContainerGap(10, Short.MAX_VALUE) .addComponent(jRadioButton_no) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton_yes) .addContainerGap()) ); jPanel8Layout.setVerticalGroup( jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel8Layout.createSequentialGroup() .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton_yes) .addComponent(jRadioButton_no)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jTextField_checksum.setBackground(java.awt.Color.darkGray); jTextField_checksum.setEditable(false); jTextField_checksum.setFont(new java.awt.Font("Consolas", 0, 12)); jTextField_checksum.setForeground(java.awt.Color.red); jTextField_checksum.setHorizontalAlignment(javax.swing.JTextField.CENTER); jTextField_checksum.setText("Inactive"); jTextField_checksum.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Checksum Status", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 14), java.awt.Color.green)); // NOI18N javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel4, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField_checksum, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE) .addContainerGap()) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jPanel8, javax.swing.GroupLayout.Alignment.LEADING, 0, 47, Short.MAX_VALUE) .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.LEADING, 0, 47, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)) .addComponent(jTextField_checksum, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPanel4, jPanel5}); jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); jTextPane_instructions.setBackground(new java.awt.Color(153, 153, 0)); jTextPane_instructions.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Instructions", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 14), java.awt.Color.white)); // NOI18N jTextPane_instructions.setEditable(false); jTextPane_instructions.setFont(new java.awt.Font("Tahoma", 0, 12)); jTextPane_instructions.setForeground(new java.awt.Color(255, 255, 255)); jScrollPane1.setViewportView(jTextPane_instructions); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 795, Short.MAX_VALUE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jMenuBar1.setBackground(new java.awt.Color(0, 255, 204)); jMenu1.setText("File"); jMenuItem1.setText("Open Encrypted File"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuItem2.setText("Save Encrypted File"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu1.add(jMenuItem2); jMenuItem4.setText("Quit"); jMenuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem4ActionPerformed(evt); } }); jMenu1.add(jMenuItem4); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuItem3.setText("Clear All"); jMenuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem3ActionPerformed(evt); } }); jMenu2.add(jMenuItem3); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents private void jTextField_initKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_initKeyReleased jTextField_init.setText(verify(jTextField_init.getText())); if (jTextField_init.getText().length() > 4) { jTextField_init.setText(jTextField_init.getText().substring(0, 4)); } if (jTextField_init.getText().length() == 4 && jTextField_key.getText().length() == 4) { if (jEditorPane_plain != null || jEditorPane_encrypted != null) { set_mode(); } } }//GEN-LAST:event_jTextField_initKeyReleased private void jTextField_keyKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_keyKeyReleased jTextField_key.setText(verify(jTextField_key.getText())); if (jTextField_key.getText().length() > 4) { jTextField_key.setText(jTextField_key.getText().substring(0, 4)); } if (jTextField_init.getText().length() == 4 && jTextField_key.getText().length() == 4) { if (jEditorPane_plain != null || jEditorPane_encrypted != null) { set_mode(); } } }//GEN-LAST:event_jTextField_keyKeyReleased private void jRadioButton_encryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_encryptActionPerformed set_mode(); }//GEN-LAST:event_jRadioButton_encryptActionPerformed private void jRadioButton_decryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_decryptActionPerformed //jButton_convert.setText("Decrypt"); //jButton_convert.setEnabled(true); set_mode(); }//GEN-LAST:event_jRadioButton_decryptActionPerformed private int Decrypt() { String out = new String(); String message = " Message:"; String encrypted = " Encrypted:"; String key = "Subtract Key:"; String result = " Result:"; String vigenere = " Vigenere:"; String end_result = " Message: "; String plain = " Plain Text:"; String send = " Encrypted:"; encrypted_message = new String(); decrypted_message = new String(); char c_cipher[] = new char[150]; char c_init[] = jTextField_init.getText().toCharArray(); char c_key[] = jTextField_key.getText().toCharArray(); int i_init[] = new int[4]; int next_init[] = new int[4]; int i_key[] = new int[4]; for (int i = 0; i < 4; i++) { next_init[i] = Search(c_init[i]); i_key[i] = Search(c_key[i]); } encrypted_message = verify(jEditorPane_encrypted.getText()); if (encrypted_message == null) { return -1; } c_cipher = encrypted_message.toCharArray(); int size = encrypted_message.length(); if (jRadioButton_yes.isSelected()) { size -= 4; } int counter = 0; int k = 0; for (int i = 0; i < size; i++) { int pos = Search(c_cipher[i]); //Store Init i_init[counter] = next_init[counter]; next_init[counter] = pos; if (pos >= 0) { if (counter == 0) { plain = plain.concat("\t["); encrypted = encrypted.concat("\t["); vigenere = vigenere.concat("\t["); key = key.concat("\t["); result = result.concat("\t["); message = message.concat("\t["); send = send.concat("\t["); end_result = end_result.concat("\t["); } //Original Message plain = plain.concat(" " + MainFrame.cbc[pos]); encrypted = encrypted.concat(" " + pos); //Subtract Key key = key.concat(" " + i_key[counter % 4]); pos = (pos - i_key[counter % 4]) % 26; if (pos < 0) { pos += 26; } result = result.concat(" " + pos); //Subtract Init vigenere = vigenere.concat(" " + i_init[counter % 4]); pos = (pos - i_init[counter % 4]) % 26; if (pos < 0) { pos += 26; } end_result = end_result.concat(" " + pos); send = send.concat(" " + MainFrame.cbc[pos]); decrypted_message = decrypted_message.concat("" + MainFrame.cbc[pos]); counter++; if (counter == 4) { plain = plain.concat("]"); message = message.concat("]"); //add = add.concat("]"); result = result.concat("]"); key = key.concat("]"); vigenere = vigenere.concat("]"); encrypted = encrypted.concat("]"); send = send.concat("]"); end_result = end_result.concat("]"); counter = 0; k++; if (k == 5) { k = 0; out = out.concat(plain + "\n" + encrypted + "\n" + key + "\n" + result + "\n" + vigenere + "\n" + end_result + "\n" + send + "\n\n"); message = " Message:"; encrypted = " Encrypted:"; key = "Subtract Key:"; result = " Result:"; vigenere = " Vigenere:"; end_result = " Message: "; plain = " Plain Text:"; send = " Encrypted:"; } } } } out = out.concat(plain + "\n" + encrypted + "\n" + key + "\n" + result + "\n" + vigenere + "\n" + end_result + "\n" + send + "\n\n"); if (jRadioButton_yes.isSelected()) { char msg[] = encrypted_message.toCharArray(); int i_checksum[] = new int[4]; String s_checksum = "["; if (encrypted_message.length() > 4) { for (int i = 0; i < encrypted_message.length(); i++) { i_checksum[i % 4] = (i_checksum[i % 4] + Search(msg[i])) % 26; } for (int i = 0; i < 4; i++) { s_checksum = s_checksum.concat(" " + i_checksum[i]); } s_checksum = s_checksum.concat("]"); if (i_checksum[0] == 0 && i_checksum[1] == 0 && i_checksum[2] == 0 && i_checksum[3] == 0) { jTextField_checksum.setForeground(Color.green); s_checksum = s_checksum.concat(" - Valid"); } else { jTextField_checksum.setForeground(Color.red); s_checksum = s_checksum.concat(" - Invalid"); } jTextField_checksum.setText(s_checksum); } } jEditorPane_plain.setText("" + out); jEditorPane_plain.setCaretPosition(0); return 0; } private int Encrypt() { String out = new String(); String plain = " Plain:"; String message = " Message:"; String add = " Add:"; String result = " Result:"; String key = " Key:"; String vigenere = " Vigenere:"; String encrypted = "Encrypted:"; String send = "Encrypted:"; encrypted_message = new String(); decrypted_message = new String(); char c_cipher[] = new char[150]; char c_init[] = jTextField_init.getText().toCharArray(); char c_key[] = jTextField_key.getText().toCharArray(); int i_init[] = new int[4]; int i_key[] = new int[4]; try { for (int i = 0; i < 4; i++) { i_init[i] = Search(c_init[i]); i_key[i] = Search(c_key[i]); } } catch (ArrayIndexOutOfBoundsException ex) { return -1; } //collect and verify input decrypted_message = verify(jEditorPane_plain.getText()); if (decrypted_message == null) { return -1; } //add Zs to input while (decrypted_message.length() % 4 != 0) { decrypted_message = decrypted_message.concat("Z"); } c_cipher = decrypted_message.toCharArray(); int counter = 0; int k = 0; for (int i = 0; i < decrypted_message.length(); i++) { int pos = Search(c_cipher[i]); if (pos >= 0) { if (counter == 0) { plain = plain.concat("\t["); message = message.concat("\t["); add = add.concat("\t["); result = result.concat("\t["); key = key.concat("\t["); vigenere = vigenere.concat("\t["); encrypted = encrypted.concat("\t["); send = send.concat("\t["); } plain = plain.concat(" " + MainFrame.cbc[pos]); message = message.concat(" " + pos); add = add.concat(" " + i_init[counter % 4]); pos = (pos + i_init[counter % 4]) % 26; result = result.concat(" " + pos); key = key.concat(" " + i_key[counter % 4]); i_init[counter % 4] = pos = (pos + i_key[counter % 4]) % 26; vigenere = vigenere.concat(" " + pos); send = send.concat(" " + MainFrame.cbc[pos]); encrypted_message = encrypted_message.concat("" + MainFrame.cbc[pos]); counter++; if (counter == 4) { plain = plain.concat("]"); message = message.concat("]"); add = add.concat("]"); result = result.concat("]"); key = key.concat("]"); vigenere = vigenere.concat("]"); encrypted = encrypted.concat("]"); send = send.concat("]"); counter = 0; k++; if (k == 5) { out = out.concat(plain + "\n" + message + "\n" + add + "\n" + result + "\n" + key + "\n" + vigenere + "\n" + send + "\n\n"); k = 0; plain = " Plain:"; message = " Message:"; add = " Add:"; result = " Result:"; key = " Key:"; vigenere = " Vigenere:"; encrypted = "Encrypted:"; send = "Encrypted:"; } } } } out = out.concat(plain + "\n" + message + "\n" + add + "\n" + result + "\n" + key + "\n" + vigenere + "\n" + send); if (jRadioButton_yes.isSelected()) { char msg[] = encrypted_message.toCharArray(); int i_checksum[] = {0, 0, 0, 0}; String numbers = new String(); String characters = new String(); numbers = characters = "["; int pos = 0; for (int i = 0; i < encrypted_message.length(); i++) { char c = msg[i]; int val = Search(c); if (val >= 0) { i_checksum[pos % 4] = (i_checksum[pos % 4] + val) % 26; pos++; } } for (int i = 0; i < 4; i++) { i_checksum[i] = (26 - i_checksum[i]) % 26; numbers = numbers.concat(" " + i_checksum[i]); Checksum = Checksum.concat("" + MainFrame.cbc[i_checksum[i]]); characters = characters.concat(" " + MainFrame.cbc[i_checksum[i]]); encrypted_message = encrypted_message.concat("" + MainFrame.cbc[i_checksum[i]]); } characters = characters.concat("]"); numbers = numbers.concat("]"); out = out.concat("\t" + characters); jTextField_checksum.setText(numbers); } jEditorPane_encrypted.setText(" " + out); jEditorPane_encrypted.setCaretPosition(0); return 0; } private void jEditorPane_plainKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jEditorPane_plainKeyReleased jEditorPane_plain.setText(jEditorPane_plain.getText().toUpperCase()); Encrypt(); }//GEN-LAST:event_jEditorPane_plainKeyReleased private void jEditorPane_encryptedKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jEditorPane_encryptedKeyReleased jEditorPane_encrypted.setText(jEditorPane_encrypted.getText().toUpperCase()); Decrypt(); }//GEN-LAST:event_jEditorPane_encryptedKeyReleased private void jRadioButton_encryptStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jRadioButton_encryptStateChanged }//GEN-LAST:event_jRadioButton_encryptStateChanged private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed try { Open_File(); } catch (IOException ex) { Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jMenuItem1ActionPerformed private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed /** * Allows a user to (Save As) the contents of the Text Editor to a file. */ File output; int option = jFileChooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) { if (jFileChooser.getSelectedFile() != null) { output = jFileChooser.getSelectedFile(); //check if the file extension is correct if (!output.getName().endsWith(".txt")) { output = new File(output.getAbsolutePath() + ".txt"); } try { BufferedWriter out = null; out = new BufferedWriter(new FileWriter(output)); out.write(jTextField_key.getText() + " "); out.write(jTextField_init.getText() + " "); out.write(encrypted_message); out.close(); } catch (IOException ex) { Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_jMenuItem2ActionPerformed private void jRadioButton_encryptPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jRadioButton_encryptPropertyChange set_mode(); }//GEN-LAST:event_jRadioButton_encryptPropertyChange private void jRadioButton_yesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_yesActionPerformed jTextField_checksum.setForeground(Color.green); jTextField_checksum.setText("Activated"); set_mode(); }//GEN-LAST:event_jRadioButton_yesActionPerformed private void jRadioButton_noActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_noActionPerformed jTextField_checksum.setText("Inactive"); jTextField_checksum.setForeground(Color.red); set_mode(); }//GEN-LAST:event_jRadioButton_noActionPerformed private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed clear_all(); }//GEN-LAST:event_jMenuItem3ActionPerformed private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed System.exit(0); }//GEN-LAST:event_jMenuItem4ActionPerformed /** * Attempts to open a file. */ private int Open_File() throws IOException { int returnVal = jFileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { jRadioButton_decrypt.setSelected(true); File file = jFileChooser.getSelectedFile(); FileReader input = new FileReader(file.getAbsolutePath()); char t_init[] = new char[4]; char t_key[] = new char[4]; char text[] = new char[104]; char foo[] = new char[1]; String new_init = new String(); String new_key = new String(); String new_text = new String(); input.read(t_key, 0, 4); input.read(foo, 0, 1); input.read(t_init, 0, 4); input.read(foo, 0, 1); input.read(text, 0, 104); int size = text.toString().length(); String test; for (int i = 0; i < 4; i++) { test = verify("" + t_init[i]); if (test != null) { new_init = new_init.concat(test); } else { return -1; } test = verify("" + t_key[i]); if (test != null) { new_key = new_key.concat(test); } else { return -1; } } for (int i = 0; i < text.length; i++) { if (Search(text[i]) != -1) { new_text = new_text.concat("" + text[i]); } } jTextField_init.setText(new_init.toString()); jTextField_key.setText(new_key.toString()); jEditorPane_encrypted.setText(new_text); encrypted_message = verify(new_text); set_mode(); } return 0; } private void clear_all() { encrypted_message = decrypted_message = null; jEditorPane_encrypted.setText(""); jEditorPane_plain.setText(""); jTextField_init.setText(""); jTextField_key.setText(""); buttonGroup_checksum.clearSelection(); buttonGroup_mode.clearSelection(); jTextField_checksum.setText("Inactive"); jTextField_init.requestFocusInWindow(); set_mode(); } void set_mode() { if (jRadioButton_encrypt.isSelected()) { jEditorPane_plain.setEnabled(true); jEditorPane_plain.setBackground(new Color(0, 204, 204)); jEditorPane_encrypted.setEnabled(false); jEditorPane_encrypted.setBackground(Color.lightGray); jEditorPane_encrypted.setText(""); try { jEditorPane_plain.setText(decrypted_message); Encrypt(); } catch (NullPointerException e) { jEditorPane_plain.setText(""); } jEditorPane_plain.requestFocusInWindow(); } else if (jRadioButton_decrypt.isSelected()) { jEditorPane_encrypted.setBackground(new Color(0, 204, 204)); jEditorPane_encrypted.setEnabled(true); jEditorPane_encrypted.setEditable(true); jEditorPane_plain.setEnabled(false); jEditorPane_plain.setBackground(Color.lightGray); jEditorPane_plain.setText(""); try { jEditorPane_encrypted.setText(encrypted_message); Decrypt(); } catch (NullPointerException e) { jEditorPane_encrypted.setText(""); } jEditorPane_encrypted.requestFocusInWindow(); } else { jEditorPane_encrypted.setBackground(Color.lightGray); jEditorPane_encrypted.setEnabled(false); jEditorPane_encrypted.setEditable(false); jEditorPane_plain.setEnabled(false); jEditorPane_plain.setBackground(Color.lightGray); jEditorPane_plain.setText(""); jEditorPane_encrypted.setText(""); jTextField_init.requestFocusInWindow(); } } private String verify(String unverified) { unverified = unverified.toUpperCase(); char data[] = unverified.toCharArray(); for (int i = 0; i < unverified.length(); i++) { if (Search(data[i]) == -1) { //custom title, error icon String error_msg = "Input that is not alphanumeric has been detected.\n" + "All input has been reset. Please verify your input.\n" + "Valid input can be:\n" + " 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\n" + " 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\n"; JOptionPane.showMessageDialog(jDialog_error, error_msg, "Invalid Input", JOptionPane.ERROR_MESSAGE); clear_all(); return null; } } return unverified; } /** * @param args the command line arguments */ private void initialize() { this.setVisible(true); this.init = new int[4]; Checksum = new String(); this.jRadioButton_no.setSelected(true); String instructions = new String(); instructions = ("1.) Enter 4 letter (capital only) cipher (Init).\n" + "2.) Enter 4 letter (capital only) key.\n" + "3.) Select either encryption or decryption mode.\n" + "4.) Choose to use a checksum or not.\n" + "5.) Enter text to either encrypt or decrypt.\n"); this.jTextPane_instructions.setText(instructions); } public static void main(String[] args) { MainFrame window = new MainFrame(); window.initialize(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup buttonGroup_checksum; private javax.swing.ButtonGroup buttonGroup_mode; private javax.swing.JDialog jDialog_error; private javax.swing.JEditorPane jEditorPane_encrypted; private javax.swing.JEditorPane jEditorPane_plain; private javax.swing.JFileChooser jFileChooser; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel7; private javax.swing.JPanel jPanel8; private javax.swing.JRadioButton jRadioButton_decrypt; private javax.swing.JRadioButton jRadioButton_encrypt; private javax.swing.JRadioButton jRadioButton_no; private javax.swing.JRadioButton jRadioButton_yes; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane_plain; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField_checksum; private javax.swing.JTextField jTextField_init; private javax.swing.JTextField jTextField_key; private javax.swing.JTextPane jTextPane_instructions; // End of variables declaration//GEN-END:variables } [/java]