0% found this document useful (0 votes)
10 views16 pages

CNS prac YATHARTH

The document outlines several practical exercises related to cryptography and network security, including implementations of various encryption techniques such as XOR operations, Caesar Cipher, Mono-alphabetic Cipher, Hill Cipher, Vigenere Cipher, Playfair Cipher, and Rail Fence Cipher. Each practical includes a clear aim, corresponding C code, and instructions for encryption and decryption processes. The exercises are designed to enhance understanding of cryptographic principles through hands-on coding experience.
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)
10 views16 pages

CNS prac YATHARTH

The document outlines several practical exercises related to cryptography and network security, including implementations of various encryption techniques such as XOR operations, Caesar Cipher, Mono-alphabetic Cipher, Hill Cipher, Vigenere Cipher, Playfair Cipher, and Rail Fence Cipher. Each practical includes a clear aim, corresponding C code, and instructions for encryption and decryption processes. The exercises are designed to enhance understanding of cryptographic principles through hands-on coding experience.
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/ 16

IU2241230105 Cryptography and Network Security

Practical - 1
AIM: A. Write a C program that contains a string (char pointer) with a value 'Hello
World’. The program should XOR, AND and OR each character in this string with 0
and displays the result.
B. Write a C program that contains a string (char pointer) with a value 'Hello
World’. The program should XOR, AND and OR each character in this string with
127 and displays the result.
C. Write a C program that contains a string (char pointer) with a value 'Hello
World’. The program should bitwise OR, left shift and right shift each character in
this string and displays the result.
Code:
#include <stdio.h>
#include <string.h>

void bitwise_operations(char *str, int num) {


printf("\nBitwise operations with %d:\n", num);
for (int i = 0; i < strlen(str); i++) {
char ch = str[i];
printf("Char: %c (ASCII: %d) | XOR: %d | AND: %d | OR: %d\n",
ch, ch, ch ^ num, ch & num, ch | num);
}
}

void shift_operations(char *str) {


printf("\nBitwise Shift Operations:\n");
for (int i = 0; i < strlen(str); i++) {
char ch = str[i];
printf("Char: %c (ASCII: %d) | OR: %d | Left Shift: %d | Right Shift: %d\n",
ch, ch, ch | 1, ch << 1, ch >> 1);
}
}

int main() {

6CSE-E1 1
IU2241230105 Cryptography and Network Security

char *text = "Hello World"; printf("Original

String: %s\n", text);

bitwise_operations(text, 0);
bitwise_operations(text, 127);
shift_operations(text);

return 0;
}
Output:

6CSE-E1 2
IU2241230105 Cryptography and Network Security

6CSE-E1 3
IU2241230105 Cryptography and Network Security

Practical - 2
AIM: To implement Caesar Cipher Encryption – Decryption.
Code:
#include <stdio.h>
#include <string.h>

void caesarEncrypt(char text[], int shift) {


for (int i = 0; i < strlen(text); i++) {
if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = ((text[i] - 'A' + shift) % 26) + 'A';
} else if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = ((text[i] - 'a' + shift) % 26) + 'a';
}
}
}

void caesarDecrypt(char text[], int shift) {


for (int i = 0; i < strlen(text); i++) {
if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = ((text[i] - 'A' - shift + 26) % 26) + 'A';
} else if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = ((text[i] - 'a' - shift + 26) % 26) + 'a';
}
}
}

int main() {
char text[100];
int shift;

printf("Enter text: ");

6CSE-E1 4
IU2241230105 Cryptography and Network Security

scanf("%s", text);
printf("Enter shift value: ");
scanf("%d", &shift);

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

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

return 0;
}

6CSE-E1 5
IU2241230105 Cryptography and Network Security

Practical - 3
AIM: To implement Mono-alphabetic Cipher Encryption – Decryption.
Code:
#include <stdio.h>
#include <string.h>

#define ALPHABET_SIZE 26

char key[ALPHABET_SIZE] = "QWERTYUIOPASDFGHJKLZXCVBNM";

void monoEncrypt(char text[]) {


for (int i = 0; text[i] != '\0'; i++) {
if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = key[text[i] - 'A'];
} else if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = key[text[i] - 'a'] + ('a' - 'A');
}
}
}

void monoDecrypt(char text[]) {


for (int i = 0; text[i] != '\0'; i++) {
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')) {
for (int j = 0; j < ALPHABET_SIZE; j++) {
if (text[i] == key[j] || text[i] == key[j] + ('a' - 'A')) {
text[i] = j + (text[i] >= 'a' ? 'a' : 'A');
break;
}
}
}

6CSE-E1 6
IU2241230105 Cryptography and Network Security

}
}

int main() {
char text[100];

printf("Enter text: ");


scanf("%99s", text);

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

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

return 0;
}
Output:

6CSE-E1 7
IU2241230105 Cryptography and Network Security

Practical - 4
AIM: To implement Hill Cipher Encryption.
Code:
#include <stdio.h>
#include <string.h>
#define MATRIX_SIZE 2

void matrixMultiply(int key[MATRIX_SIZE][MATRIX_SIZE], int text[MATRIX_SIZE],


int result[MATRIX_SIZE]) {
for (int i = 0; i < MATRIX_SIZE; i++) {
result[i] = 0;
for (int j = 0; j < MATRIX_SIZE; j++) {
result[i] += key[i][j] * text[j];
}
result[i] %= 26;
}
}

