CNS Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

ST.

MARY’S GROUP OF INSTITUTIONS GUNTUR


(Approved by AICTE &Govt .of AP, Affiliated to JNTU-KAKINADA, Accredited by 'NAAC')
Chebrolu (V&M), Guntur (Dist), Andhra Pradesh, INDIA-522212

SCHOOL OF ENGINEERING

PRACTICAL RECORD

Name of the student: __________________________________________________

Course:______________Branch:____________Reg.No:______________________

Year:_____________ Semester:_______________ Regulation:_________________

Name of Laboratory:___________________________________________________
ST.MARY’S GROUP OF INSTITUTIONS GUNTUR
(Approved by AICTE &Govt .of AP, Affiliated to JNTU-KAKINADA, Accredited by 'NAAC')
Chebrolu (V&M), Guntur (Dist), Andhra Pradesh, INDIA-522212

SCHOOL OF ENGINEERING

Certificate
This is to certify that Mr. / Ms.

bearing roll no of _______B. Tech semester

branch has satisfactorily

completed laboratory during the

academic year .

Place: Signature of Faculty


Date:

External Practical Examination held on:______________________

Signature of HOD

Signature of Internal Examiner Signature of External Examiner


ST.MARY’S GROUP OF INSTITUTIONS GUNTUR
(Approved by AICTE &Govt .of AP, Affiliated to JNTU-KAKINADA, Accredited by 'NAAC')
Chebrolu (V&M), Guntur (Dist), Andhra Pradesh, INDIA-522212

Index
Signature of
S.No Name of the Program Page No. Date
the Faculty
Lab 1: Implementation of Caesar Cipher technique

# include<stdio.h>

#include<ctype.h>

int main() {

char text[500], ch;

int key;

// Taking user input.


printf("Enter a message to encrypt: ");

scanf("%s", text);

printf("Enter the key: ");

scanf("%d", & key);

// Visiting character by character.

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

ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {

//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}

// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// Invalid character.
else {
printf("Invalid Message");
}
// Adding encoded answer.
text[i] = ch;

printf("Encrypted message: %s", text);

return 0;
}

Output:
Enter a message to encrypt: yZq8NS92mdR
Enter the key: 6
Encrypted message: eFw4TY58sjX

#include<stdio.h>

#include<ctype.h>

int main() {

char text[500], ch;

int key;

// Taking user input.

printf("Enter a message to decrypt: ");

scanf("%s", text);

printf("Enter the key: ");

scanf("%d", & key);

// Visiting each character.


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

ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {
//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
}
// Invalid characters.
else {
printf("Invalid Message");
}
// Adding decoded character back.
text[i] = ch;

printf("Decrypted message: %s", text);

return 0;

Output:
Enter a message to decrypt: eFw4TY58sjX
Enter the key: 6
Decrypted message: yZq8NS92mdR

Lab 2: Implement the Play fair Cipher

//PLAY FAIR CIPHER


// C program to implement Playfair Cipher

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

#define SIZE 30

// Function to 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;
}
}

// Function to remove all spaces in a string


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

// Function to generate the 5x5 key square


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

// a 26 character hashmap
// to 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;
}
}
}
}

// Function to search for the characters of a digraph


// in the key square and return their position
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;
}
}
}
}

// Function to find the modulus with 5


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

// Function to make the plain text length to be even


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

// Function for performing the encryption


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

// Function to encrypt using Playfair Cipher


void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];

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

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

ps = prepare(str, ps);

generateKeyTable(key, ks, keyT);

encrypt(str, keyT, ps);


}
// Driver code
int main()
{
char str[SIZE], key[SIZE];

// Key to be encrypted
strcpy(key, "HOWAREYOU");
printf("Key text: %s\n", key);

// Plaintext to be encrypted
strcpy(str, "NEVERGIVEUP");
printf("Plain text: %s\n", str);

// encrypt using Playfair Cipher


encryptByPlayfairCipher(str, key);

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

return 0;
}
Output:
Key text: HOWAREYOU
Plain text: NEVERGIVEUP
Cipher text: lusuwkgxybqx

Lab 3: Implement the Pure Transposition Cipher

