0% found this document useful (0 votes)
2 views20 pages

Encryption and Decryption Techniques

The document contains practical implementations of various encryption and decryption algorithms, including Caesar cipher, Playfair cipher, Polyalphabetic cipher, and Hill cipher. Each section provides the aim, program code, and user interaction for encryption and decryption processes. Additionally, it includes prompts for user input and outputs for encrypted and decrypted texts.

Uploaded by

tinalclg
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)
2 views20 pages

Encryption and Decryption Techniques

The document contains practical implementations of various encryption and decryption algorithms, including Caesar cipher, Playfair cipher, Polyalphabetic cipher, and Hill cipher. Each section provides the aim, program code, and user interaction for encryption and decryption processes. Additionally, it includes prompts for user input and outputs for encrypted and decrypted texts.

Uploaded by

tinalclg
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

Enrollment No.

221230107036 [INFORMATION SECURITY (3170720)]

PRACTICAL: - 1
Aim:- Implement caeser cipher encryption decryption.

PROGRAM:-
#include <stdio.h>

#include <ctype.h>

void encrypt(char text[], int key) {

int i = 0; while (text[i] != '\0') {

char ch = text[i];

if (isalpha(ch)) {

char base = isupper(ch) ? 'A' : 'a,:

text[i] = (ch - base + key) % 26 + base;

i++;

void decrypt(char text[], int key) {

int i = 0;

while (text[i] != '\0') {

char ch = text[i];

if (isalpha(ch)) {

char base = isupper(ch) ? 'A' : 'a';

text[i] = (ch - base - key + 26) % 26 + base;

i++; }

}
int main() {

SSAIET, Computer Dept, Navsari Page 1


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

char text[100];

int key;

char choice;

printf("Do you want to (E)ncrypt or (D)ecrypt? ");

scanf(" %c", &choice);

printf("Enter the text: ");

getchar();

fgets(text, sizeof(text), stdin);

int i = 0;

while (text[i] != '\0') {

if (text[i] == '\n') {

text[i] = '\0';

break

i++;

printf("Enter the key (shift amount): ");

scanf("%d", &key); key = key % 26;

if (choice == 'E' || choice == 'e') {

encrypt(text, key);

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

} else if (choice == 'D' || choice == 'd') {

decrypt(text, key);

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

} else {
printf("Invalid choice.\n");

SSAIET, Computer Dept, Navsari Page 2


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

} return 0;

OUTPUT:-
ENCRYPTION:-

DESCRYPTION:-

SSAIET, Computer Dept, Navsari Page 3


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

PRACTICAL: - 2
Aim:- Implement Play fair cipher encryption decryption.

PROGRAM:-
#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define SIZE 5 char matrix[SIZE][SIZE];

void generateKeyMatrix(char key[]) {

int i, j, k;

int flag[26] = {0};

flag['J' - 'A'] = 1;

i = 0; j = 0;

for (k = 0; key[k] != '\0'; k++) {

char ch = toupper(key[k]);

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

if (ch < 'A' || ch > 'Z') continue;

if (!flag[ch - 'A']) {

matrix[i][j] = ch;

flag[ch - 'A'] = 1;

j++;

if (j == SIZE) { j = 0; i++; } }

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

if (!flag[ch - 'A']) {

matrix[i][j] = ch;

SSAIET, Computer Dept, Navsari Page 4


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

flag[ch - 'A'] = 1;

j++;

if (j == SIZE) { j = 0; i++; } } }

} void findPosition(char letter, int *row, int *col) {

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

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

for (int j = 0; j < SIZE; j++) {

if (matrix[i][j] == letter) {

*row = i;

*col = j;

return; } } }

} int prepareText(char input[], char output[]) {

int len = 0;

int i = 0;

while (input[i]) {

char ch = toupper(input[i]);

if (ch >= 'A' && ch <= 'Z') {

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

output[len++] = ch; }

i++; }

output[len] = '\0';

char temp[200];

int tempLen = 0;

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

temp[tempLen++] = output[i];

SSAIET, Computer Dept, Navsari Page 5


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

if (i + 1 == len) {

temp[tempLen++] = 'X';

} else if (output[i] == output[i + 1]) {

temp[tempLen++] = 'X';

i--;

} else {

temp[tempLen++] = output[i + 1]; } }

temp[tempLen] = '\0';

strcpy(output, temp);

return tempLen++; }

void encrypt(char plaintext[], char ciphertext[], int len) {

int r1, c1, r2, c2;

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

findPosition(plaintext[i],&r1,&c1);

findPosition(plaintext[i+1], &r2, &c2);

if (r1 == r2) {

ciphertext[i] = matrix[r1][(c1 + 1) % SIZE];

ciphertext[i+1] = matrix[r2][(c2 + 1) % SIZE];

} else if (c1 == c2) {

ciphertext[i] = matrix[(r1 + 1) % SIZE][c1];

ciphertext[i+1] = matrix[(r2 + 1) % SIZE][c2];

} else {

ciphertext[i] = matrix[r1][c2];

ciphertext[i+1] = matrix[r2][c1]; }

ciphertext[len] = '\0';

SSAIET, Computer Dept, Navsari Page 6


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

}
void decrypt(char ciphertext[], char plaintext[], int len) {

int r1, c1, r2, c2;

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

findPosition(ciphertext[i], &r1, &c1);

findPosition(ciphertext[i+1], &r2, &c2);

if (r1 == r2) {

ciphertext[i] = matrix[r1][(c1 + SIZE - 1) % SIZE];

ciphertext[i+1] = matrix[r2][(c2 + SIZE - 1) % SIZE];

} else if (c1 == c2) {

ciphertext[i] = matrix[(r1 + SIZE - 1) % SIZE][c1];

ciphertext[i+1] = matrix[(r2 + SIZE - 1) % SIZE][c2];

} else {

ciphertext[i]=matrix[r1][c2];

ciphertext[i+1] = matrix[r2][c1];

} }

ciphertext[len] = '\0';

strcpy(plaintext, ciphertext);

} void printMatrix() {

printf("Key Matrix:\n");

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

for (int j = 0; j < SIZE; j++) {

printf("%c ", matrix[i][j]);

printf("\n");

SSAIET, Computer Dept, Navsari Page 7


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

} int main() {

char key[100], input[200], processed[200], output[200];

char choice;

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

fgets(key, sizeof(key), stdin);

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

generateKeyMatrix(key);

printMatrix();

printf("Do you want to (E)ncrypt or (D)ecrypt? ");

scanf(" %c", &choice);

printf("Enter the text: ");

getchar();

fgets(input, sizeof(input), stdin);

input[strcspn(input, "\n")] = 0;

int len = 0;

if (choice == 'E' || choice == 'e') {

len=prepareText(input,processed);

encrypt(processed,output,len);

printf("Encrypted text: %s\n", output);

} else if (choice == 'D' || choice == 'd') {

int idx = 0;

for (int i = 0; input[i]; i++) {

char ch = toupper(input[i]);

if (ch >= 'A' && ch <= 'Z') {

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

SSAIET, Computer Dept, Navsari Page 8


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

processed[idx++] = ch; } }

processed[idx] = '\0';

len = idx;

if (len % 2 != 0) {

printf("Invalid ciphertext length for Playfair cipher.\n");

return 1;

decrypt(processed, output, len);

printf("Decrypted text: %s\n", output);

} else {

printf("Invalid choice.\n"); }

return 0; }

OUTPUT:-

ENCRYPTION:-

SSAIET, Computer Dept, Navsari Page 9


Enrollment No. 221230107036 [INFORMATION SECURITY (3170720)]

DECRYPTION:-

SSAIET, Computer Dept, Navsari Page 10


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

PRACTICAL:- 3
AIM:- Implement Polyalphabetic cipher encryption-decryption.

PROGRAM:-
#include <stdio.h>

#include <string.h>

#include <ctype.h>

void encrypt(char text[], char key[]) {

int textLen = strlen(text);

int keyLen = strlen(key);

int i, j = 0;

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

if (isalpha(text[i])) {

char base = isupper(text[i]) ? 'A' : 'a';

char k = toupper(key[j % keyLen]) - 'A';

text[i] = ( (text[i] - base + k) % 26 ) + base;

j++; } }

} void decrypt(char text[], char key[]) {

int textLen = strlen(text);

int keyLen = strlen(key);

int i, j = 0; for (i = 0; i < textLen; i++) {

if (isalpha(text[i])) {

char base = isupper(text[i]) ? 'A' : 'a';

char k = toupper(key[j % keyLen]) - 'A';

SSAIET, Computer Dept, Navsari Page 11


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

text[i] = ( (text[i] - base - k + 26) % 26 ) + base;

j++; } }

} int main() {

char text[100], key[100];

int choice;

printf("Enter text: ");

fgets(text, sizeof(text), stdin);

text[strcspn(text, "\n")] = '\0';

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

scanf("%s", key);

printf("1. Encrypt\n2. Decrypt\nChoose: ");

scanf("%d", &choice);

if (choice == 1) {

encrypt(text, key);

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

} else if (choice == 2) {

decrypt(text, key);

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

} else {

printf("Invalid choice.\n");

} return 0;

SSAIET, Computer Dept, Navsari Page 12


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

OUTPUT:-
ENCRYPTION:-

DECRYPTION:-

SSAIET, Computer Dept, Navsari Page 13


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

PRACTICAL: - 4
Aim:- Implement Hill cipher encryption decryption.

PROGRAM:-
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define MAX 10

int mod26(int n);

int determinant(int matrix[MAX][MAX], int n);

int modInverse(int a, int m);

void getCofactor(int matrix[MAX][MAX], int temp[MAX][MAX], int p, int q, int n);

void adjoint(int matrix[MAX][MAX], int adj[MAX][MAX], int n);

int inverseMatrix(int matrix[MAX][MAX], int inv[MAX][MAX], int n);

void multiplyMatrixVector(int matrix[MAX][MAX], int vector[MAX], int result[MAX], int

n); v

oid encrypt(int key[MAX][MAX], int plaintext[], int ciphertext[], int len, int n) {

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

int block[MAX];

for (int j = 0; j < n; j++) {

block[j] = plaintext[i + j];

multiplyMatrixVector(key, block, &ciphertext[i], n); }

void decrypt(int invKey[MAX][MAX], int ciphertext[], int plaintext[], int len, int n) {

SSAIET, Computer Dept, Navsari Page 14


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

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

int block[MAX];

for (int j = 0; j < n; j++) {


block[j] = ciphertext[i + j]; }

multiplyMatrixVector(invKey, block, &plaintext[i], n; }

} int main() {

int n;

int key[MAX][MAX]; int invKey[MAX][MAX];

char input[1000];

int plaintext[1000], ciphertext[1000];

int len; printf("Enter matrix size n (e.g. 2 for 2x2): ");

scanf("%d", &n); if (n <= 0 || n > MAX) {

printf("Invalid matrix size. Max supported is %d.\n", MAX);

return 1; }

printf("Enter key matrix elements (row-wise) mod 26:\n");

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

for (int j = 0; j < n; j++) {

scanf("%d", &key[i][j]);

key[i][j] = mod26(key[i][j]); }

int det = determinant(key, n);

det = mod26(det);

int detInv = modInverse(det, 26);

if (det == 0 || detInv == -1) {

printf("Key matrix is not invertible mod 26, can't decrypt.\n");

return 1; }

SSAIET, Computer Dept, Navsari Page 15


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

if (!inverseMatrix(key, invKey, n)) {

printf("Error computing inverse key matrix.\n");

return 1; }

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

getchar();

fgets(input, sizeof(input), stdin);

input[strcspn(input, "\n")] = 0;

len = 0;

for (int i = 0; input[i] != '\0'; i++) {

if (isalpha(input[i])) {

plaintext[len++] = toupper(input[i]) - 'A'; }

while (len % n != 0) {

plaintext[len++] = 'X' - 'A'; }

char choice;

printf("Encrypt or Decrypt? (E/D): ");

scanf(" %c", &choice);

if (choice == 'E' || choice == 'e') {

encrypt(key, plaintext, ciphertext, len, n);

printf("Encrypted text: ");

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

printf("%c", ciphertext[i] + 'A'); }

printf("\n");

} else if (choice == 'D' || choice == 'd') {

decrypt(invKey, plaintext, ciphertext, len, n);

printf("Decrypted text: ");

SSAIET, Computer Dept, Navsari Page 16


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

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

printf("%c", ciphertext[i] + 'A'); }

printf("\n");

} else {

printf("Invalid choice.\n");

return 1; }

return 0; }

int mod26(int n) {

n %= 26;

if (n < 0) n += 26;

return n; }

int determinant(int matrix[MAX][MAX], int n) {

int det = 0;

if (n == 1)

return matrix[0][0];

int temp[MAX][MAX];

int sign = 1;

for (int f = 0; f < n; f++) {

getCofactor(matrix, temp, 0, f, n);

det += sign * matrix[0][f] * determinant(temp, n - 1);

sign = -sign; }

return det; }

void getCofactor(int matrix[MAX][MAX], int temp[MAX][MAX], int


p, int q, int n) {

int i = 0, j = 0;

SSAIET, Computer Dept, Navsari Page 17


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

for (int row = 0; row < n; row++) {

for (int col = 0; col < n; col++) {

if (row != p && col != q) {

temp[i][j++] = matrix[row][col];

if (j == n - 1) {

j = 0;

i++; } } }

} 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; }

void adjoint(int matrix[MAX][MAX], int adj[MAX][MAX], int n) {

if (n == 1) {

adj[0][0] = 1;

return; }

int sign = 1, temp[MAX][MAX];

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

for (int j = 0; j < n; j++) {

getCofactor(matrix, temp, i, j, n);

sign = ((i + j) % 2 == 0) ? 1 : -1;

adj[j][i] = sign * determinant(temp, n - 1);

adj[j][i] = mod26(adj[j][i]); } }

} int inverseMatrix(int matrix[MAX][MAX], int inv[MAX][MAX], int n) {

SSAIET, Computer Dept, Navsari Page 18


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

int det = determinant(matrix, n);

det = mod26(det);

int detInv = modInverse(det, 26);

if (detInv == -1) return 0;

int adj[MAX][MAX];

adjoint(matrix, adj, n);

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

for (int j = 0; j < n; j++) {

inv[i][j] = mod26(adj[i][j] * detInv);

} } return 1; }

void multiplyMatrixVector(int matrix[MAX][MAX], int vector[MAX], int result[MAX], int

n) {

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

int sum = 0;

for (int j = 0; j < n; j++) {

sum += matrix[i][j] * vector[j];

result[i] = mod26(sum);

SSAIET, Computer Dept, Navsari Page 19


Enrollment No. 221230107020 [INFORMATION SECURITY (3170720)]

OUTPUT:-
ENCRYPTION:-

DECRYPTION:-

SSAIET, Computer Dept, Navsari Page 20

You might also like