void hillEncrypt(char text[], int key[MATRIX_SIZE][MATRIX_SIZE]) {


int textVector[MATRIX_SIZE], resultVector[MATRIX_SIZE];
for (int i = 0; i < strlen(text); i += MATRIX_SIZE) {
for (int j = 0; j < MATRIX_SIZE; j++) {
textVector[j] = text[i + j] - 'A';
}
matrixMultiply(key, textVector, resultVector);
for (int j = 0; j < MATRIX_SIZE; j++) {
text[i + j] = resultVector[j] + 'A';
}
}
}

6CSE-E1 8
IU2241230105 Cryptography and Network Security

int main() {
char text[100];
int key[MATRIX_SIZE][MATRIX_SIZE] = {{6, 24}, {1, 13}};

printf("Enter text (in uppercase, multiple of %d letters): ", MATRIX_SIZE);


scanf("%s", text);

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

return 0;
}
Output:

6CSE-E1 9
IU2241230105 Cryptography and Network Security

Practical - 5
AIM: To implement Poly-alphabetic Cipher (Vigener Cipher) Technique.
Code:
#include <stdio.h>
#include <string.h>

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


int textLen = strlen(text), keyLen = strlen(key);
for (int i = 0, j = 0; i < textLen; i++, j++) {
if (j == keyLen) j = 0;
if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = ((text[i] - 'A' + (key[j] - 'A')) % 26) + 'A';
} else if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = ((text[i] - 'a' + (key[j] - 'a')) % 26) + 'a';
}
}
}

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


int textLen = strlen(text), keyLen = strlen(key);
for (int i = 0, j = 0; i < textLen; i++, j++) {
if (j == keyLen) j = 0;
if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = ((text[i] - 'A' - (key[j] - 'A') + 26) % 26) + 'A';
} else if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = ((text[i] - 'a' - (key[j] - 'a') + 26) % 26) + 'a';
}
}
}

int main() {

6CSE-E1 10
IU2241230105 Cryptography and Network Security

char text[100], key[100];

printf("Enter text: ");


scanf("%s", text);
printf("Enter key: ");
scanf("%s", key);

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

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

return 0;
}
Output:

6CSE-E1 11
IU2241230105 Cryptography and Network Security

Practical - 6
AIM: To implement Play-Fair Cipher Technique.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 5

char keyTable[SIZE][SIZE];

void generateKeyTable(char key[]) {


int map[26] = {0}, k = 0;
char filled[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

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


if (key[i] == 'J') key[i] = 'I';
if (!map[key[i] - 'A']) {
keyTable[k / SIZE][k % SIZE] = key[i];
map[key[i] - 'A'] = 1;
k++;
}
}
for (int i = 0; i < 26; i++) {
if (filled[i] == 'J') continue;
if (!map[filled[i] - 'A']) {
keyTable[k / SIZE][k % SIZE] = filled[i];
map[filled[i] - 'A'] = 1;
k++;
}
}
}

6CSE-E1 12
IU2241230105 Cryptography and Network Security

void findPosition(char c, int *row, int *col) {


if (c == 'J') c = 'I';
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (keyTable[i][j] == c) {
*row = i;
*col = j;
return;
}
}
}
}

void playfairEncrypt(char text[]) {


int len = strlen(text);
for (int i = 0; i < len; i += 2) {
int r1, c1, r2, c2;
findPosition(text[i], &r1, &c1);
findPosition(text[i + 1], &r2, &c2);

if (r1 == r2) {
text[i] = keyTable[r1][(c1 + 1) % SIZE];
text[i + 1] = keyTable[r2][(c2 + 1) % SIZE];
} else if (c1 == c2) {
text[i] = keyTable[(r1 + 1) % SIZE][c1];
text[i + 1] = keyTable[(r2 + 1) % SIZE][c2];
} else {
text[i] = keyTable[r1][c2];
text[i + 1] = keyTable[r2][c1]}

6CSE-E1 13
IU2241230105 Cryptography and Network Security

}
}

int main() {
char key[26], text[100];

printf("Enter key (no spaces, uppercase): ");


scanf("%s", key);
printf("Enter text (uppercase, even length): ");
scanf("%s", text);

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

return 0;
}
Output:

6CSE-E1 14
IU2241230105 Cryptography and Network Security

Practical - 7
AIM: To implement Caesar Cipher Encryption – Decryption.
Code:
#include <stdio.h>
#include <string.h>

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


int len = strlen(text);
if (key <= 1) {
printf("Encrypted Text: %s\n", text);
return;
}

char rail[key][len];
memset(rail, '\n', sizeof(rail));

int row = 0, dir = 1;


for (int i = 0; i < len; i++) {
rail[row][i] = text[i];
if (row == 0)
dir = 1;
else if (row == key - 1)
dir = -1;
row += dir;
}

printf("Encrypted Text: ");


for (int i = 0; i < key; i++)
for (int j = 0; j < len; j++)
if (rail[i][j] != '\n')
printf("%c", rail[i][j]);\

6CSE-E1 15
IU2241230105 Cryptography and Network Security

printf("\n");
}

int main() {
char text[100];
int key;

printf("Enter text: ");


scanf(" %[^\n]s", text);
printf("Enter key: ");
scanf("%d", &key);

railFenceEncrypt(text, key);

return 0;
}
Output:

6CSE-E1 16

You might also like