0% found this document useful (0 votes)
19 views7 pages

Playfair Cipher Implementation Guide

The document outlines a practical exercise for B. Tech CSE students to implement the Playfair cipher for encryption and decryption. It provides a theoretical background on the cipher, its historical usage, and includes detailed C code for the implementation. The main function allows users to choose between encrypting or decrypting text using a specified key.

Uploaded by

ashutoshya903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Playfair Cipher Implementation Guide

The document outlines a practical exercise for B. Tech CSE students to implement the Playfair cipher for encryption and decryption. It provides a theoretical background on the cipher, its historical usage, and includes detailed C code for the implementation. The main function allows users to choose between encrypting or decrypting text using a specified key.

Uploaded by

ashutoshya903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Faculty of Engineering & Technology

Information and Network security Laboratory (203105311)


B. Tech CSE 4rd Year 7th Semester

Practical -3
Aim: Implement Playfair cipher encryption-decryption

Theory:

The Playfair cipher was the first practical digraph substitution cipher. The scheme was invented
in 1854 by Charles Wheatstone but was named after Lord Playfair who promoted the use of the
cipher. In playfair cipher unlike traditional cipher we encrypt a pair of alphabets(digraphs)
instead of a single alphabet.
It was used for tactical purposes by British forces in the Second Boer War and in World War I
and for the same purpose by the Australians during World War II. This was because Playfair is
reasonably fast to use and requires no special equipment.

Code for Encryption and Decryption using Playfair cipher:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 30

// Function prototypes
void toLowerCase(char plain[], int ps);
int removeSpaces(char* plain, int ps);
void generateKeyTable(char key[], int ks, char keyT[5][5]);
void search(char keyT[5][5], char a, char b, int arr[]);
int mod5(int a);
int prepare(char str[], int ptrs);
void encrypt(char str[], char keyT[5][5], int ps);
void decrypt(char str[], char keyT[5][5], int ps);
void encryptByPlayfairCipher(char str[], char key[]);
void decryptByPlayfairCipher(char str[], char key[]);

// Function to convert the string to lowercase


void toLowerCase(char plain[], int ps) {
for (int i = 0; i < ps; i++) {
if (plain[i] >= 'A' && plain[i] <= 'Z') {
plain[i] += 'a' - 'A';
Enrollment No.: 2203051057074 Page No:
Roll Number: 40
Div: 7A9(CSE)
Faculty of Engineering & Technology
Information and Network security Laboratory (203105311)
B. Tech CSE 4rd Year 7th Semester

}
}
}

// Function to remove all spaces


int removeSpaces(char* plain, int ps) {
int count = 0;
for (int i = 0; i < ps; i++) {
if (plain[i] != ' ') {
plain[count++] = plain[i];
}
}
plain[count] = '\0';
return count;
}

// Function to generate the 5x5 key table


void generateKeyTable(char key[], int ks, char keyT[5][5]) {
int dicty[26] = {0};

for (int i = 0; i < ks; i++) {


if (key[i] != 'j') {
dicty[key[i] - 'a'] = 2;
}
}

dicty['j' - 'a'] = 1;

int i = 0, j = 0;
for (int k = 0; k < ks; k++) {
if (dicty[key[k] - 'a'] == 2) {
dicty[key[k] - 'a'] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}

Enrollment No.: 2203051057074 Page No:


Roll Number: 40
Div: 7A9(CSE)
Faculty of Engineering & Technology
Information and Network security Laboratory (203105311)
B. Tech CSE 4rd Year 7th Semester

}
}

for (int k = 0; k < 26; k++) {


if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 'a');
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
}

// Function to search for the characters of a digraph in the key table


void search(char keyT[5][5], char a, char b, int arr[]) {
if (a == 'j') a = 'i';
if (b == 'j') b = 'i';

for (int i = 0; i < 5; i++) {


for (int j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}

// Function to find the modulus with 5


int mod5(int a) {
return (a % 5 + 5) % 5;

Enrollment No.: 2203051057074 Page No:


Roll Number: 40
Div: 7A9(CSE)
Faculty of Engineering & Technology
Information and Network security Laboratory (203105311)
B. Tech CSE 4rd Year 7th Semester

// Function to prepare the plain text


int prepare(char str[], int ptrs) {
int i;
for (i = 0; i < ptrs; i += 2) {
if (str[i] == str[i + 1]) {
memmove(&str[i + 2], &str[i + 1], ptrs - i);
str[i + 1] = 'x';
ptrs++;
}
}

if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}

// Function for encryption using Playfair Cipher


void encrypt(char str[], char keyT[5][5], int ps) {
int a[4];

for (int i = 0; i < ps; i += 2) {


search(keyT, str[i], str[i + 1], a);

if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
} else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
} else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}

Enrollment No.: 2203051057074 Page No:


Roll Number: 40
Div: 7A9(CSE)
Faculty of Engineering & Technology
Information and Network security Laboratory (203105311)
B. Tech CSE 4rd Year 7th Semester

// Function for decryption using Playfair Cipher


void decrypt(char str[], char keyT[5][5], int ps) {
int a[4];

for (int i = 0; i < ps; i += 2) {


search(keyT, str[i], str[i + 1], a);

if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];
} else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];
} else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}

// Function to encrypt the text using Playfair Cipher


void encryptByPlayfairCipher(char str[], char key[]) {
int ps = strlen(str);
int ks = strlen(key);
char keyT[5][5];

ks = removeSpaces(key, ks);
toLowerCase(key, ks);

ps = removeSpaces(str, ps);
toLowerCase(str, ps);

ps = prepare(str, ps);

generateKeyTable(key, ks, keyT);

Enrollment No.: 2203051057074 Page No:


Roll Number: 40
Div: 7A9(CSE)
Faculty of Engineering & Technology
Information and Network security Laboratory (203105311)
B. Tech CSE 4rd Year 7th Semester

encrypt(str, keyT, ps);


}

// Function to decrypt the text using Playfair Cipher


void decryptByPlayfairCipher(char str[], char key[]) {
int ps = strlen(str);
int ks = strlen(key);
char keyT[5][5];

ks = removeSpaces(key, ks);
toLowerCase(key, ks);

ps = removeSpaces(str, ps);
toLowerCase(str, ps);

generateKeyTable(key, ks, keyT);

decrypt(str, keyT, ps);


}

// Main function
int main() {
char str[SIZE], key[SIZE];
int choice;

printf("Enter 1 to Encrypt or 2 to Decrypt: ");


scanf("%d", &choice);

if (choice == 1) {
// Encryption
printf("Enter key text: ");
scanf("%s", key);

printf("Enter plain text: ");


scanf("%s", str);

encryptByPlayfairCipher(str, key);
printf("Cipher text: %s\n", str);

Enrollment No.: 2203051057074 Page No:


Roll Number: 40
Div: 7A9(CSE)
Faculty of Engineering & Technology
Information and Network security Laboratory (203105311)
B. Tech CSE 4rd Year 7th Semester

} else if (choice == 2) {
// Decryption
printf("Enter key text: ");
scanf("%s", key);

printf("Enter cipher text: ");


scanf("%s", str);

decryptByPlayfairCipher(str, key);
printf("Decrypted text: %s\n", str);
} else {
printf("Invalid choice\n");
}

return 0;
}

Output:

Encryption:

Decryption:

Enrollment No.: 2203051057074 Page No:


Roll Number: 40
Div: 7A9(CSE)

You might also like