#include<stdio.h>
int check(int x,int y)
{
int a,b,c;

if(x%y==0)
return 0;

a=x/y;
b=y*(a+1);
c=b-x;
return c;
}
void main()
{
int l1,i,d,j;

printf("\nEnter the length of the key. ");


scanf("%d",&l1);
int sequence[l1];
printf("\nEnter the sequence key. ");
for(i=0;i<l1;++i)
{
scanf("%d",&sequence[i]);
}

int order[l1];
for(i=1;i<=l1;++i)
{
for(j=0;j<l1;++j)
{
if(sequence[j]==i)
order[i-1]=j;
}
}

printf("\nEnter the depth. ");


scanf("%d",&d);

int l2;
printf("\nEnter the length of String without spaces . ");
scanf("%d",&l2);
int temp1=check(l2,l1);

int r=(l2+temp1)/l1;

char p[l2+temp1];
char p1[r][l1];
//char p2[r][l1];
if(temp1>0)
printf("\nYou need to enter %d bogus characters.So enter total %d characters. ",temp1,(l2+temp1));
else
printf("\nEnter the string. ");

for(i=-1;i<(l2+temp1);++i)
{
scanf("%c",&p[i]);
}
int count=0;
while(d>0)
{
count=0;

for(i=0;i<r;++i)
{
for(j=0;j<l1;++j)
{
p1[i][j]=p[count];
count=count+1;
}
}

printf("\n\n\n");
for(i=0;i<r;++i)
{
for(j=0;j<l1;++j)
{
printf("%c ",p1[i][j]);
}
printf("\n");
}

count=0;
for(i=0;i<l1;++i)
{
for(j=0;j<r;++j)
{
p[count]=p1[j][order[i]];
count=count+1;
}
}

for(i=0;i<(l2+temp1);++i)
printf("%c ",p[i]);

d=d-1;
}
}

OUTPUT
[prat@localhost Desktop]$ gcc transposition.c
[prat@localhost Desktop]$ ./a.out

Enter the length of the key. 7

Enter the sequence key. 4 3 1 2 5 6 7

Enter the depth. 2

Enter the length of String without spaces . 23

You need to enter 5 bogus characters.So enter total 28 characters. attackpostponeduntiltwoamxyz


attackp
ostpone
duntilt
woamxyz

ttnaaptmtsuoaodwcoixknlypetz

ttnaapt
mtsuoao
dwcoixk
nlypetz

nscyauopttwltmdnaoiepaxttokz

Lab 4: Implement DES Encryption and Decryption

// Define DES key and plaintext

const key = "0123456789abcdef";

const plaintext = "Hello, world!";

// Perform DES encryption

const des = new DES(key);

const ciphertext = des.encrypt(plaintext);

// Perform DES decryption

const decrypted = des.decrypt(ciphertext);

// Print results

console.log("Plaintext: ", plaintext);

console.log("Ciphertext: ", ciphertext);

console.log("Decrypted: ", decrypted);


// Define DES class

