CNS Lab Manual
CNS Lab Manual
CNS Lab Manual
SCHOOL OF ENGINEERING
PRACTICAL RECORD
Course:______________Branch:____________Reg.No:______________________
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.
academic year .
Signature of HOD
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() {
int key;
scanf("%s", text);
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;
return 0;
}
Output:
Enter a message to encrypt: yZq8NS92mdR
Enter the key: 6
Encrypted message: eFw4TY58sjX
#include<stdio.h>
#include<ctype.h>
int main() {
int key;
scanf("%s", text);
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;
return 0;
Output:
Enter a message to decrypt: eFw4TY58sjX
Enter the key: 6
Decrypted message: yZq8NS92mdR
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
// 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;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
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]];
}
}
}
// 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);
// 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);
return 0;
}
Output:
Key text: HOWAREYOU
Plain text: NEVERGIVEUP
Cipher text: lusuwkgxybqx
#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;
int order[l1];
for(i=1;i<=l1;++i)
{
for(j=0;j<l1;++j)
{
if(sequence[j]==i)
order[i-1]=j;
}
}
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
ttnaaptmtsuoaodwcoixknlypetz
ttnaapt
mtsuoao
dwcoixk
nlypetz
nscyauopttwltmdnaoiepaxttokz
// Print results
class DES {
constructor(key) {
this.key = CryptoJS.enc.Hex.parse(key);
encrypt(plaintext) {
plaintext,
this.key,
{ mode: CryptoJS.mode.ECB }
);
return encrypted.ciphertext.toString();
decrypt(ciphertext) {
{ ciphertext: ciphertextHex },
this.key,
{ mode: CryptoJS.mode.ECB }
);
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
Decryption
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;
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.");
}
}
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:
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
#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;
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
return NULL;
}
hashArray[hashIndex] = item;
}
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
return NULL;
}
void display() {
int i = 0;
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