Wednesday, April 2, 2014

Java Applet program for encryption and Decryption

package mypackages.encryption;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Encryption extends JFrame implements ActionListener
{
FileInputStream fis=null;
FileOutputStream fos=null;

File f=null;

int n = 0,x = 0;

private JPanel pnl = null;
private JTextField tf=null;
private JFileChooser jf=null;
private JButton openbtn=null;
private JButton enc=null;
private JButton denc=null;


public Encryption()
{
super("Do What You Want !!!!!!!");
pnl = new JPanel();
tf = new JTextField(15);
jf = new JFileChooser("F:/BCA/Semester-III/Java/Exercise-1");
openbtn = new JButton(".......");
enc = new JButton("Encrypt");
denc = new JButton("Dencrypt");

tf.setEnabled(false);

tf.setToolTipText("Selected File");
openbtn.setToolTipText("Browse");
enc.setToolTipText("Click to encrypt");
denc.setToolTipText("Click to dencrypt");

openbtn.addActionListener(this);
enc.addActionListener(this);
denc.addActionListener(this);

pnl.add(tf);
pnl.add(openbtn);
pnl.add(enc);
pnl.add(denc);

this.add(pnl);
this.setSize(300,100);
this.setVisible(true);
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});

}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==openbtn)
{
jf.showOpenDialog(this);
tf.setText(jf.getSelectedFile().getAbsolutePath());
}
if (ae.getSource()==enc)
{
if(tf.getText().length()!=0)
this.encrpt(tf.getText());
else
JOptionPane.showMessageDialog(this,"First select the file.");
}
if (ae.getSource()==denc)
{
if(tf.getText().length()!=0)
this.decrpt(tf.getText());
else
JOptionPane.showMessageDialog(this,"First select the file.");
}
}
private boolean requery(int digit,String filename)
{
boolean done = false;
try
{
f = new File("mypackages/encryption/tempfile.html");

fis = new FileInputStream(filename);
fos = new FileOutputStream(f);
n = 0;
x = fis.read();
if(digit == 1)
digit = x;
else
if(digit == 0)
digit = -x;
fos.write(x);
n++;
x = fis.read();
while (x!=-1)
{
n++;
fos.write(x+digit);
x = fis.read();
}

fis = new FileInputStream(f);
fos = new FileOutputStream(filename);

x = fis.read();
while (x!=-1)
{
fos.write(x);
x = fis.read() ;
}
fis.close();
fos.close();
done = true;
   }
   catch (IOException ioe)
   {
    JOptionPane.showMessageDialog(this,"Error : " +ioe);
   }
   return done;
}

public void encrpt(String filename)
{
if(requery(0,filename))
JOptionPane.showMessageDialog(this,"The file is Encrypted."+"\nThe size of this file is : "+this.n);
}

public void decrpt(String filename)
{
if(requery(1,filename))
JOptionPane.showMessageDialog(this,"The file is Dencrypted."+"\nThe size of this file is : "+this.n);
}
public static void main(String[] args)
{
Encryption e = new Encryption();
}
}

zip download: http://sh.st/qMGWv

No comments:

Post a Comment