class DES {

constructor(key) {

// Initialize DES with key

this.key = CryptoJS.enc.Hex.parse(key);

encrypt(plaintext) {

// Perform DES encryption on plaintext

const encrypted = CryptoJS.DES.encrypt(

plaintext,

this.key,

{ mode: CryptoJS.mode.ECB }

);

// Return ciphertext as hex string

return encrypted.ciphertext.toString();

decrypt(ciphertext) {

// Parse ciphertext from hex string

const ciphertextHex = CryptoJS.enc.Hex.parse(ciphertext);


// Perform DES decryption on ciphertext

const decrypted = CryptoJS.DES.decrypt(

{ ciphertext: ciphertextHex },

this.key,

{ mode: CryptoJS.mode.ECB }

);

// Return decrypted plaintext as UTF-8 string

return decrypted.toString(CryptoJS.enc.Utf8);

Output
...60AF7CA5
Round 12 FF3C485F 22A5963B C2C1E96A4BF3
Round 13 22A5963B 387CCDAA 99C31397C91F
Round 14 387CCDAA BD2DD2AB 251B8BC717D0
Round 15 BD2DD2AB CF26B472 3330C5D9A36D
Round 16 19BA9212 CF26B472 181C5D75C66D

Cipher Text: C0B7A8D05F3A829C

Decryption

After initial permutation: 19BA9212CF26B472


After splitting: L0=19BA9212 R0=CF26B472
Round 1 CF26B472 BD2DD2AB 181C5D75C66D
Round 2 BD2DD2AB 387CCDAA 3330C5D9A36D
Round 3 387CCDAA 22A5963B 251B8BC717D0
Round 4 22A5963B FF3C485F 99C31397C91F
Round 5 FF3C485F 6CA6CB20 C2C1E96A4BF3
Round 6 6CA6CB20 10AF9D37 6D5560AF7CA5
Round 7 10AF9D37 308BEE97 02765708B5BF
Round 8 308BEE97 A9FC20A3 84BB4473DCCC
Round 9 A9FC20A3 2E8F9C65 34F822F0C66D
Round 10 2E8F9C65 A15A4B87 708AD2DDB3C0
Round 11 A15A4B87 236779C2 C1948E87475E
Round 12 236779C2 B8089591 69A629FEC913
Round 13 B8089591 4A1210F6 DA2D032B6EE3
Round 14 4A1210F6 5A78E394 06EDA4ACF5B5
Round 15 5A78E394 18CA18AD 4568581ABCCE
Round 16 14A7D678 18CA18AD 194CD072DE8C

Plain Text: 123456ABCD132536


Output:
Encryption:

After initial permutation: 14A7D67818CA18AD


After splitting: L0=14A7D678 R0=18CA18AD

Round 1 18CA18AD 5A78E394 194CD072DE8C


Round 2 5A78E394 4A1210F6 4568581ABCCE
Round 3 4A1210F6 B8089591 06EDA4ACF5B5
Round 4 B8089591 236779C2 DA2D032B6EE3
Round 5 236779C2 A15A4B87 69A629FEC913
Round 6 A15A4B87 2E8F9C65 C1948E87475E
Round 7 2E8F9C65 A9FC20A3 708AD2DDB3C0
Round 8 A9FC20A3 308BEE97 34F822F0C66D
Round 9 308BEE97 10AF9D37 84BB4473DCCC
Round 10 10AF9D37 6CA6CB20 02765708B5BF
Round 11 6CA6CB20 FF3C485F 6D5560AF7CA5
Round 12 FF3C485F 22A5963B C2C1E96A4BF3
Round 13 22A5963B 387CCDAA 99C31397C91F
Round 14 387CCDAA BD2DD2AB 251B8BC717D0
Round 15 BD2DD2AB CF26B472 3330C5D9A36D
Round 16 19BA9212 CF26B472 181C5D75C66D

Cipher Text: C0B7A8D05F3A829C

Decryption

After initial permutation: 19BA9212CF26B472


After splitting: L0=19BA9212 R0=CF26B472

Round 1 CF26B472 BD2DD2AB 181C5D75C66D


Round 2 BD2DD2AB 387CCDAA 3330C5D9A36D
Round 3 387CCDAA 22A5963B 251B8BC717D0
Round 4 22A5963B FF3C485F 99C31397C91F
Round 5 FF3C485F 6CA6CB20 C2C1E96A4BF3
Round 6 6CA6CB20 10AF9D37 6D5560AF7CA5
Round 7 10AF9D37 308BEE97 02765708B5BF
Round 8 308BEE97 A9FC20A3 84BB4473DCCC
Round 9 A9FC20A3 2E8F9C65 34F822F0C66D
Round 10 2E8F9C65 A15A4B87 708AD2DDB3C0
Round 11 A15A4B87 236779C2 C1948E87475E
Round 12 236779C2 B8089591 69A629FEC913
Round 13 B8089591 4A1210F6 DA2D032B6EE3
Round 14 4A1210F6 5A78E394 06EDA4ACF5B5
Round 15 5A78E394 18CA18AD 4568581ABCCE
Round 16 14A7D678 18CA18AD 194CD072DE8C

Plain Text: 123456ABCD132536

Lab 5: Implement the AES Encryption and decryption

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;

public class AESEncryptionDecryption {

public static void main(String[] args) {


try{
Cipher cipher = Cipher.getInstance("AES");
KeyGenerator kg = KeyGenerator.getInstance("AES");
Key key = kg.generateKey();

cipher.init(Cipher.ENCRYPT_MODE, key);
CipherInputStream cipt=new CipherInputStream(new FileInputStream(new
File("D:\\PlainTextInput.txt")), cipher);
FileOutputStream fip=new FileOutputStream(new File("D:\\EncryptedText.txt"));

int i;
while((i=cipt.read())!=-1)
{
fip.write(i);
}

cipher.init(Cipher.DECRYPT_MODE, key);
CipherInputStream ciptt=new CipherInputStream(new FileInputStream(new
File("D:\\EncryptedText.txt")), cipher);
FileOutputStream fop=new FileOutputStream(new File("D:\\DecryptedText.txt"));

int j;
while((j=ciptt.read())!=-1)
{
fop.write(j);
}

}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Encryption and Decryption of plain text file performed successfully.");
}
}

Output of the program:

Encryption and Decryption of plain text file performed successfully.

Lab 6: Implement RSA Encryption Algorithm

1. #include<stdio.h>
2. #include<conio.h>
3. #include<stdlib.h>
4. #include<math.h>
5. #include<string.h>
6.
7. long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
8. char msg[100];
9. int prime(long int);
10. void ce();
11. long int cd(long int);
12. void encrypt();
13. void decrypt();
14. void main()
15. {
16. printf("\nENTER FIRST PRIME NUMBER\n");
17. scanf("%d", &p);
18. flag = prime(p);
19. if (flag == 0)
20. {
21. printf("\nWRONG INPUT\n");
22. getch();
23. exit(1);
24. }
25. printf("\nENTER ANOTHER PRIME NUMBER\n");
26. scanf("%d", &q);
27. flag = prime(q);
28. if (flag == 0 || p == q)
29. {
30. printf("\nWRONG INPUT\n");
31. getch();
32. exit(1);
33. }
34. printf("\nENTER MESSAGE\n");
35. fflush(stdin);
36. scanf("%s", msg);
37. for (i = 0; msg[i] != NULL; i++)
38. m[i] = msg[i];
39. n = p * q;
40. t = (p - 1) * (q - 1);
41. ce();
42. printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
43. for (i = 0; i < j - 1; i++)
44. printf("\n%ld\t%ld", e[i], d[i]);
45. encrypt();
46. decrypt();
47. }
48. int prime(long int pr)
49. {
50. int i;
51. j = sqrt(pr);
52. for (i = 2; i <= j; i++)
53. {
54. if (pr % i == 0)
55. return 0;
56. }
57. return 1;
58. }
59. void ce()
60. {
61. int k;
62. k = 0;
63. for (i = 2; i < t; i++)
64. {
65. if (t % i == 0)
66. continue;
67. flag = prime(i);
68. if (flag == 1 && i != p && i != q)
69. {
70. e[k] = i;
71. flag = cd(e[k]);
72. if (flag > 0)
73. {
74. d[k] = flag;
75. k++;
76. }
77. if (k == 99)
78. break;
79. }
80. }
81. }
82. long int cd(long int x)
83. {
84. long int k = 1;
85. while (1)
86. {
87. k = k + t;
88. if (k % x == 0)
89. return (k / x);
90. }
91. }
92. void encrypt()
93. {
94. long int pt, ct, key = e[0], k, len;
95. i = 0;
96. len = strlen(msg);
97. while (i != len)
98. {
99. pt = m[i];
100. pt = pt - 96;
101. k = 1;
102. for (j = 0; j < key; j++)
103. {
104. k = k * pt;
105. k = k % n;
106. }
107. temp[i] = k;
108. ct = k + 96;
109. en[i] = ct;
110. i++;
111. }
112. en[i] = -1;
113. printf("\nTHE ENCRYPTED MESSAGE IS\n");
114. for (i = 0; en[i] != -1; i++)
115. printf("%c", en[i]);
116. }
117. void decrypt()
118. {
119. long int pt, ct, key = d[0], k;
120. i = 0;
121. while (en[i] != -1)
122. {
123. ct = temp[i];
124. k = 1;
125. for (j = 0; j < key; j++)
126. {
127. k = k * ct;
128. k = k % n;
129. }
130. pt = k + 96;
131. m[i] = pt;
132. i++;
133. }
134. m[i] = -1;
135. printf("\nTHE DECRYPTED MESSAGE IS\n");
136. for (i = 0; m[i] != -1; i++)
137. printf("%c", m[i]);
138. }

Output:

ENTER FIRST PRIME NUMBER: 7

ENTER ANOTHER PRIME NUMBER: 19

ENTER MESSAGE: Dharmendra

POSSIBLE VALUES OF e AND d ARE

5 65
11 59
13 25
17 89
23 47
29 41
31 7
37 73
41 29
THE ENCRYPTED MESSAGE IS
=’a…º¢É½…a
THE DECRYPTED MESSAGE IS
Dharmendra

Lab 7: Implementation of Hash Functions

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

#define SIZE 20

struct DataItem {
int data;
int key;
};
struct DataItem* hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;

int hashCode(int key) {


return key % SIZE;
}

struct DataItem *search(int key) {


//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

void insert(int key,int data) {

struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));


item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

hashArray[hashIndex] = item;
}

struct DataItem* delete(struct DataItem* item) {


int key = item->key;
//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];

//assign a dummy item at deleted position


hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

void display() {
int i = 0;

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

if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}

printf("\n");
}

int main() {
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = -1;
dummyItem->key = -1;

insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);
display();
item = search(37);

if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}

delete(item);
item = search(37);

if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
}

Output
~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44) (13,78) (14,32) ~~ ~~
(17,11) (37,97) ~~
Element found: 97
Element not found

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy