Tuesday, 14 August 2018

Implement Polyalphabetic cipher encryption-decryption in c.


We can improve the simple monoalphabetic technique is to use different monoalphabetic substitutions as one proceeds through the plaintext message. The general name for this approach is polyalphabetic substitution cipher. All these techniques have the following features in common:
1. A set of related monoalphabetic substitution rules is used.
2. A key determines which particular rule is chosen for a given transformation.


The best known, and one of the simplest, polyalphabetic ciphers is the Vigenère cipher. In this scheme, the set of related monoalphabetic substitution rules consists of the 26 Caesar ciphers with shifts of 0 through 25. Each cipher is denoted by a key letter, which is the cipher text letter that substitutes for the plaintext letter a. Thus, a Caesar cipher with a shift of 3 is denoted by the key value 38.

 

To encrypt a message using the Vigenère Cipher you first need to choose a keyword (or keyphrase). You then repeat this keyword over and over until it is the same length as the plaintext. This is called the keystream.
Now for each plaintext letter, you find the letter down the left hand side of the tabula recta. You also take the corresponding letter from the keystream, and find this across the top of the tabula recta. Where these two lines cross in the table is the ciphertext letter you use.
 

Formula:
Cipher text C= (p+key)mod26
Plain text p= (C-key)mod26

Example:
key:              deceptivedeceptivedeceptive
plaintext:     wearediscoveredsaveyourself
ciphertext:    ZICVTWQNGRZGVTWAVZHCQYGLMGJ
 
Code:- 



//POLYALPHABATIC CIPHER
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char pt[20]={'\0'},ct[20]={'\0'},key[20]={'\0'},rt[20]={'\0'};
    int i,j;
    clrscr();
    printf("\n enter the plain text:");
    scanf("%s",pt);
    printf("\n enter the key:");
    scanf("%s",key);

    //length of plaintext equal to length of key
    j=0;
    for(i=strlen(key);i<strlen(pt);i++)
    {
    if(j==strlen(key))
    {
    j=0;
    }
    key[i]=key[j];
    j++;
    }
    printf("\n new key is:%s",key);

    //converting plain text to cipher text (encryption)
    for(i=0;i<strlen(pt);i++)
    {
        ct[i]=(((pt[i]-97)+(key[i]-97))%26)+97;
    }
    printf("\n \n cipher text is:%s",ct);

    //converting cipher text to plain text (decryption)
    for(i=0;i<strlen(ct);i++)
    {
    if(ct[i]<key[i])
    {
        rt[i]=26+((ct[i]-97)-(key[i]-97))+97;
    }
    else
        rt[i]=(((ct[i]-97)-(key[i]-97))%26)+97;
    }
    printf("\n \n plain text is:%s",rt);
    getch();
}

Output:-
 

THANK YOU!!!!

No comments:

Post a Comment