This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Encryption Decryption  RSS feed

 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
i have written a program to encrypt and decrypt the file.I have used the latest java.nio package.It works as follows::
1)Map 8 bytes from the file
2)Covert these bytes to bits.
3)Then combine these bits.
4)Then swap 1st bit with last
2nd with 2nd last
& so on.
I m enclosing the code here,the code is for Ecryption class
&EncryptionDecryption class.
I m not sending the code of Decryption as it is just the opposite of Encryption.
Here is the code:
//Encryption Class
package EncryptDecrypt;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.nio.*;
import java.nio.channels.*;
public class Encryption
{
private FileInputStream stream;
//private FileOutputStream ostream;
private static int pos_of_byte=0;
private int num_Of_Bytes;
//private static volatile byte bytes_to_write[];
private ByteBuffer write_buffer;
private ByteBuffer buffer;
private MappedByteBuffer mBuf;
private FileChannel channel,ochannel;
private int no_of_eight_byte_group;
private int remaining;
private int size;
private int start=0;
private static int pos_in_buffer=0;
private String path_of_file;
private File file,new_file;
private FileOutputStream ostream;
private BitSet one_Byte_BitSet[];
private BitSet eight_Byte_BitSet;
private BitSet bitset;
private String forDelete;
private JProgressBar progress_bar;
private int bar_value=1;
private Runtime r;
private static int po;
//private ByteBuffer buffer;

public Encryption(FileInputStream stream,String path_of_file,File file,JProgressBar progress_bar)
{
if(stream==null)
{

JOptionPane.showMessageDialog(null,"Please Enter the file");
return;
}


try{

this.stream=stream;
this.path_of_file=forDelete=path_of_file;
this.file=file;
this.progress_bar=progress_bar;
progress_bar.setStringPainted(true);
channel=stream.getChannel();
size=(int)channel.size();
no_of_eight_byte_group=(int)size/8;
remaining=(int)size%8;

//

progress_bar.setMinimum(0);
progress_bar.setMaximum(remaining!=0?no_of_eight_byte_group+1:no_of_eight_byte_group);
// progress_bar.setValue(0);
// progress_bar.setMillisToDecideToPopup(10);
r=Runtime.getRuntime();
}catch(Exception e) {System.out.println("Hi");}
//bytes_to_write=new byte[available()];
//ystem.out.println("lenght" +bytes_to_write.length);
//System.out.println(progress_bar.getMaximum());
}





public void Encrypt() throws IOException
{
path_of_file=path_of_file+".tanu";
new_file=new File(path_of_file);
ostream=new FileOutputStream(new_file);
ochannel=ostream.getChannel();



for(int k=1;k<=no_of_eight_byte_group;k++)
{

mBuf=channel.map(FileChannel.MapMode.READ_ONLY,start,8);


DES();
ochannel.write(write_buffer);

write_buffer=null;
mBuf=null;
//System.out.println("Free: "+r.freeMemory());
//r.gc();
//System.out.println("Free: "+r.freeMemory());
progress_bar.setValue(bar_value++);
//try{Thread.sleep(100);}catch(Exception e){}
System.out.println("progress: "+progress_bar.getPercentComplete()*100);
start+=8;


}


if(remaining!=0)
{



mBuf=channel.map(FileChannel.MapMode.READ_ONLY,start,remaining);

DES();

ochannel.write(write_buffer);
progress_bar.setStringPainted(true);
progress_bar.setValue(bar_value++);
System.out.println("progress: "+progress_bar.getPercentComplete()*100);
}

JOptionPane.showMessageDialog(null,"File is succesfully encrypted");


}




private void DES() throws IOException
{

byte by[]=new byte[mBuf.capacity()];
int p=0;
//System.out.println("des" + po);
po++;

write_buffer=ByteBuffer.allocateDirect(mBuf.capacity());
// System.out.println(mBuf.capacity());

// System.out.println(write_buffer.capacity());
one_Byte_BitSet=new BitSet[mBuf.capacity()];
eight_Byte_BitSet=new BitSet();
bitset=new BitSet();
// bytes_to_write=new byte[num_Of_Bytes];

int pos_in_eight_byte_bite_set=0;
int size_of_eight_Byte_BitSet=0;
boolean state=false;
int counter=0;
int j=0;
byte b;
int pos=0;



size_of_eight_Byte_BitSet=8*mBuf.capacity();

for(int i=0;i<mBuf.capacity();i++)
{


one_Byte_BitSet[i]=byteToBits(mBuf.get(i));


}


for(int i=0;i<mBuf.capacity();i++)
{

for(j=0;j<=7;j++)
{
if(one_Byte_BitSet[i].get(j))
eight_Byte_BitSet.set(pos_in_eight_byte_bite_set++);

else
eight_Byte_BitSet.clear(pos_in_eight_byte_bite_set++);

}
}



eight_Byte_BitSet=Permutation(eight_Byte_BitSet);





for(int i=0;i<size_of_eight_Byte_BitSet;i++)
{
state=eight_Byte_BitSet.get(i);


if(state)
bitset.set(counter++);
else
bitset.clear(counter++);

if(counter==8)
{


b =BitSet_To_Byte(bitset);

// System.out.println("b :" +b);
by[p++]=b;





counter=0;
}





}
write_buffer.put(by);
write_buffer.rewind();
by=null;
// return buffer;




}


private BitSet byteToBits(byte b)
{
BitSet temp=new BitSet();
int abs;
int ch;
int no;
String bits="";
boolean positive;

if(b<0)
{

positive=false;
abs=Math.abs(b);
no=abs;
while(true)
{

if(no==1)
{
bits="00000001";
break;
}

ch=no%2;
no=no/2;


if(no==1)
{
bits="1"+ch+bits;
break;
}

else
{
bits=ch+bits;

}
}


}


else
{
positive=true;
abs=(byte)Math.abs(b);
no=abs;
while(true)
{
if(no==1)
{
bits="00000001";
break;
}

ch=no%2;
no=no/2;

if(no==1)
{
bits="1"+ch+bits;
break;
}

else if(no==0)
{
bits="00000000";
//System.out.println("zero");
break;
}
else
{
bits=ch+bits;

}
}

}

if(bits.length()<8)
{
int length=bits.length();
for(int i=1;i<=8-length;i++)
bits=0+bits;

}

//System.out.println(bits);
// System.out.println(b);
/* */

temp=StringToBitSet(bits,positive);


// System.out.println(b);
// System.out.println(temp);
return temp;
}


private BitSet StringToBitSet(String bits,boolean positive)
{
char ch;
BitSet temp=new BitSet();
BitSet last1BitSet=new BitSet();
last1BitSet.set(7);

for(int i=0;i<bits.length();i++)
{
ch=bits.charAt(i);
if(ch=='1')
temp.set(i);
else
temp.clear(i);
}


if(!positive)
{
for(int i=0;i<=7;i++)
{
if(temp.get(i))
temp.clear(i);
else
temp.set(i);
}

for(int i=7;i>=0;i--)
{
if(temp.get(i) && last1BitSet.get(i))
{
temp.clear(i);
last1BitSet.set(i-1);

}

else if(temp.get(i) && !last1BitSet.get(i))
{
temp.set(i);
}

else if(!temp.get(i) && last1BitSet.get(i))
{
temp.set(i);
}

else if(!temp.get(i) && last1BitSet.get(i))
{
temp.clear(i);
}

}

}

return temp;
}


private BitSet Permutation(BitSet eight_Byte_BitSet)
{
BitSet temp;
boolean startstate,endstate;
int i=0;
int median_of_group;
int size_of_group;
temp=eight_Byte_BitSet;

size_of_group=(8*mBuf.capacity())-1;
median_of_group=(8*mBuf.capacity()/2);

while(i<median_of_group)
{

startstate=temp.get(i);
endstate=temp.get(size_of_group-i);

if(startstate)
temp.set(size_of_group-i);
else
temp.clear(size_of_group-i);

if(endstate)
temp.set(i);
else
temp.clear(i);

i++;



}





return temp;

}



private byte BitSet_To_Byte(BitSet bitset)
{
byte b=0;
BitSet last1BitSet=new BitSet();

last1BitSet.set(7);
boolean positive=true;
if(bitset.get(0))
{
positive=false;
for(int i=0;i<=7;i++)
{
if(bitset.get(i))
bitset.clear(i);
else
bitset.set(i);
}

for(int i=7;i>=0;i--)
{
if(bitset.get(i) && last1BitSet.get(i))
{
bitset.clear(i);
last1BitSet.set(i-1);

}

else if(bitset.get(i) && !last1BitSet.get(i))
{
bitset.set(i);
}

else if(!bitset.get(i) && last1BitSet.get(i))
{
bitset.set(i);
}

else if(!bitset.get(i) && last1BitSet.get(i))
{
bitset.clear(i);
}

}

// System.out.println("Bitset value" +bitset);
}

for(int i=7;i>=0;i--)
{


if(bitset.get(i))
{
b=(byte)(b+pow(2,(7-i)));
}

}


if(positive)
{
// System.out.println("byte value hai" +b);
// try{Thread.sleep(2000);}catch(Exception jfkd) {}
return b;
}
else
{
// System.out.println("byte value hai" +(byte)-b);
// try{Thread.sleep(2000);}catch(Exception jfkdj) {}
return (byte)-b;
}
}

private int pow(int base,int exp)
{
int answer=1;
for(int i=1;i<=exp;i++)
{
answer=answer*base;
}

return answer;
}
}

