15
Main Window Encrypt the file Enter the Encryption Key

EncryptDecryptSnapsCode

Embed Size (px)

Citation preview

Main Window

Encrypt the file

Enter the Encryption Key

Encryption Complete Message

Decrypt the file

Enter the Key

Decrypted the File

Source code:

package com.ksh2.crypt;

/** * * @author Administrator */

import java.util.*;import java.io.*;import com.ksh2.crypt.Blowfish;import com.ksh2.crypt.BlowKey;import com.ksh2.crypt.BlowCipher;import java.security.KeyException;import com.ksh2.crypt.CipherMethods;import com.ksh2.crypt.PasswordDialog;import javax.swing.*;import java.awt.event.*;import com.ksh2.crypt.MessageDialog;import com.ksh2.crypt.CryptFilter;import com.ksh2.crypt.YesNoDialog;

/* *KSHCoder is the main class of the project.

*It serves as the main GUI and as well coordinates the actions of al the other clases in the project. *It contains two sub classes, namely: *EmailFile: That handles the tranfer of the encrypted/decrypted file via email *MonitorCipher a class that monitors the progress of the encryption/decryption operation */

public class KSHCoder extends javax.swing.JFrame { protected JFileChooser eChooser; protected JFileChooser dChooser; private static File inputFile; protected JFileChooser pChooser; protected PipedInputStream in; protected File eFile; protected File dFile; protected MessageDialog md; private CryptFilter cFilter; protected int state; private static String keyString; File outDirectory; public String outString; /** Creates new form KSHCoder */ public KSHCoder() { initComponents(); eChooser =new JFileChooser(); dChooser =new JFileChooser(); pChooser=new JFileChooser(); statusDisplay.setEditable(false); cFilter=new CryptFilter("enc","KSHCoder encrypted Files"); eChooser.setDialogTitle("Select File For Encrypting "); dChooser.setDialogTitle("Select File For Decrypting "); dChooser.setFileFilter(cFilter); pChooser.setDialogTitle("Select The Output Directory "); eFile=null; dFile=null; outString="Ready"; md=new MessageDialog(this,true); pack(); } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents encryptButton = new javax.swing.JButton(); decryptButton = new javax.swing.JButton(); samePathCheck = new javax.swing.JCheckBox(); deleteCheck = new javax.swing.JCheckBox(); jLabel1 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); statusDisplay = new javax.swing.JTextArea(); closeButton = new javax.swing.JButton();

getContentPane().setLayout(new java.awt.FlowLayout());

setTitle("KSHCoder"); setName("kshCoderFrame"); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } });

encryptButton.setToolTipText("Click to start encryption"); encryptButton.setLabel("Encrypt"); encryptButton.setName("encryptButton"); encryptButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { encryptButtonActionPerformed(evt); } });

getContentPane().add(encryptButton);

decryptButton.setLabel("Decrypt"); decryptButton.setName("decryptButton"); decryptButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { decryptButtonActionPerformed(evt); } });

getContentPane().add(decryptButton);

samePathCheck.setText("Encrypt/Decrypt to Same Folder?"); samePathCheck.setPreferredSize(new java.awt.Dimension(220, 24));

getContentPane().add(samePathCheck);

deleteCheck.setText("Delete Plain File After Encryption?"); deleteCheck.setPreferredSize(new java.awt.Dimension(220, 24)); getContentPane().add(deleteCheck);

jLabel1.setFont(new java.awt.Font("Dialog", 1, 10)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("View Status"); jLabel1.setPreferredSize(new java.awt.Dimension(200, 16)); getContentPane().add(jLabel1);

jPanel1.setPreferredSize(new java.awt.Dimension(300, 200)); statusDisplay.setEditable(false); statusDisplay.setLineWrap(true); statusDisplay.setRows(500); statusDisplay.setWrapStyleWord(true); statusDisplay.setPreferredSize(new java.awt.Dimension(300, 8000)); jPanel1.add(statusDisplay);

getContentPane().add(jPanel1);

closeButton.setLabel("Close"); closeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { closeButtonActionPerformed(evt); } });

getContentPane().add(closeButton);

pack(); }//GEN-END:initComponents private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed // Add your handling code here: this.setVisible(false); System.exit(0); }//GEN-LAST:event_closeButtonActionPerformed //returns the key string public static String getKeyString(){ return keyString; }

//sets the key string public static void setKeyString(String kString){ keyString=kString; } private void encryptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_encryptButtonActionPerformed // Add your handling code here: int vVal=0; int rVal=0; state=CipherMethods.ENCRYPT; outString="\n"+"Ready"+"\n"; PasswordDialog pDialog= new PasswordDialog(this,true); rVal=eChooser.showOpenDialog(this); if (rVal==JFileChooser.APPROVE_OPTION){ inputFile=eChooser.getSelectedFile(); pChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if(this.samePathCheck.isSelected()){ outDirectory=new File(inputFile.getParent()); }else{ vVal=pChooser.showOpenDialog(this); outDirectory=pChooser.getSelectedFile(); } pDialog.show(); System.out.println("key from coder is "+keyString); //pass the input file to the Cipher Methods class for encryption CipherMethods cipherm=new CipherMethods(inputFile,outDirectory,keyString,state,deleteCheck.isSelected()); //Start the Monitor cipher class to monitor the progress of the ecryption operation MonitorCipher mc=new MonitorCipher(cipherm); cipherm.start(); mc.start(); } }//GEN-LAST:event_encryptButtonActionPerformed private void decryptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_decryptButtonActionPerformed // Add your handling code here: int rVal=0; int vVal=0;

