Skip to main content

Vigenère cipher

After a long time, I've been doing some coding, well I was teaching.I had to do Vigenère cipher in Java.

First of all, What is "Vigenère cipher"?
The Vigenère cipher is a method of encrypting alphabetic text by using a series of interwoven Caesar ciphers based on the letters of a keyword. It is a form of polyalphabetic substitution (Wikipedia

There are a lot of methods for creating this Encryption method. For those who know how to code with OOP Principles, this code may look like Babies Rusk. 😋

We can code this in two ways.
Method 1
We use the Vigenère Matrix to find the Cipher Text. We will create the Matrix through looping and row will be either plaintext and column will be key string.

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
import java.util.*;
public class Vigenere
{
 public static void main(String [] args)
 {
  Scanner scan = new Scanner (System.in);
  System.out.print("Enter the message= ");
  String plain = scan.nextLine();
  plain=plain.toUpperCase();
  System.out.print("Enter the key= ");
  String key = scan.nextLine();
  key=key.toUpperCase();
  char [][] vignerematrix = new char [26][26];
  char [] alp = {'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'};
  int num = 0;
  int t = 0;
  for(int i=0;i<=25;i++)
  {
   for (int j=0;j<=25;j++)
   {
    num = (i+j)%26;
    vignerematrix [i][j] = alp[num]; 
   }
  }
  int strlen = plain.length();
  int keylen = key.length();
  char onstr = 'a';
  char onkeyey = 'a';
  for(int i=0;i<strlen;i++)
  {
   onstr = plain.charAt(i);
   onkeyey = key.charAt(i%keylen);
   int row=0;
   int col=0;
   
   for(int j=0;j<26;j++)
   {
    if(onstr == alp[j])
    {
     row = j;
     break;
    }
   }
   
   for(int j=0;j<26;j++)
   {
    if(onkeyey == alp[j])
    {
     col = j;
     break;
    }
   }
   System.out.print(vignerematrix[row][col]);
  }
 } 
}




Method 2
Calculate the Cipher key with adding the Plain text characters and key characters in order and finding the Cipher text through.

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
import java.util.*;
public class VigenereSimplified
{
 public static void main(String [] args)
 {
  Scanner scan = new Scanner (System.in);
  System.out.print("Enter the message= ");
  String plain = scan.nextLine();
  plain=plain.toUpperCase();
  System.out.print("Enter the key= ");
  String key = scan.nextLine();
  key=key.toUpperCase();
  char [] alp = {'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 cipherText="";
  int cipher=0;
  
  for(int i=0;i<plain.length();i++)
  {
   cipher = 0;
   for(int j=0;j<26;j++)
   {
    if(plain.charAt(i) == alp[j])
    {
     cipher += j;
    }
    if(key.charAt(i%key.length()) == alp[j])
    {
     cipher += j;
    }
   }
   cipherText += alp[cipher%26];
  }
  
  System.out.println(plain+" + "+key+" = "+cipherText);
 } 
}


These are some basic coding styles, which is hard coding.

Hey! There may be more than one solution for one problem.

Comments

Popular posts from this blog

TrashCoders

As I'm forming a Club "TrashCoders" after our Summer Holidays (May 2nd week), and going to share the knowledge I have, I created a new blog to upload all of the works we do in the Club. Hope you guys have some ideas and/or tips. Trash Coders  Happy Holidays..

Download your favorite Serials whole season at once

So, we all have our favorite serials. Or someone recently suggested you a serial which is now on it's latest season and you want to look at all those previous seasons. Some may watch it in online There are plenty of sites to watch them online. mywatchseries.to watch-series.com What if you want to download? You can download using IDM while playing online. How to download a whole season? Playing every episode and download? That's possible. But, is there an easy way? Yes. There is! These are the steps to follow. Before going down, download and install the latest IDM. IDM Latest (This is cracked. But, if you like. Support the developers by buying it) Step one Choose the site where you can download all your episodes. for example : http://dl2.mihanpix.com/Serial/ Index of Serial There are plenty of sites like this. To find those, just google "Index of Serials". Googled it Step two Go to the page of which season you want to download...

Scan Conversion(Line) - Bresenham's Algorithm

As the second Assignment we implemented Bresenham's Algorithm. For details visit  here . Assignment 02. 1.   Use the  Bresenham’s method to derive an algorithm to scan convert lines  with slope between 0 o  and -45 o (NOT  0 o  and +45 o ). 2.   Implement your algorithm to rasterize   lines that go from left to right, with slope between 0 o  and -45 o . 3.    Modify your program to scan convert lines with any slope. The file is available here (ScanConvertLine.cpp)