//EncryptionDecryption class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import EncryptDecrypt.Encryption;
import EncryptDecrypt.Decryption;
import java.nio.*;
import java.nio.channels.*;

class ApplicationWindow extends JFrame implements ActionListener
{
private JPanel south;
private JPanel north,center;

private JTextField jtf;
private JLabel label;
private JPasswordField password;
private JLabel Pwdlabel;
private JLabel PwdAccepted;
private JButton open,Encrypt,Decrypt;
private Box b,b1,b2;
private File file,new_file;
// private RandomAccessFile stream;
private FileInputStream stream;
private FileOutputStream ostream;
private FileChannel ochannel;
private ByteBuffer write_buffer;
private String path_of_file;
private byte[] bytes_to_write;
private JProgressBar progress_bar;



ApplicationWindow()
{
Container c=getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
north=new JPanel();
south=new JPanel();
center=new JPanel();

label=new JLabel("Name Of File");
jtf=new JTextField(20);
password=new JPasswordField(15);
Pwdlabel=new JLabel("Password :");
PwdAccepted=new JLabel("Password Accepted");
open=new JButton("...");
Encrypt=new JButton("Encrypt");
Decrypt=new JButton("Decrypt");
progress_bar=new JProgressBar();
// south.add(Encrypt);
// south.add(Decrypt);
//north.add(Pwdlabel);
// north.add(password);

b2=Box.createHorizontalBox();
b2.add(Encrypt);
b2.add(b2.createHorizontalStrut(50));
b2.add(Decrypt);


b=Box.createVerticalBox();
b.add(Pwdlabel);
b.add(password);
b.add(PwdAccepted);
b.add(Box.createGlue());
//b.add(Box.createRigidArea(new Dimension(5,10)));



b1=Box.createVerticalBox();
center.add(label);
center.add(jtf);
center.add(open);

b1.add(center);
// b1.add(Box.createRigidArea(new Dimension(5,10)));


//getContentPane().add(b,"North");
// getContentPane().add(b1,"Center");
// getContentPane().add(b2,"South");

c.add(b);
c.add(b1);
c.add(b2);
c.add(progress_bar);
setDefaultCloseOperation(3);
setSize(380,200);
open.addActionListener(this);
Encrypt.addActionListener(this);
Decrypt.addActionListener(this);
// setOpaque();


}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==open)

