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 Key :- MYPRIDE Java Code: import java.util.*; import java.awt.*; import java.awt.event.*; java.awt.event.*; import javax.swing.*; public class VernamCipher extends JFrame { private JTextArea plaintextArea,ciphertextArea plaintextArea,ciphertextArea;; private JLabel plaintext,ci p laintext,ciphertext,KeyLabel; phertext,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( application.setDefaultCloseOperation(JFrame.EXIT_ON_CLO JFrame.EXIT_ON_CLOSE); SE); application.setResizable(false); } public VernamCipher() { super("Vernam cipher"); container=getContentPane(); container.setBackground(color); container.setLayout(new 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(){ ActionListener(){ public void actionPerformed(ActionEvent actionPerformed(ActionEvent event) 1
{ 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); Me",JOptionPane.PLAIN_MESSAGE);
//
} }); JMenuItem exititem= new JMenuItem("Exit"); exititem.setMnemonic('X'); filemenu.add(exititem); exititem.addActionListener( new ActionListener(){ ActionListener(){ public void actionPerformed(ActionEvent 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"); JMenu("Font"); fontItem.setMnemonic('C'); formatmenu.add(fontItem); JMenuItem fontcolor=new JMenuItem("Front Color"); colorItem.add(fontcolor); fontcolor.addActionListener( new ActionListener(){ ActionListener(){ public void actionPerformed(ActionEvent actionPerformed(ActionEvent event) { color=JColorChooser.showDialog(VernamCipher.t color=JColorChooser.showDial og(VernamCipher.this,"Chose his,"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 plaintextArea=new JTextArea(8,22); container.add(plaintextArea); container.add(new container.add(new JScrollPane(plaintextAre JScrollPane(plaintextArea)); a)); Encrypt button is created Encrypt=new JButton("Encrypt");
2
container.add(Encrypt); Encrypt.addActionListener( new ActionListener(){ ActionListener(){ public void actionPerformed(ActionEven actionPerformed(ActionEventt event) { String check1=plaintextArea.getText(); check1=plaintextArea.getText(); String check2=keyfield.getText(); check2=keyfield.getText(); if(check2.equalsIgnoreCase("")) { JOptionPane.showMessageDialog(VernamCip JOptionPane.showMessageDialog(VernamCipher.this,"Please her.this,"Please Enter the Key ","Key is Empty",JOptionPane.ERROR_MESSAGE); Empty",JOptionPane.ERROR_MESSAGE); } else if(check1.length()>check2.length()) if(check1.length()>check2.length()) { JOptionPane.showMessageDialog(VernamCip JOptionPane.showMessageDialog(VernamCipher.this,"Key her.this,"Key length must be at least of Plaintext Size","Key Length Error",JOptionPane.ERROR_MESSAGE); } else if(check1.equalsIgnor if(check1.equalsIgnoreCase("")) eCase("")) { JOptionPane.showMessageDialog(VernamCipher.this, "Please Enter The Plaintext in the\n Textfield T extfield Area.", "Text Area Empty",JOptionPane.ERROR_MESSAGE) Empty",JOptionPane.ERROR_MESSAGE);; } else { ciphertextArea.setText(VernamCipher()); } } } ); ciphertext=new JLabel("The Ciphertext Ciphertext of the above plaintext is:"); container.add(ciphertext); ciphertextArea=new ciphertextArea=new JTextArea(8,22); container.add(ciphertextArea); container.add(new container.add(new JScrollPane(ciphertextAr JScrollPane(ciphertextArea)); ea)); ciphertextArea.setEditable(false); setSize(300,450); setVisible(true); } // Plain text is converted here private String VernamCipher() { String index="ABCDEFGHIJKLMNOPQRSTUVWXYZ",plain index="ABCDEFGHIJKLMNOPQRSTUVWXYZ",plainText="",cipherText=""; Text="",cipherText=""; plainText=plaintextArea.getText(); String key_string=keyfield.getText(); key_string=keyfield.getText(); int a[][]=new int[plainText.length()][5],cipher[ int[plainText.length()][5],cipher[][]=new ][]=new int[plainText.length()][5],v=0; int[plainText.length()][5],v=0; int key[][]=new int[key_string.length()][5] int[key_string.length()][5] ; // for key to convert in to binary digit for(int j=0;j
3
String val=String.valueOf(c); val=String.valueOf(c); String index1=findIndex(val); index1=findIndex(val); int p=Integer.parseInt(in p=Integer.parseInt(index1); dex1); 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
=0;i--) { a[j][i]=p%2; p=p/2; } } XOR operation for(int j=0;j
} public static String findIndex(String test) {
4
String letter[] = {"A", "B", "C", "D", "E", "F" ,"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "V", "W", "X", "Y", "Z" }; String k; for(int j=0; j
The
End 5