Vous êtes sur la page 1sur 5

Name: Dil Prasad Kuwor Roll no. : 02/064 Subject: Cryptography Assignment no. : Lab No. 2

Date: 10/5/2010

Q.5 Encrypt the given plain text to Vernam Cipher. (use the value as a → 0, b → 1,

1.

……., z → 25)

Testing Data

Plain Text :- My Nepal

Java Code:

Key :- MYPRIDE

import java.util.*; import java.awt.*; import java.awt.event.*;

import javax.swing.*; public class VernamCipher extends JFrame

{

private JTextArea plaintextArea,ciphertextArea; private JLabel plaintext,ciphertext,KeyLabel; private JButton Encrypt; private Color color=Color.green; private Container container; private JTextField keyfield;

//

main function is here

public static void main (String[] args)

{

VernamCipher application=new VernamCipher(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.setResizable(false);

}

public VernamCipher()

{

super("Vernam cipher"); container=getContentPane(); container.setBackground(color); container.setLayout(new FlowLayout()); JMenuBar bar=new JMenuBar(); setJMenuBar(bar); JMenu filemenu= new JMenu("File"); filemenu.setMnemonic('F'); bar.add(filemenu); JMenuItem aboutItem= new JMenuItem("About Me"); aboutItem.setMnemonic('A'); filemenu.add(aboutItem); aboutItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event)

{

JOptionPane.showMessageDialog(VernamCipher.this, "My name is DIL PRASAD KUNWAR 'SAMIR'\n Permanemtly I'am from Taplejung District\n and currently studying at PMC\n on BSc.CSIT 5th semester", "About Me",JOptionPane.PLAIN_MESSAGE);

}

}); JMenuItem exititem= new JMenuItem("Exit"); exititem.setMnemonic('X'); filemenu.add(exititem); exititem.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent event)

{

System.exit(0);

}

}); JMenu formatmenu=new JMenu("Format"); bar.add(formatmenu); formatmenu.setMnemonic('r'); JMenu colorItem= new JMenu("Set Color"); formatmenu.add(colorItem); colorItem.setMnemonic('c'); formatmenu.addSeparator(); JMenu fontItem=new JMenu("Font"); fontItem.setMnemonic('C'); formatmenu.add(fontItem);

JMenuItem fontcolor=new JMenuItem("Front Color"); colorItem.add(fontcolor); fontcolor.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event)

{

 

color=JColorChooser.showDialog(VernamCipher.this,"Chose a color",color); container.setBackground(color); if(color==null)

{

color=color.PINK;

}

}

//

}); KeyLabel=new JLabel("Enter The Key:"); container.add(KeyLabel); keyfield=new JTextField(14); container.add(keyfield); plaintext=new JLabel("Enter the plaintext below:"); container.add(plaintext); plaintextArea=new JTextArea(8,22); container.add(plaintextArea); container.add(new JScrollPane(plaintextArea)); Encrypt button is created Encrypt=new JButton("Encrypt");

container.add(Encrypt); Encrypt.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent event)

{

 

String check1=plaintextArea.getText(); String check2=keyfield.getText();

if(check2.equalsIgnoreCase(""))

{

JOptionPane.showMessageDialog(VernamCipher.this,"Please Enter the Key ","Key is Empty",JOptionPane.ERROR_MESSAGE);

}

else if(check1.length()>check2.length())

{

JOptionPane.showMessageDialog(VernamCipher.this,"Key length must be at least of Plaintext Size","Key Length Error",JOptionPane.ERROR_MESSAGE);

}

else if(check1.equalsIgnoreCase(""))

{

JOptionPane.showMessageDialog(VernamCipher.this, "Please Enter The Plaintext in the\n Textfield Area.", "Text Area Empty",JOptionPane.ERROR_MESSAGE);

}

else

{

ciphertextArea.setText(VernamCipher());

}

}

}

); ciphertext=new JLabel("The Ciphertext of the above plaintext is:"); container.add(ciphertext); ciphertextArea=new JTextArea(8,22); container.add(ciphertextArea); container.add(new JScrollPane(ciphertextArea));

ciphertextArea.setEditable(false);

setSize(300,450);

setVisible(true);

}

//

Plain text is converted here

private String VernamCipher()

{

 

String index="ABCDEFGHIJKLMNOPQRSTUVWXYZ",plainText="",cipherText=""; plainText=plaintextArea.getText();

String key_string=keyfield.getText(); int a[][]=new int[plainText.length()][5],cipher[][]=new int[plainText.length()][5],v=0;

int key[][]=new

int[key_string.length()][5]

;

//

for key to convert in to binary digit

for(int j=0;j<key_string.length();j++)

{

char c = key_string.charAt(j);

String val=String.valueOf(c); String index1=findIndex(val); int p=Integer.parseInt(index1); for(int i=4;i>=0;i--)

{

 

key[j][i]=p%2;

p=p/2;

}

}

//

for plaintext to convert in to binary digit for(int j=0;j<plainText.length();j++)

{

char c = plainText.charAt(j);

String val=String.valueOf(c); String index1=findIndex(val); int p=Integer.parseInt(index1); for(int i=4;i>=0;i--)

{

a[j][i]=p%2;

p=p/2;

}

}

//

XOR operation for(int j=0;j<plainText.length();j++)

{

for(int i=0;i<=4;i++)

{

if(a[j][i]==key[j][i])

{

cipher[j][i]=0;

}

else

{

cipher[j][i]=1;

}

}

}

for(int j=0;j<plainText.length();j++)

{

double power=4,k=0; for(int i=0;i<=4;i++)

{

k=k+cipher[j][i]*Math.pow(2,power);

power=power-1;

}

int l=(int )k;

l=l%26;

cipherText=cipherText+index.charAt(l);

}

return cipherText;

}

public static String findIndex(String test)

{

String letter[] = {"A", "B", "C", "D", "E", "F" ,"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; String k; for(int j=0; j<letter.length; j++)

{

if(test.equalsIgnoreCase(letter[j]))

{

}

}

Output:

k=String.valueOf(j); return k;

}

}

return null;

{ } } Output: k=String.valueOf(j); return k; } } return null;  The End  5

The End