outString="\n"+"Ready"+"\n"; state=CipherMethods.DECRYPT; PasswordDialog pDialog= new PasswordDialog(this,true); rVal=dChooser.showOpenDialog(this); if (rVal==JFileChooser.APPROVE_OPTION){ inputFile=dChooser.getSelectedFile(); pChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if(this.samePathCheck.isSelected()){ outDirectory=new File(inputFile.getParent()); }else{ vVal=pChooser.showOpenDialog(this); outDirectory=pChooser.getSelectedFile(); } pDialog.show(); //pass the input file to the Cipher Methods class for decryption CipherMethods cipherm=new CipherMethods(inputFile,outDirectory,keyString,state,deleteCheck.isSelected()); //Start the Monitor cipher class to monitor the progress of the ecryption operation MonitorCipher mc=new MonitorCipher(cipherm); cipherm.start(); mc.start(); } }//GEN-LAST:event_decryptButtonActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { KSHCoder k=new KSHCoder(); k.setSize(330,410); k.show(); }

// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JCheckBox samePathCheck; private javax.swing.JLabel jLabel1; private javax.swing.JTextArea statusDisplay; private javax.swing.JButton encryptButton; private javax.swing.JPanel jPanel1; private javax.swing.JButton closeButton; private javax.swing.JButton decryptButton; private javax.swing.JCheckBox deleteCheck; // End of variables declaration//GEN-END:variables /** Holds value of property userName. */ private static String userName; /** Holds value of property password. */ private static String password; class MonitorCipher extends Thread{ protected PipedInputStream in,dialogIn; protected PipedOutputStream out; protected int cipherStatus=1000000000; protected YesNoDialog yn; private boolean stop; int c; public MonitorCipher(CipherMethods cm){ System.out.println("Starting MonitorCipher initialization"); outString+="Starting MonitorCipher initialization"+"\n"; statusDisplay.setText(outString); yn=new YesNoDialog(); //initialize and connect to the io streams that are used to communicate with the Ciphermethods class through the writing of tokens. try{ in=new PipedInputStream(cm.out); out=new PipedOutputStream(cm.in); dialogIn=new PipedInputStream(yn.getOut()); System.out.println("Finished MonitorCipher connection"); outString+="Finished MonitorCipher connection"+"\n"; statusDisplay.setText(outString); }catch(IOException e){ System.err.println(e.toString());

} stop=false; } public synchronized void stopMonitorCipher(){ try{ in.close(); out.close(); dialogIn.close(); }catch(IOException e){} stop=true; } //This method evaluates the token read from the input stream connected to the Cipher Methods class. // so as to determine the sate of the encryption/ decryption operation //via the io streams protected void evaluateStatus(){ switch(cipherStatus){ case CipherMethods.FILE_EXISTS: yn.setDialogLabel("Output File already exists. Overwrite it?"); yn.setVisible(true); try{ int d=dialogIn.read(); System.out.println("d= "+d); out.write(d); }catch(IOException e){} break; case CipherMethods.OPERATION_STARTED: if(state==CipherMethods.ENCRYPT) synchronized(outString){ outString+="\n"+"Encrypting "+inputFile.getName()+"..."; statusDisplay.setText(outString); } else synchronized(outString){ outString+="\n"+"Decrypting "+inputFile.getName()+"..."; statusDisplay.setText(outString); } break; case CipherMethods.OPERATION_ENDED: cipherStatus=100000000; synchronized(outString){ outString+="\n"+"Finished "+inputFile.getName(); statusDisplay.setText(outString);

} stopMonitorCipher(); break; case CipherMethods.OPERATION_ABORTED: cipherStatus=100000000; synchronized(outString){ outString+="\n"+"OPERATION ABORTED"; statusDisplay.setText(outString); } stopMonitorCipher(); break; } } public void run(){ while(!stop){ readFromInput(); System.out.println("CipherStatus from run is ="+cipherStatus); evaluateStatus(); } } //reads from the input stream so as to determine the current state of the encryption / decryption process protected synchronized void readFromInput(){ int i=0; try{ while((c=in.read())!=CipherMethods.EOL){ //if(c!=null) cipherStatus=c; //System.out.println("CipherStatus from read is ="+c); } }catch(IOException e){ md.setDialogLabel("IO error in reading from PipedInputStream"); md.show(); outString+=e.getMessage()+"\n"; statusDisplay.setText(outString); e.printStackTrace(); return; } } } }