{

JFileChooser d=new JFileChooser();
int result=d.showOpenDialog(null);

try{file=d.getSelectedFile();}catch(Exception rr) {System.out.println("mar gaye");}

path_of_file=file.getAbsolutePath();
jtf.setText(path_of_file);


try
{
stream=new FileInputStream(file);
}catch(Exception ex) {System.out.println("File not found");}



}

else if(e.getSource()==Encrypt)
{
try{


progress_bar.setStringPainted(true);
Encryption encryption=new Encryption(stream,path_of_file,file,progress_bar);
encryption.Encrypt();
// stream.close();
// stream=null;
}catch(Exception ee) {}


}
else if(e.getSource()==Decrypt)
{
try{

Decryption decryption=new Decryption(stream,path_of_file,file);
decryption.Decrypt();
}catch(Exception er) {}

}

}
}
class PasswordWindow extends JFrame
{

PasswordWindow()
{

}
}

public class EncryptionDecryption
{
public static void main(String args[])
{
ApplicationWindow window=new ApplicationWindow();
// PasswordWindow pwwindow=new PasswordWindow();
window.setVisible(true);
window.setVisible(true);
}
}


Run the program as java EncryptionDecryption
The problemsthat i am facing are::
1)Sometimes in large file the program stops after sometime.
2)The progress bar is not showing any percentage,even though it is calculating the percentage as it shows in the console.
3)Also the program runs slow(but thats not the big problem as this is the tedious job for the CPU he program that is made by me: )
So anyone who can take out his precious time and have a look to the code,please solve my problems.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!