Note:
1) Main Class File: FileEncryptDecrypt.java
2) Result encrypted/decrypted file’s will be available in your java application files directory it self.
3) In case of ” Multiple Files ” , make sure you have to put all the source files into your java application running directory (or) put your java .class file’s into your source directory where the files are available.
Source Code:
1. FileEncryptDecrypt.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FileEncryptDecrypt extends JFrame implements ActionListener{
JButton b1,b2,b3,b4;
Font font;
JOptionPane op=new JOptionPane();
FileEncryptDecrypt(){
ImageIcon b1i=new ImageIcon("E.gif");
ImageIcon b2i=new ImageIcon("D.gif");
b2=new JButton("Secure All Files",b2i);
ImageIcon img2=new ImageIcon("java.gif");
b3=new JButton("",img2);
ImageIcon img3=new ImageIcon("brain.gif");
ImageIcon img4=new ImageIcon("NLogo.gif");
b1=new JButton("Secure One File",img3);
b4=new JButton("",img4);
b1.setBackground(Color.green);
b2.setBackground(Color.green);
Container c=getContentPane();
op.showMessageDialog(this,"Lets Protect Your Data...........");
op.showMessageDialog(this,"By Mr.Nanddeswar(MCA)");
font=new Font("times new roman",Font.BOLD,22);
c.setLayout(null);
b1.setBounds(10,10,400,200);
b2.setBounds(600,10,350,200);
b4.setBounds(10,620,300,150);
b3.setBounds(860,620,120,150);
b1.setFont(font);
b2.setFont(font);
b3.setFont(font);
b4.setFont(font);
c.add(b1);c.add(b2);c.add(b3);c.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
Object o=ae.getSource();
if(o==b1){
SingleFileEncrypt ne=new SingleFileEncrypt();
ne.setTitle("Secure One File");
ne.setSize(1000,800);
ne.setVisible(true);
//ne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
if(o==b2){
MultipleFilesEncrypt nv=new MultipleFilesEncrypt();
nv.setTitle("Secure All Files");
nv.setSize(1000,800);
nv.setVisible(true);
//nv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
if(o==b3){
op.showMessageDialog(this,"Java Logo");
}
if(o==b4){
op.showMessageDialog(this,"Nandu Logo");
}
}
public static void main(String ar[])throws Exception{
FileEncryptDecrypt ned=new FileEncryptDecrypt();
ned.setTitle("Nandu Experiment");
ned.setSize(1000,800);
ned.setVisible(true);
ned.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}//end of file
2.SingleFileEncrypt.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.util.zip.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
public class SingleFileEncrypt extends JFrame implements ActionListener{
JButton b1,b2,b3,b4,close;
File f;
Font font;
JOptionPane op=new JOptionPane();
String fname="";
FileInputStream fis;
JFileChooser fc;
DeflaterOutputStream dos;
int key=0;
FileOutputStream fos;
String ext="";
InflaterInputStream iis;
String output="";
SingleFileEncrypt(){
ImageIcon b1i=new ImageIcon("E.gif");
ImageIcon b2i=new ImageIcon("D.gif");
b2=new JButton("Decrypt",b2i);
ImageIcon img2=new ImageIcon("java.gif");
b3=new JButton("",img2);
ImageIcon img3=new ImageIcon("brain.gif");
ImageIcon img4=new ImageIcon("NLogo.gif");
b1=new JButton("Encrypt",img3);
b4=new JButton("",img4);
close=new JButton("Close Window");
fc=new JFileChooser();
b1.setBackground(Color.green);
b2.setBackground(Color.green);
Container c=getContentPane();
op.showMessageDialog(this,"Lets Protect Your Data...........");
op.showMessageDialog(this,"By Mr.Nanddeswar(MCA)");
font=new Font("times new roman",Font.BOLD,33);
c.setLayout(null);
b1.setBounds(10,10,300,200);
b2.setBounds(600,10,300,200);
b4.setBounds(10,620,300,150);
b3.setBounds(860,620,120,150);
close.setBounds(650,620,100,50);
b1.setFont(font);
b2.setFont(font);
b3.setFont(font);
b4.setFont(font);
close.setFont(font);
c.add(b1);c.add(b2);c.add(b3);c.add(b4);c.add(close);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
close.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
Object o=ae.getSource();
if(o==b1){
int n=fc.showOpenDialog(this);
if(n==JFileChooser.APPROVE_OPTION){
f=fc.getSelectedFile();
try{
try{
key=Integer.parseInt(op.showInputDialog("Enter Key:"));
}catch(NumberFormatException ne){
key=2;
op.showMessageDialog(this,"The default KEY is used");
}
fis=new FileInputStream(f);
output=op.showInputDialog(this,"Enter the Output Encrypted file Name");
if(output.equals(""))
{
output="Default";
op.showMessageDialog(this,"The Default File Name is used");
}
ext=op.showInputDialog("Enter extention");
if(!ext.equals(""))
fos=new FileOutputStream(output+"."+ext);
else
{
fos=new FileOutputStream(output+".exe");
op.showMessageDialog(this,"File created with Default extension");
}
dos=new DeflaterOutputStream(fos);
int ed;
while((ed=fis.read())!=-1) dos.write(ed+key);
fis.close();dos.close();fis.close();
op.showMessageDialog(this,"Encryption Successfully Completed");
op.showMessageDialog(this,"Encrypted File will be placed at where the project file is existed ");
ext="";
output="";
}catch(Exception e){e.printStackTrace();}
}
}
if(o==b4){
op.showMessageDialog(this,"Nandu Logo");
}
if(o==b2){
int n=fc.showOpenDialog(this);
if(n==JFileChooser.APPROVE_OPTION){
f=fc.getSelectedFile();
try{
fis=new FileInputStream(f);
try{
key=Integer.parseInt(op.showInputDialog("Enter Key:"));
}catch(NumberFormatException ne){
key=2;
op.showMessageDialog(this,"The default KEY is used");
}
iis=new InflaterInputStream(fis);
output=op.showInputDialog(this,"Enter output Decrypted file name");
if(output.equals(""))
{
output="Default";
op.showMessageDialog(this,"The Default File Name is used");
}
ext=op.showInputDialog("Enter extention");
if(!ext.equals(""))
fos=new FileOutputStream(output+"."+ext);
else
{
fos=new FileOutputStream(output+".txt");
op.showMessageDialog(this,"File created with Default extension");
}
int dd;
while((dd=iis.read())!=-1) fos.write(dd-key);
fis.close();iis.close();fos.close();
op.showMessageDialog(this,"Decryption Successfully Completed");
op.showMessageDialog(this,"Decrypted File will be placed at where the project file is existed ");
ext="";
output="";
}catch(Exception e){e.printStackTrace();}
}
}
if(o==b3){
op.showMessageDialog(this,"Java Logo");
}
if(o==close){
this.dispose();
}
}
}
//end of file
3.MultipleFilesEncrypt.java
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.zip.*;
import javax.swing.border.*;
class MultipleFilesEncrypt extends JFrame implements ActionListener{
Container c;
JOptionPane jp;
JButton v,av,b3,b4,close;
String output="Default";
Font font;
int key=2;
String ext="";
String oext="";
MultipleFilesEncrypt(){
c=this.getContentPane();
font=new Font("times new roman",Font.BOLD,33);
c.setLayout(null);
jp=new JOptionPane();
v=new JButton("Encrypt All Files");
c.add(v);
ImageIcon img2=new ImageIcon("java.gif");
ImageIcon img4=new ImageIcon("NLogo.gif");
av=new JButton("Decrypt All Files");
c.add(av);
close=new JButton("Close Window");
c.add(close);
b3=new JButton("",img2);
c.add(b3);
b4=new JButton("",img4);
c.add(b4);
v.setBounds(10,10,300,200);
av.setBounds(600,10,300,200);
b4.setBounds(10,620,300,150);
b3.setBounds(860,620,120,150);
close.setBounds(650,620,100,50);
v.setFont(font);
av.setFont(font);
b3.setFont(font);
b4.setFont(font);
close.setFont(font);
v.addActionListener(this);
av.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
close.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
Object o=ae.getSource();
if(o==v){
try{
key=Integer.parseInt(jp.showInputDialog("Enter Key to Encrypt:"));
}catch(NumberFormatException ne){
key=2;
jp.showMessageDialog(this,"The default KEY is used");
}
output=jp.showInputDialog(this,"Enter the Output Encrypted file's Name:");
if(output.equals(""))
{
output="Default";
jp.showMessageDialog(this,"The Default File Name is used.");
}
ext=jp.showInputDialog("Enter extention of Input file's");
if(ext.equals(""))
{
ext="jpg";
jp.showMessageDialog(this,"The Default extention . JPG is used.");
}
oext=jp.showInputDialog("Enter extention of Output file's to generate:");
if(oext.equals(""))
{
oext="exe";
jp.showMessageDialog(this,"The Default extention . EXE is used.");
}
jp.showMessageDialog(this,"Wait until all files are created in your folder...");
File avpath=new File("MultipleFilesEncrypt.class");
String tep=avpath.getAbsolutePath();
StringTokenizer st=new StringTokenizer(tep,"\\");
int j=0;
String tep2=tep.replace("\\","/");
String tep3="";
while(j<st.countTokens()-1){
tep3+=st.nextToken()+"/";
}
String directory=tep3;
java.util.List<String> files=new java.util.ArrayList<String>();
File dir=new File(directory);
for(File file:dir.listFiles()){
if(file.getName().endsWith("."+ext)){
files.add(file.getName());
}
}
int listSize=files.size();
for(int i=0;i<listSize;i++){
try{
String s=files.get(i);
File delete=new File(directory+s);
Thread.sleep(2000);
FileInputStream fis=new FileInputStream(delete);
FileOutputStream fos=new FileOutputStream(output+(i+1)+"."+oext);
DeflaterOutputStream dos=new DeflaterOutputStream(fos);
int ed;
while((ed=fis.read())!=-1) dos.write(ed+key);
fis.close();dos.close();fis.close();
}catch(Exception e){e.printStackTrace();}
}
jp.showMessageDialog(this,"Encryption Successfully Completed");
jp.showMessageDialog(this,"Encrypted File's will be placed at where the project file is existed ");
key=2;
ext="";
oext="";
}//end of if(o==v) condition
if(o==av){
try{
key=Integer.parseInt(jp.showInputDialog("Enter Key:"));
}catch(NumberFormatException ne){
key=2;
jp.showMessageDialog(this,"The default KEY is used");
}
output=jp.showInputDialog(this,"Enter the Output Encrypted file's Name");
if(output.equals(""))
{
output="Default";
jp.showMessageDialog(this,"The Default File Name is used");
}
ext=jp.showInputDialog("Enter extention of Input file's");
if(ext.equals(""))
{
ext="exe";
jp.showMessageDialog(this,"The Default extention . EXE is used.");
}
oext=jp.showInputDialog("Enter extention of Output file's to generate:");
if(oext.equals(""))
{
oext="jpg";
jp.showMessageDialog(this,"The Default extention . JPG is used.");
}
jp.showMessageDialog(this,"Please wait until all files are created in your folder...");
File avpath=new File("MultipleFilesEncrypt.class");
String tep=avpath.getAbsolutePath();
StringTokenizer st=new StringTokenizer(tep,"\\");
int j=0;
String tep2=tep.replace("\\","/");
String tep3="";
while(j<st.countTokens()-1){
tep3+=st.nextToken()+"/";
}
String directory=tep3;
java.util.List<String> files=new java.util.ArrayList<String>();
File dir=new File(directory);
for(File file:dir.listFiles()){
if(file.getName().endsWith("."+ext)){
files.add(file.getName());
}
}
int listSize=files.size();
for(int i=0;i<listSize;i++){
try{
String s=files.get(i);
File delete=new File(directory+s);
Thread.sleep(2000);
FileInputStream fis=new FileInputStream(delete);
InflaterInputStream iis=new InflaterInputStream(fis);
FileOutputStream fos=new FileOutputStream(output+(i+1)+"."+oext);
int dd;
while((dd=iis.read())!=-1) fos.write(dd-key);
fis.close();iis.close();fos.close();
}catch(Exception e){e.printStackTrace();}
}
jp.showMessageDialog(this,"Decryption Successfully Completed");
jp.showMessageDialog(this,"Decrypted File's will be placed at where the project file is existed ");
key=2;
ext="";
oext="";
}//end of if(o==av) condition
if(o==b3){
jp.showMessageDialog(this,"Java Logo");
}
if(o==b4){
jp.showMessageDialog(this,"Nandu Logo");
}
if(o==close){
this.dispose();
}
}//end of actionPerformed() method
}
//end of file
//End of Code....















