Vernam Cipher in JAVA Frame

Hey Guys ! 
Today I would like to post the Java code for Vernam Cipher.

To Run:

Open Command Prompt.
Navigate to current location of file.
Compile java file using javac. [ javac Vernam.java ]
Run the java file .[ java Vernam ] 


Vernam.java

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
public class Vernam extends Frame implements ActionListener
{
TextField fl,pad;
Button otp,op,br,en,de;
TextArea pt,ct;
Vernam()
{
setBackground(Color.decode("#eeccff"));
setLayout(null);
fl=new TextField(30);
add(fl);
fl.setBounds(60,90,520,30);
br=new Button("Browse");
add(br);
op=new Button("Open");
op.setBounds(800,90,180,30);
add(op);
br.setBounds(600,90,180,30);
en=new Button("Encrypt >>");
add(en);
en.setBounds(430,180,80,30);
de=new Button("<< Decrypt");
add(de);
otp=new Button("Generate OTP");
add(otp);
otp.setBounds(430,380,80,30);
de.setBounds(430,280,80,30);
pad=new TextField(30);
add(pad);
pad.setBounds(430,420,80,30);
pt=new TextArea();
add(pt);
pt.setBounds(60,140,350,350);
ct=new TextArea("text",100,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
add(ct);
ct.setBounds(550,140,350,350);
br.addActionListener(this);
op.addActionListener(this);
en.addActionListener(this);
de.addActionListener(this);
otp.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
 {
  setVisible(false);
  System.exit(0);
 }
});
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(br))
{
try
{
FileDialog fd=new FileDialog(this,"Select File to Encrypt",FileDialog.LOAD);
fd.show();
String filename=fd.getDirectory()+fd.getFile();
if(!filename.equals("nullnull"))
fl.setText(filename);
}
catch(Exception e)
{
System.out.println("Error in opening file");
}
}
else if(ae.getSource().equals(op))
{
try
{
String filename=fl.getText();
FileInputStream fis=new FileInputStream(filename);
DataInputStream dis=new DataInputStream(fis);
                String str;
                while((str=dis.readLine())!=null)
                {
                   
pt.append(str+"\n");
  }
                dis.close();

}
catch(Exception e)
{
}
}
else if(ae.getSource().equals(en))
{
String plaintext=pt.getText();
String key=pad.getText();
int len=plaintext.length();
char[] cipher=new char[len];
for(int i=0;i<len;i++)
{
char p=plaintext.charAt(i);
if(p!=' '&&p!='.'&&p!='\n')
{
if((int)p >=97)
p-=97;
else if((int) p >=65)
p-=65;
else
p-=48;
char k=key.charAt(i%key.length());
if((int)k >=97)
k-=97;
else if((int) k >=65)
k-=65;
else
k-=48;

int res=(int)p+(int)k;
while(res>=26)
res-=26;
res+=97;
cipher[i]=(char)res;
}
else
cipher[i]=p;
}
ct.setText(new String(cipher));
}
else if(ae.getSource().equals(de))
{

String ciphertext=ct.getText();
String key=pad.getText();
int len=ciphertext.length();
char[] plain=new char[len];
for(int i=0;i<len;i++)
{
char c=ciphertext.charAt(i);
if(c!=' '&&c!='.'&&c!='\n')
{
if((int)c >=97)
c-=97;
else if((int) c >=65)
c-=65;
else
c-=48;
char k=key.charAt(i%key.length());
if((int)k >=97)
k-=97;
else if((int) k >=65)
k-=65;
else
k-=48;

int res=(int)c-(int)k;
while(res<0)
res+=26;
res+=97;
plain[i]=(char)res;
}
else
plain[i]=c;
}
pt.setText(new String(plain));



}
else
{
String str="abcdefghijklmnopqrstuvwxyz";
Random newran=new Random();
char[] OTP=new char[6];
for(int i=0;i<6;i++)
OTP[i]=str.charAt(newran.nextInt(str.length()));
pad.setText(new String(OTP));
}
}

public static void main(String args[])
{
Vernam v=new Vernam();
v.setSize(1000,600);
v.setVisible(true);
}

}







Comments

Popular posts from this blog

Simple Database Management using JDBC