0% found this document useful (0 votes)
7 views5 pages

Practical 2

The document outlines the implementation of the Playfair cipher, detailing the code for encoding and decoding messages using a specified key and plaintext. It includes functions for creating a Playfair matrix, finding letter positions, and formatting text for encryption. Additionally, it discusses the advantages of polyalphabetic ciphers over monoalphabetic ciphers.

Uploaded by

kriyank
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)
7 views5 pages

Practical 2

The document outlines the implementation of the Playfair cipher, detailing the code for encoding and decoding messages using a specified key and plaintext. It includes functions for creating a Playfair matrix, finding letter positions, and formatting text for encryption. Additionally, it discusses the advantages of polyalphabetic ciphers over monoalphabetic ciphers.

Uploaded by

kriyank
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
You are on page 1/ 5

Enrollment No.

:230843116012

PRACTICAL – 2
AIM:-Implement playfair cipher. The plaintext is paired in two characters.
Discuss the advantage of polyalphabetic cipher over monoalphabetic
cipher.
Key = MONARCHY
Plaintext = ar mu hs ea
Ciphertext = RM CM BP IM

CODE:-
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define SIZE 5
#define MAX_INPUT 100

void createPlayfairMatrix(char *key, char matrix[SIZE][SIZE]) {


char keySet[26] = {0};
int keyLen = strlen(key);
int index = 0;

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


key[i] = toupper(key[i]);
if (key[i] == 'J') key[i] = 'I';
}

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


if (isalpha(key[i]) && !keySet[key[i] - 'A']) {
keySet[key[i] - 'A'] = 1;
matrix[index / SIZE][index % SIZE] = key[i];
index++;
}
}

for (char c = 'A'; c <= 'Z'; c++) {


if (c != 'J' && !keySet[c - 'A']) {
keySet[c - 'A'] = 1;
matrix[index / SIZE][index % SIZE] = c;
index++;
}
}
}

4
Enrollment No.:230843116012

void findPosition(char matrix[SIZE][SIZE], char letter, int *row, int *col) {


if (letter == 'J') letter = 'I';

for (*row = 0; *row < SIZE; (*row)++) {


for (*col = 0; *col < SIZE; (*col)++) {
if (matrix[*row][*col] == letter) {
return;
}
}
}
}

void playfairEncode(char matrix[SIZE][SIZE], char *plaintext, char *ciphertext) {


int len = strlen(plaintext);
int cipherIndex = 0;

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


char first = plaintext[i];
char second = (i + 1 < len) ? plaintext[i + 1] : 'X';

if (first == second) {
second = 'X';
i--;
}

int row1, col1, row2, col2;


findPosition(matrix, first, &row1, &col1);
findPosition(matrix, second, &row2, &col2);

if (row1 == row2) {
ciphertext[cipherIndex++] = matrix[row1][(col1 + 1) % SIZE];
ciphertext[cipherIndex++] = matrix[row2][(col2 + 1) % SIZE];
} else if (col1 == col2) {
ciphertext[cipherIndex++] = matrix[(row1 + 1) % SIZE][col1];
ciphertext[cipherIndex++] = matrix[(row2 + 1) % SIZE][col2];
} else {
ciphertext[cipherIndex++] = matrix[row1][col2];
ciphertext[cipherIndex++] = matrix[row2][col1];
}
}

ciphertext[cipherIndex] = '\0';
}

void playfairDecode(char matrix[SIZE][SIZE], char *ciphertext, char *plaintext) {


int len = strlen(ciphertext);
int plainIndex = 0;

5
Enrollment No.:230843116012

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


char first = ciphertext[i];
char second = ciphertext[i + 1];

int row1, col1, row2, col2;


findPosition(matrix, first, &row1, &col1);
findPosition(matrix, second, &row2, &col2);

if (row1 == row2) {
plaintext[plainIndex++] = matrix[row1][(col1 + SIZE - 1) % SIZE];
plaintext[plainIndex++] = matrix[row2][(col2 + SIZE - 1) % SIZE];
} else if (col1 == col2) {
plaintext[plainIndex++] = matrix[(row1 + SIZE - 1) % SIZE][col1];
plaintext[plainIndex++] = matrix[(row2 + SIZE - 1) % SIZE][col2];
} else {
plaintext[plainIndex++] = matrix[row1][col2];
plaintext[plainIndex++] = matrix[row2][col1];
}
}

plaintext[plainIndex] = '\0';
}

void printMatrix(char matrix[SIZE][SIZE]) {


printf("Playfair Matrix:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}

void prepareText(char *input, char *output) {


int j = 0;
for (int i = 0; i < strlen(input); i++) {
if (isalpha(input[i])) {
output[j++] = toupper(input[i]);
if (output[j-1] == 'J') output[j-1] = 'I';
}
}
output[j] = '\0';
}

void formatInPairs(char *input, char *output) {


6
Enrollment No.:230843116012

int len = strlen(input);


int j = 0;

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


output[j++] = input[i];
if (i + 1 < len) {
output[j++] = input[i + 1];
}
if (i + 2 < len) {
output[j++] = ' ';
}
}
output[j] = '\0';
}

void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}

int main() {
char key[MAX_INPUT];
char plaintext[MAX_INPUT];
char matrix[SIZE][SIZE];
char processedText[MAX_INPUT] = {0};
char result[MAX_INPUT] = {0};
char formattedOutput[MAX_INPUT * 2] = {0};
char choice;

printf("Playfair Cipher Implementation\n");


printf("=============================\n\n");

printf("Enter the key (letters only): ");


fgets(key, MAX_INPUT, stdin);
key[strcspn(key, "\n")] = 0;

printf("Enter the plaintext: ");


fgets(plaintext, MAX_INPUT, stdin);
plaintext[strcspn(plaintext, "\n")] = 0;

printf("\nKey: %s\n", key);

createPlayfairMatrix(key, matrix);
printMatrix(matrix);

prepareText(plaintext, processedText);

7
Enrollment No.:230843116012

printf("Do you want to encode (E) or decode (D)? ");


scanf(" %c", &choice);
clearInputBuffer();

if (toupper(choice) == 'E') {
playfairEncode(matrix, processedText, result);
printf("Plaintext: %s\n", plaintext);
formatInPairs(result, formattedOutput);
printf("Ciphertext: %s\n", formattedOutput);
} else if (toupper(choice) == 'D') {
playfairDecode(matrix, processedText, result);
printf("Ciphertext: %s\n", plaintext);
formatInPairs(result, formattedOutput);
printf("Plaintext: %s\n", formattedOutput);
} else {
printf("Invalid choice. Exiting.\n");
return 1;
}
return 0;
}

OUTPUT

You might also like