0% found this document useful (0 votes)
17 views19 pages

Is Contenthalf

The document outlines practical implementations of various encryption and decryption algorithms in C programming, including Caesar cipher, Monoalphabetic cipher, Playfair cipher, Polyalphabetic cipher, and Hill cipher. Each section provides code snippets for the algorithms along with explanations of their functionality. The document serves as a guide for students at Sardar Vallabhbhai Patel Institute of Technology studying Information Security.
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)
17 views19 pages

Is Contenthalf

The document outlines practical implementations of various encryption and decryption algorithms in C programming, including Caesar cipher, Monoalphabetic cipher, Playfair cipher, Polyalphabetic cipher, and Hill cipher. Each section provides code snippets for the algorithms along with explanations of their functionality. The document serves as a guide for students at Sardar Vallabhbhai Patel Institute of Technology studying Information Security.
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/ 19

SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

PRACTICAL-1

Implement Caesar cipher encryption-decryption.

#include <stdio.h >


int main(){
char str_message[500], ch;
int j, key;
printf("\n \n Enter a message to encrypt: ");
scanf("%s", str_message);
printf(" Enter the shift: ");
scanf("%d", &key);
for (j = 0; str_message[j] != '\0'; ++j)
{
ch = str_message[j];
if (ch >= 'a' && ch <= 'z')
{
ch = ch + key;
if (ch > 'z')
{
ch = ch - 'z' + 'a' - 1;
}
str_message[j] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch + key;
if (ch > 'Z')

ENROLLMENT NO.:210410107049 Page|1


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

{
ch = ch - 'Z' + 'A' - 1;
}
str_message[j] = ch;
}
}
printf(" Encrypted message: %s", str_message);
for (j = 0; str_message[j] != '\0'; ++j)
{
ch = str_message[j];
if (ch >= 'a' && ch <= 'z')
{
ch = ch - key;
if (ch<'a')
{
ch = ch + 'z' - 'a' + 1;
}
str_message[j] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch - key;
if (ch<'A')
{
ch = ch + 'Z' - 'A' + 1;
}
str_message[j] = ch;
}
}

ENROLLMENT NO.:210410107049 Page|2


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

printf("\n Decrypted message: %s \n ", str_message);


return 0;
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|3


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

PRACTICAL-2

 Implement Monoalphabetic cipher encryption-decryption.

#include <stdio.h >


#include <string.h>
int main()
{
char plain[50], encryp[50], decryp[50];
int ch, flag[26], i, j, size, a = 0;
char per[26], ch1;
printf("Enter the message- ");
gets(plain);
size = strlen(plain);
for (i = 0; i < 26; i++) flag[i] = 0;
for (i = 0; i < 26; i++)
{
a = rand() % 26;
while (flag[a] == 1) a = rand() % 26;
flag[a] = 1;
per[i] = (char)(a + 97);
}
printf("The permuted array (randomized) is- ");
for (i = 0; i < 26; i++)
{
printf("%c ", per[i]);
}
printf("\n");

ENROLLMENT NO.:210410107049 Page|4


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

for (j = 0; j < size; j++)


{
ch = plain[j];
ch = ch - 97;
ch1 = per[ch];
encryp[j] = ch1;
}
printf("The encrypted message is- ");
for (i = 0; i < size; i++)
{
printf("%c", encryp[i]);
}
printf("\n");
//Decryption i=0;
while (i < size)
{
a = 0;
while (a != 26)
{
if (per[a] == encryp[i])
{
decryp[i] = a + 97;
break;
}
a++;
}
i++;
}
decryp[i] = '\0'; printf("The decrypted message is- %s", decryp); return 0:}

ENROLLMENT NO.:210410107049 Page|5


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

OUTPUT:

ENROLLMENT NO.:210410107049 Page|6


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

PRACTICAL-3

 Implement Playfair cipher encryption-decryption.

#include <stdio.h> //header file


#include <stdlib.h> //header file
#include <string.h> //header file
#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])

ENROLLMENT NO.:210410107049 Page|7


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

{
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

ENROLLMENT NO.:210410107049 Page|8


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

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)

ENROLLMENT NO.:210410107049 Page|9


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

{ 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);

ENROLLMENT NO.:210410107049 Page|10


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

encrypt(str, keyT, ps);


}
// main code
int main()
{
char str[SIZE], key[SIZE];
// 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);
return 0;
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|11


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

PRACTICAL-4

 Implement Polyalphabetic cipher encryption-decryption.

#include <stdio.h >


#include <string.h >
#include <math.h >
int main(){
int i, j, m, a[26][26], klen, plen;
char p[100], p1[100], e[100], d[100], k[100];
printf("Enter the Plaintext: ");
gets(p);
printf("Enter the KEY: ");
scanf("%s", k);
for (i = 0; i < 26; i++)
{
m = i;
for (j = 0; j < 26; j++)
{
if (m <= 25)
{
a[i][j] = m + 97;
m++;
}
else
{
a[i][j] = 97;
m = 1; } } }

ENROLLMENT NO.:210410107049 Page|12


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

plen = strlen(p);
klen = strlen(k);
m = 0;
for (i = 0; i < plen; i++)
{
if (p[i] != 32)
{
p1[m] = p[i];
m++;
}
}
plen = strlen(p1);
m = 0;
for (i = klen; i < plen; i++)
{
if (m == klen) m = 0;
k[i] = k[m];
m++;
}
k[i] = '\0';
printf("%s", k);
printf(" %d\n", plen);
printf("Encrypted Text is: ");
for (i = 0; i < plen; i++)
{
e[i] = a[k[i] - 97][p1[i] - 97];
printf("%c", e[i]);
}
printf("\nDecrypted Text is: ");

ENROLLMENT NO.:210410107049 Page|13


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

for (i = 0; i < strlen(e); i++)


{
if (97 <= (int) e[i] && (int) e[i] <= 122)
{
if (((int) e[i] - (int) k[i]) < 0)
d[i] = ((int) e[i] - (int) k[i]) + 123;
else
d[i] = (int) e[i] - (int) k[i] + 97;
printf("%c", d[i]);
}
else
{
d[i] = e[i];
printf("%c", d[i]);
}
}
return 0;
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|14


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

PRACTICAL-5

 Implement Hill cipher encryption-decryption.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 3 // Size of the matrix (3x3 for simplicity)
// Function to find the modular inverse of a number (used in decryption)
int modInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1)
return x;
}
return -1;
}
// Function to calculate the determinant of a 3x3 matrix
int determinant(int matrix[SIZE][SIZE]) {
int det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) -
matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) +
matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
return det;
}
// Function to calculate the adjoint of a 3x3 matrix (used for decryption)
void adjoint(int matrix[SIZE][SIZE], int adj[SIZE][SIZE]) {
adj[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]);
adj[0][1] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1]);

