Pages

Wednesday, October 7, 2015

Vigenere cipher

The Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.

This cipher is similar with caesar cipher but different in term of shifting. The Vigenère cipher consists of several Caesar ciphers in sequence with different shift values. To encrypt, a table of alphabets can be used called Vigenère table. It consists of the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers.

Vigenere table
For example, suppose that the plaintext to be encrypted is:

Let Encrypt the text

Given string : ATTACKATDAWN

For the key, for example "LEMON". Repeat the word "LEMON" until the length of key is the same of the text length.

Key : LEMONLEMONLE

Then, compare the text and key using vigenere table.

Plaintext: ATTACKATDAWN
Key:         LEMONLEMONLE

Key is the row and plaintext is the column. for example, ROW L and COLUMN A is L. ROW E and COLUMN T is X and so on.

Cipher:    LXFOPVEFRNHR

Note: just ignore the null or replace with other alphabet


Let Decrypt the cipher

Cipher:    LXFOPVEFRNHR
Key:        LEMONLEMONLE

Key is the column. at L column find the letter Cipher L and the row letter is A. at E column find the letter Cipher X and the row letter is T and so on until you get

Plaintext: ATTACKATDAWN

To decrypt Vigenere cipeher without Key is difficult..This cipher was so strong because by swapping between all 26 possible Caesar ciphers it reduced the effectiveness of frequency analysis.

But during the Crimean War of the 1850s, Charles Babbage, under pressure of the British government, broke this cipher. The steps are :-
  1. Look for sequences of letters that appear more than once in the ciphertext.
  2. Then used vigenere table to find the most common spacing factor to identify the length of the keyword.
  3. Having established the length of the keyword and performed a frequency analysis of the portion of the ciphertext corresponding to that letter.
  4. Then, matched the frequency analysis data to the frequency in normal english to find that letter of the keyword.
  5. Repeated this for every letter in the keyword.




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
   authors by hafiq
   
   Vigenere Cipher and decrytion
   
   Decrypted without key (under contruction)
   
   any expert can advise me to improve this code...tq..
*/
import java.util.*;

public class VCipher{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      
      String text = "REPEAT ATTACK TONIGHT";
      
      String key = "HACKS";
      
      String enc = VCEncryptionWork(text,key);
      
      System.out.println(enc);
      
      String dec = VCDecryption(enc,key);
      
      System.out.println(dec);
   }
   
   public static String VCEncryptionWork(String text,String key){
      
      String alphaL = "abcdefghijklmnopqrstuvwxyz";
      String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
      String reKey = "";
      int len = text.length();
      
      // rearrange the key
      int mv=0;
      int count=0;
      for(int x=0;x<len;x++){
         if(mv == key.length()){
            count=0;
            mv=0;
         }
         reKey = reKey + key.charAt(count);
         count++;
         mv++;
      }
        
      //create venigher table
      char [][] vcl = new char[26][26];
      char [][] vcu = new char[26][26];      
      
      //using caesar cipher algorithm to form table
      for(int x=0;x<26;x++){
         count=x;
         for(int y=0;y<26;y++){
            vcl[x][y] = (char) ('a' + count % 26 );
            vcu[x][y] = (char) ('A' + count % 26 );
            count++;
         }
      }
      
      //encrypting
      String sb = "";
      for(int x=0;x<len;x++){
          if(Character.isUpperCase(text.charAt(x))){
             int srow  = alphaU.indexOf(Character.toUpperCase(reKey.charAt(x)));
             int scol = alphaU.indexOf(text.charAt(x));            
             sb = sb + vcu[srow][scol];
          }
          else if (Character.isLowerCase(text.charAt(x))){
             int srow  = alphaL.indexOf(Character.toLowerCase(reKey.charAt(x)));
             int scol = alphaL.indexOf(text.charAt(x));
             sb = sb + vcl[srow][scol];
             
          }
          else{
            //can be replace with any random alpha or null space or get from original text
            sb = sb +text.charAt(x);
          }
          
       }
      
      return sb;
   }
   
   public static String VCDecryption(String text,String key){
      
      String alphaL = "abcdefghijklmnopqrstuzwxyz";
      String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
      String reKey = "";
      int len = text.length();
      
      // rearrange the key
      int mv=0;
      int count=0;
      for(int x=0;x<len;x++){
         if(mv == key.length()){
            count=0;
            mv=0;
         }
         reKey = reKey + key.charAt(count);
         count++;
         mv++;
      }
      
      //create venigher table
      char [][] vcl = new char[26][26];
      char [][] vcu = new char[26][26];      
      
      //using caesar cipher algorithm to form table
      for(int x=0;x<26;x++){
         count=x;
         for(int y=0;y<26;y++){
            vcl[x][y] = (char) ('a' + count % 26 );
            vcu[x][y] = (char) ('A' + count % 26 );
            count++;
         }
      }
      
      //decrypting
      String sb = "";
      for(int x=0;x<len;x++){
         if(Character.isUpperCase(text.charAt(x))){
            for(int y=0;y<26;y++){
               int scol  = alphaU.indexOf(reKey.charAt(x));
               if(vcu[y][scol] == text.charAt(x)){
                  sb = sb + (char) ('A' + y % 26 );
               }
            }
         }
         else if(Character.isLowerCase(text.charAt(x))){
            for(int y=0;y<26;y++){
               int scol  = alphaL.indexOf(Character.toLowerCase(reKey.charAt(x)));
               if(vcl[y][scol] == text.charAt(x)){
                  sb = sb + (char) ('a' + y % 26 );
               }
            }
         }
         else{
            sb = sb+text.charAt(x);
         }    
      }
      return sb;
   }
}

No comments:

Post a Comment