Showing posts with label Playfair cipher. Show all posts
Showing posts with label Playfair cipher. Show all posts

Thursday, July 23, 2020

Implement Playfair cipher encryption and decryption in C.

Playfair cipher

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MX 5
int choice;
void playfair(char chr1, char chr2, char key[MX][MX]) {
    int i, j, s, x1, y1, z1;
    for (i = 0; i < MX; i++) {
        for (j = 0; j < MX; j++) {
            if (chr1 == key[i][j]) {
                s = i;
                x1 = j;
            } else if (chr2 == key[i][j]) {
                y1 = i;
                z1 = j;
            }
        }
    }
    //printf("%d%d %d%d",w,x,y,z);
    if (s == y1) {
if(choice==1){
x1 = (x1 + 1) % 5;
z1 = (z1 + 1) % 5;
}
else{
x1 = ((x1 - 1) % 5+5)%5;
z1 = ((z1 - 1) % 5+5)%5;
}
        printf("%c%c", key[s][x1], key[y1][z1]);
    } else if (x1 == z1) {
if(choice==1){
s = (s + 1) % 5;
y1 = (y1 + 1) % 5;
}
else{
s = ((s - 1) % 5+5)%5;
y1 = ((y1 - 1) % 5+5)%5;
}
        printf("%c%c", key[s][x1], key[y1][z1]);
    } 
else {
        printf("%c%c", key[s][z1], key[y1][x1]);
    }
}
void removeDuplicates(char str[]){
    int hash[256]        =  {0};
    int currentIndex     = 0;
    int lastUniqueIndex  = 0;
    while(*(str+currentIndex)){
        char temp = *(str+currentIndex);
        if(0 == hash[temp]){
            hash[temp] = 1;
            *(str+lastUniqueIndex) = temp;
            lastUniqueIndex++;
        }
        currentIndex++;
    }
    *(str+lastUniqueIndex) = '\0';
 
}
void main() {
    int i, j, k = 0, l1, m = 0, n1;
    char key[MX][MX], keyminus[25], keystr[10], str[25] = {
        0
    };
    char alpa[26] = {
        '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'
    };
printf("\n1.Encryption\n2.Decryption\n\nChoice(1 or 2):");
    scanf("%d",&choice);
if(choice!=1 && choice!=2){ printf("Invalid Choice"); return;}
fflush(stdin);
    printf("\nEnter key:");
    gets(keystr);
    printf("Enter the text:");
    gets(str);
removeDuplicates(keystr);
    n1 = strlen(keystr);
    //convert the characters to uppertext
    for (i = 0; i < n1; i++) {
        if (keystr[i] == 'j') keystr[i] = 'i';
        else if (keystr[i] == 'J') keystr[i] = 'I';
        keystr[i] = toupper(keystr[i]);
    }
    //convert all the characters of plaintext to uppertext
    for (i = 0; i < strlen(str); i++) {
        if (str[i] == 'j') str[i] = 'i';
        else if (str[i] == 'J') str[i] = 'I';
        str[i] = toupper(str[i]);
    }
    // store all characters except key
    j = 0;
    for (i = 0; i < 26; i++) {
        for (k = 0; k < n1; k++) {
            if (keystr[k] == alpa[i]) break;
            else if (alpa[i] == 'J') break;
        }
        if (k == n1) {
            keyminus[j] = alpa[i];
            j++;
        }
    }
    //construct key keymatrix
    k = 0;
    for (i = 0; i < MX; i++) {
        for (j = 0; j < MX; j++) {
            if (k < n1) {
                key[i][j] = keystr[k];
                k++;
            } else {
                key[i][j] = keyminus[m];
                m++;
            }
            printf("%c ", key[i][j]);
        }
        printf("\n");
    }
    // construct diagram and convert to cipher text
    printf("\nEntered text :%s\nOutput Text :", str);
    for (i = 0; i < strlen(str); i++) {
        if (str[i] == 'J') str[i] = 'I';
        if (str[i + 1] == '\0') playfair(str[i], 'X', key);
        else {
            if (str[i + 1] == 'J') str[i + 1] = 'I';
            if (str[i] == str[i + 1]) playfair(str[i], 'X', key);
            else {
                playfair(str[i], str[i + 1], key);
                i++;
            }
        }
    }
if(choice==2) printf(" (Remove unnecessary X)");
}


OUTPUT

Encryption

1.Encryption
2.Decryption

Choice(1 or 2):1

Enter key:Enter the text:computer
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 

Entered text :COMPUTER
Output Text :EMKRYPCT


Decryption

1.Encryption
2.Decryption

Choice(1 or 2):2

Enter key:Enter the text:computer
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 

Entered text :COMPUTER
Output Text :EMKRYPCT 


Playfair cipher
 Playfair cipher