ENROLLMENT NO.:210410107049 Page|15


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

adj[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]);


adj[1][0] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]);
adj[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]);
adj[1][2] = -(matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0]);
adj[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
adj[2][1] = -(matrix[0][0] * matrix[2][1] - matrix[0][1] * matrix[2][0]);
adj[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]);
}
// Function to multiply two matrices
void multiplyMatrices(int mat1[SIZE][SIZE], int mat2[], int res[], int mod) {
for (int i = 0; i < SIZE; i++) {
res[i] = 0;
for (int j = 0; j < SIZE; j++) {
res[i] += mat1[i][j] * mat2[j];
}
res[i] %= mod; // Take mod 26
}
}
// Function to encrypt the message using the Hill cipher
void encrypt(char message[], int keyMatrix[SIZE][SIZE]) {
int messageVector[SIZE];
int cipherVector[SIZE];
// Convert message characters to numbers (A=0, B=1, ..., Z=25)
for (int i = 0; i < SIZE; i++) {
messageVector[i] = message[i] - 'A';
}
// Multiply key matrix with message vector
multiplyMatrices(keyMatrix, messageVector, cipherVector, 26);

ENROLLMENT NO.:210410107049 Page|16


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

// Convert back to characters


printf("Encrypted message: ");
for (int i = 0; i < SIZE; i++) {
printf("%c", cipherVector[i] + 'A');
}
printf("\n");
}
// Function to decrypt the message using the Hill cipher
void decrypt(char cipher[], int keyMatrix[SIZE][SIZE]) {
int cipherVector[SIZE];
int messageVector[SIZE];
int adj[SIZE][SIZE];
int det = determinant(keyMatrix) % 26;
if (det < 0)
det += 26;
int detInverse = modInverse(det, 26);
if (detInverse == -1) {
printf("Inverse of determinant does not exist, decryption is not possible.\n");
return;
}
// Calculate the adjoint matrix
adjoint(keyMatrix, adj);
// Find the inverse key matrix by multiplying adjoint matrix with determinant inverse (mod
26)
int inverseKey[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
inverseKey[i][j] = (adj[i][j] * detInverse) % 26;

ENROLLMENT NO.:210410107049 Page|17


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

if (inverseKey[i][j] < 0)
inverseKey[i][j] += 26;
}
}
// Convert cipher characters to numbers (A=0, B=1, ..., Z=25)
for (int i = 0; i < SIZE; i++) {
cipherVector[i] = cipher[i] - 'A';
}
// Multiply inverse key matrix with cipher vector
multiplyMatrices(inverseKey, cipherVector, messageVector, 26);
// Convert back to characters
printf("Decrypted message: ");
for (int i = 0; i < SIZE; i++) {
printf("%c", messageVector[i] + 'A');
}
printf("\n");
}
int main() {
char message[SIZE + 1], cipher[SIZE + 1];
int keyMatrix[SIZE][SIZE] = {
{6, 24, 1},
{13, 16, 10},
{20, 17, 15}
};
printf("Enter a 3-letter message to encrypt (uppercase only): ");
scanf("%s", message);
// Encrypt the message
encrypt(message, keyMatrix);
printf("Enter a 3-letter cipher to decrypt (uppercase only): ");

ENROLLMENT NO.:210410107049 Page|18


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :INFORMATION SECURITY SUBJECT CODE:3170720

scanf("%s", cipher);
// Decrypt the message
decrypt(cipher, keyMatrix);
return 0;
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|19

You might also like