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

Practical 4 - BIS - Manual

The document provides a C program that implements the Playfair Cipher Technique for encryption. It includes functions for converting strings to lowercase, removing spaces, generating a 5x5 key table, and encrypting plaintext using the Playfair algorithm. The main function demonstrates the encryption process using a specified key and plaintext.

Uploaded by

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

Practical 4 - BIS - Manual

The document provides a C program that implements the Playfair Cipher Technique for encryption. It includes functions for converting strings to lowercase, removing spaces, generating a 5x5 key table, and encrypting plaintext using the Playfair algorithm. The main function demonstrates the encryption process using a specified key and plaintext.

Uploaded by

Punani Nikul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 4:

Write a Program to implement the Play-Fair Cipher Technique for


encryption. (Any of the Language C/C++/Java/Python)

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

#define SIZE 30

// this function will convert the string to lowercase

void toLowerCase(char plain[], int ps)


{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}

// this function will remove all the spaces


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

// this function will generate the 5x5 grid square


void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;

// character hashmap of 26 character that will


// store count of the alphabet.
dicty = (int*)calloc(26, sizeof(int));
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}

dicty['j' - 97] = 1;

i = 0;
j = 0;

for (k = 0; k < ks; k++) {


if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}
}
}

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


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

// this function will search for the characters of a digraph


// in the key and return position of key
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';

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

for (j = 0; j < 5; j++) {

if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}

// this function will find the modulus with 5


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

// this function will make the plain text length even


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

// encryption will done using this function


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

for (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]];
}
}
}

// this function will encrypt cipher text using Playfair Cipher algorithm
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];

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

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

ps = prepare(str, ps);

generateKeyTable(key, ks, keyT);

encrypt(str, keyT, ps);


}
// main code
void main()
{
char str[SIZE], key[SIZE];
clrscr();
// key text
strcpy(key, "Algorithm");
printf("Key text: %s\n", key);

// Plaintext
strcpy(str, "Programming");
printf("Plain text: %s\n", str);

// encryption using the "Playfair Cipher" algorithmn


encryptByPlayfairCipher(str, key);

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

getch();
}

You might also like