0% found this document useful (0 votes)
50 views6 pages

Tayyab Abdullah 30

The document is a lab task submission that implements the Hill cipher for encrypting and decrypting 3-letter messages. It includes: 1) C code to get a 3x3 key matrix and 3-letter message from the user, encrypt the message using matrix multiplication modulo 26, and decrypt the encrypted ciphertext. 2) Functions for the encryption, decryption, getting the key and message, and calculating the inverse of the key matrix. 3) Sample output encrypting the message "cat" with a given key.

Uploaded by

Amna Arooj
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)
50 views6 pages

Tayyab Abdullah 30

The document is a lab task submission that implements the Hill cipher for encrypting and decrypting 3-letter messages. It includes: 1) C code to get a 3x3 key matrix and 3-letter message from the user, encrypt the message using matrix multiplication modulo 26, and decrypt the encrypted ciphertext. 2) Functions for the encryption, decryption, getting the key and message, and calculating the inverse of the key matrix. 3) Sample output encrypting the message "cat" with a given key.

Uploaded by

Amna Arooj
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/ 6

DATA SECURITY AND APPLICATIONS

LAB TASK

Submitted By

Ahmed Asim (F-301012)

Class: BSSE-3 (7th Semester)

Instructor Name
Engr.Fahim Muhammad Khan

Submission Date: 03rd-Jan-2024


LAB TASK
HILL CIPHER IMPLEMENTATION

CODE

#include<stdio.h>
#include<math.h>

float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];

void encryption(); //encrypts the message void


decryption(); //decrypts the message void
getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix

void main()
{ getKeyMessage();
encryption(); decryption();
}

void encryption() {
int i, j, k; for(i = 0; i
< 3; i++) for(j = 0; j
< 1; j++) for(k = 0; k
< 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
printf("\nEncrypted string is: "); for(i = 0; i < 3;
i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));

void decryption()
{ int i, j, k; inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
printf("\nDecrypted string is: "); for(i = 0; i < 3; i+
+)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));
printf("\n");
}

void getKeyMessage() {
int i, j;
char msg[3];

printf("Enter 3x3 matrix for key (It should be inversible):\n");


for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) { scanf("%f", &a[i]
[j]); c[i][j] = a[i][j]; }
printf("\nEnter a 3 letter string: ");
scanf("%s", msg); for(i
= 0; i < 3; i++) mes[i]
[0] = msg[i] - 97;
}

void inverse() {
int i, j, k; float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j) b[i][j]=1; else b[i]
[j]=0; } for(k = 0; k < 3; k+
+) { for(i = 0; i < 3; i++) { p
= c[i][k]; q = c[k][k]; for(j =
0; j < 3; j++) { if(i != k)
{ c[i][j] = c[i][j]*q - p*c[k]
[j]; b[i][j] = b[i][j]*q -
p*b[k][j];
}
}
} } for(i = 0; i < 3; i++) for(j = 0;
j < 3; j++) b[i][j] = b[i][j] / c[i]
[i]; printf("\n\nInverse Matrix is:\
n"); for(i = 0; i < 3; i++) { for(j =
0; j < 3; j++) printf("%d ", b[i]
[j]); printf("\n");
}
}

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

#define SIZE 100

int main() {
int i, j, k, n;
// Declare variables for loop indices and size
printf("Enter the message\n");
char s[SIZE], origin[SIZE];
// Prompt the user to enter the message, declare variables for input
fgets(origin, sizeof(origin), stdin);
// Read the message from the user
printf("Enter the key\n");
char key[SIZE];
// Prompt the user to enter the key, declare variable for input
scanf("%s", key);

// Remove spaces from the input string


k = 0;
for (i = 0; i < strlen(origin); i++) {
if (origin[i] != ' ')
s[k++] = origin[i];
}
s[k] = '\0';
// Remove spaces from the message and store it in 's'

// Initialize the key square matrix


char a[5][5];
n = 5;
int mp[26] = {0};
k = 0;
int pi, pj;

// Fill the key square matrix with the key


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
while (mp[key[k] - 'a'] > 0 && k < strlen(key)) {
k++;
}
if (k < strlen(key)) {
a[i][j] = key[k];
mp[key[k] - 'a']++;
pi = i;
pj = j;
}
if (k == strlen(key))
break;
}
if (k == strlen(key))
break;
}
// Fill the key square matrix with the key and remember the last position

// Fill the remaining part of the key square matrix


k = 0;
for (; i < n; i++) {
for (; j < n; j++) {
while (mp[k] > 0 && k < 26) {
k++;
}
if (k + 'a' == 'j') {
j--;
k++;
continue;
}
if (k < 26) {
a[i][j] = k + 'a';
mp[k]++;
}
}
j = 0;
}
// Fill the remaining part of the key square matrix with the alphabet

// Prepare the plaintext


char ans[SIZE];
if (strlen(s) % 2 == 1)
strcat(s, "x");
for (i = 0; i < strlen(s) - 1; i++) {
if (s[i] == s[i + 1])
s[i + 1] = 'x';
}
// Make sure the plaintext length is even and replace consecutive identical characters

// Create a mapping of characters to their positions in the key square


int mp2[26][2];
k = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
mp2[a[i][j] - 'a'][0] = i;
mp2[a[i][j] - 'a'][1] = j;
}
}
// Create a mapping of characters to their positions in the key square

// Encrypt the plaintext using the Playfair Cipher algorithm


k = 0;
for (i = 0; i < strlen(s) - 1; i += 2) {
int y1 = mp2[s[i] - 'a'][0];
int x1 = mp2[s[i] - 'a'][1];
int y2 = mp2[s[i + 1] - 'a'][0];
int x2 = mp2[s[i + 1] - 'a'][1];
if (y1 == y2) {
ans[k++] = a[y1][(x1 + 1) % 5];
ans[k++] = a[y1][(x2 + 1) % 5];
} else if (x1 == x2) {
ans[k++] = a[(y1 + 1) % 5][x1];
ans[k++] = a[(y2 + 1) % 5][x2];
} else {
ans[k++] = a[y1][x2];
ans[k++] = a[y2][x1];
}
}
ans[k] = '\0';
// Encrypt the plaintext using the Playfair Cipher algorithm

printf("%s\n", ans);
// Print the encrypted message

return 0;
}

You might also like