Professional Documents
Culture Documents
Cns Pratical Ajay
Cns Pratical Ajay
LABORATORY MANUAL
1
CNS(3161606) A1 190160116029
course within the four walls of Government Engineering College, Modasa. This
University.
Date:
2
CNS(3161606) A1 190160116029
List of Experiments
Sr.
Experiments Date Sign Remarks
No.
3
CNS(3161606) A1 190160116029
Practical 1
Aim: Implement Caesar cipher encryption-decryption.
Program:
Encryption:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
Output:
4
CNS(3161606) A1 190160116029
Decrypted:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;}}
printf("Decrypted message: %s", message);
return 0;
}
Output:
5
CNS(3161606) A1 190160116029
Practical:2
Aim: Implement Brute Force attack on Caesar cipher.
Program:
import java.util.Scanner;
public class BruteForce {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j, l, k = 97, key = 0, flag = 0, index = 0, keyVal;
String pt;
char[] ct1 = new char[10];
char[] pt1 = new char[10];
char temp;
System.out.println("ENTER PLAIN TEXT");
pt = sc.next();
System.out.println("ENTER KEY VALUE :");
key = sc.nextInt();
for (i = 0; i < pt.length(); i++) {
for (j = 0; j < 26; j++) {
if (pt.charAt(i) == ' ') {
flag = 0;
break;}
temp = (char) (j + k);
if (pt.charAt(i) == temp) {
flag = 1;
index = j;
break;}}
if (flag == 1) {
char c = (char) (((index + key) % 26) + 97);
6
CNS(3161606) A1 190160116029
ct1[i] = c;}}
System.out.println("ENCRYPTED DATA:");
for (i = 0; i < pt.length(); i++) {
System.out.print(ct1[i]);}
System.out.println("\n" + "DECRYPTION OF DATA USING BRUTE-FORCE ATTACK
:");
key = 1;
while (key <= 26) {
for (i = 0; i < pt.length(); i++) {
for (j = 0; j < 26; j++) {
if (ct1[i] == ' ') {
flag = 0;
break;}
temp = (char) (j + k);
if (ct1[i] == temp) {
flag = 1;
index = j;
break;}}
keyVal = index - key;
if (flag == 1 & keyVal > 0) {
pt1[i] = (char) ((keyVal % 26) + 97);
} else if (flag == 1) {
pt1[i] = (char) ((26 + keyVal) + 97);}}
System.out.print("\n" + "DECRYPTED DATA:");
for (i = 0; i < pt.length(); i++) {
System.out.print(pt1[i]);}
key++;} }}
7
CNS(3161606) A1 190160116029
Output:
8
CNS(3161606) A1 190160116029
Practical:3
Aim: Implement Monoalphabetic cipher encryption-decryption.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char
pt[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char
ct[26]={'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'};
char p[20]={'\0'},c[20]={'\0'},r[20]={'\0'};
int i,j;
clrscr();
printf("\n enter the plain text:");
gets(p);
//converting plain text into cipher text (encryption)
for(i=0;i<strlen(p);i++)
{
for(j=0;j<26;j++)
{
if(pt[j]==p[i])
{
c[i]=ct[j];
}
}
}
printf("\n cipher text is: %s",c);
9
CNS(3161606) A1 190160116029
Output:
10
CNS(3161606) A1 190160116029
Practical:4
Aim: Implement Play fair encryption decryption.
Program:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int removerepeated(int size,int a[]);
int insertelementat(int position,int a[],int size);
main()
{
int i,j,k,numstr[100],numcipher[100],numkey[100],lenkey,templen,tempkey[100],flag=-
1,size,cipherkey[5][5],lennumstr,row1,row2,col1,col2;
char str[100],key[100];
printf("Enter a string\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++){
if(str[i]!=' '){
str[j]=toupper(str[i]);
j++;} }
str[j]='\0';
printf("Entered String is %s\n",str);
size=strlen(str);
for(i=0;i<size;i++) {
if(str[i]!=' ')
numstr[i]=str[i]-'A';}
lennumstr=i;
printf("Enter the key (Non repeated elements if possible)\n");
gets(key);
11
CNS(3161606) A1 190160116029
for(i=0,j=0;i<strlen(key);i++) {
if(key[i]!=' '){
key[j]=toupper(key[i]);
j++;} }
key[j]='\0';
printf("%s\n",key);
k=0;
for(i=0;i<strlen(key)+26;i++) {
if(i<strlen(key)) {
if(key[i]=='J') {
flag=8;
printf("%d",flag); }
numkey[i]=key[i]-'A'; }
else {
if(k!=9 && k!=flag {
numkey[i]=k; }
k++;} }
templen=i;
lenkey=removerepeated(templen,numkey);
printf("Entered key converted according to Play Fair Cipher rule\n");
for(i=0;i<lenkey;i++){
printf("%c",numkey[i]+'A'); }
printf("\n");
k=0;
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
cipherkey[i][j]=numkey[k];
k++;} }
12
CNS(3161606) A1 190160116029
printf("Arranged key\n");
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
printf("%c ",cipherkey[i][j]+'A');}
printf("\n"); }
for(i=0;i<lennumstr;i+=2 {
if(numstr[i]==numstr[i+1]) {
insertelementat(i+1,numstr,lennumstr);
lennumstr++;}}
if(lennumstr%2!=0){
insertelementat(lennumstr,numstr,lennumstr);
lennumstr++;}
printf("Entered String/Message After Processing according to Play fair cipher rule\n");
for(i=0;i<lennumstr;i++){
printf("%c",numstr[i]+'A');}
for(k=0;k<lennumstr;k+=2){
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(numstr[k]==cipherkey[i][j]){
row1=i;
col1=j; }
if(numstr[k+1]==cipherkey[i][j]){
row2=i;
col2=j; } }}
if(row1==row2){
col1=(col1-1)%5;
col2=(col2-1)%5;
if(col1<0){
13
CNS(3161606) A1 190160116029
col1=5+col1;}
if(col2<0){
col2=5+col2;}
numcipher[k]=cipherkey[row1][col1];
numcipher[k+1]=cipherkey[row2][col2];}
if(col1==col2){
row1=(row1-1)%5;
row2=(row2-1)%5;
if(row1<0){
row1=5+row1;}
if(row2<0){
row2=5+row2;}
numcipher[k]=cipherkey[row1][col1];
numcipher[k+1]=cipherkey[row2][col2];}
if(row1!=row2&&col1!=col2){
numcipher[k]=cipherkey[row1][col2];
numcipher[k+1]=cipherkey[row2][col1];}}
printf("\nCipher Text is\n");
for(i=0;i<lennumstr;i++){
if((numcipher[i]+'A')!='X')
printf("%c",numcipher[i]+'A'); }
printf("\n"); }
int removerepeated(int size,int a[]){
int i,j,k;
for(i=0;i<size;i++){
for(j=i+1;j<size;){
if(a[i]==a[j]){
for(k=j;k<size;k++){
14
CNS(3161606) A1 190160116029
a[k]=a[k+1];}
size--;}
else{
j++;} } }
return(size);}
int insertelementat(int position,int a[],int size){
int i,insitem=23,temp[size+1];
for(i=0;i<=size;i++){
if(i<position) {
temp[i]=a[i]; }
if(i>position){
temp[i]=a[i-1]; }
if(i==position){
temp[i]=insitem; } }
for(i=0;i<=size;i++){
a[i]=temp[i]; } }
Output:
15
CNS(3161606) A1 190160116029
Practical:5
Aim: Implementation of Polyalphabetic cipher encryption decryption.
Program:
#include<stdio.h>
#include<string.h>
int main(){
char msg[] = "THECRAZYPROGRAMMER";
char key[] = "HELLO";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;
newKey[i] = key[j];
}
newKey[i] = '\0';
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
encryptedMsg[i] = '\0';
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
decryptedMsg[i] = '\0';
Output:
16
CNS(3161606) A1 190160116029
Practical:6
Aim: Implement Hill cipher encryption decryption.
Program:
#include<stdio.h>
#include<math.h>
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption();
void decryption();
void getKeyMessage();
void inverse();
void main() {
getKeyMessage();
encryption();
decryption(); }
void encryption() {
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
printf("\nEncrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97)); }
void decryption() {
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
printf("\nDecrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));
printf("\n"); }
void getKeyMessage() {
int i, j;
char msg[3];
printf("Enter 3x3 matrix for key (It should be inversible):\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
scanf("%f", &a[i][j]);
c[i][j] = a[i][j]; }
printf("\nEnter a 3 letter string: ");
scanf("%s", msg);
for(i = 0; i < 3; i++)
mes[i][0] = msg[i] - 97;
17
CNS(3161606) A1 190160116029
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
printf("\n\nInverse Matrix is:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", b[i][j]);
printf("\n");
}
}
Output:
18
CNS(3161606) A1 190160116029
Practical:7
Aim: Perform DES or AES encryption-decryption techniques with cryptool
THEORY:
The Data Encryption Standard is a previously predominant symmetric-key algorithm for the
encryption of electronic data. It was highly influential in the advancement of modern
cryptography in the academic world. Developed in the early 1970s at IBM and based on an
earlier design by Horst Feistel, the algorithm was submitted to the National Bureau of Standards
(NBS) following the agency's invitation to propose a candidate for the protection of sensitive,
unclassified electronic government data. In 1976, after consultation with the National Security
Agency (NSA), the NBS eventually selected a slightly modified version, which was published as
an official Federal Information Processing Standard (FIPS) for the United States in 1977. The
publication of an NSA approved encryption standard simultaneously resulted in its quick
international adoption and widespread academic scrutiny. Controversies arose out of classified
design elements, a relatively short key length of the symmetric-key block cipher design, and the
involvement of the NSA, nourishing suspicions about a backdoor. The intense academic scrutiny
the algorithm received over time led to the modern understanding of block ciphers and their
cryptanalysis.
DES FEATURES:
Block size = 64 bits
Key size = 56 bits (in reality, 64 bits, but 8 are used as parity-check bits for error control)
Number of rounds = 16
19
CNS(3161606) A1 190160116029
PROCEDURE:
In this exercise you can use the DES-component to encrypt an arbitrary text entered in the Input
message-component on the left side. The resulting encrypted text is displayed in the Output
message component on the right side after hitting the Play-button. The DES-component works
on binary values, i.e. bytes. Thus, the inputted text is first converted to bytes with the Message
decoder-component. With the current settings, it is interpreted as ASCII. The resulting bytes are
20
CNS(3161606) A1 190160116029
then encrypted with DES, yielding another sequence of bytes. These bytes are then simply
printed as hexadecimal values with the help of the Message encoder-component. Note that you
can also decrypt messages with this template. To do so, you first copy the encrypted hexadecimal
values to the Input message. Then you have to change the following: Set Input format of the
Message decoder to Hexadecimal
Set Action of DES to Decrypt;
Set Format of the Message encoder to Text and the encoding to ASCII.
OBSERVATION:
21
CNS(3161606) A1 190160116029
Step 4: For Decryption Process Enter Cipher Text and the same key value
OBSERVATIONS:
22
CNS(3161606) A1 190160116029
23
CNS(3161606) A1 190160116029
24
CNS(3161606) A1 190160116029
25
CNS(3161606) A1 190160116029
26
CNS(3161606) A1 190160116029
Practical:8
Aim: Implement Diffi-Hellmen key Exchange method.
Program:
#include<stdio.h>
#include<math.h>
int power( int a, int b, int p){
if (b == 1)
return a;
else
return ((( int)pow(a, b)) % p); }
int main() {
int p, g, x, a, y, b, ka, kb;
printf("The value of Prime number : p=");
scanf("%d",&p);
printf("The value of primitive root for p:g= ");
scanf("%d",&g);
printf("The private key a for 1st person :");
scanf("%d",&a);
x = power(g, a, p);
printf("The private key b for 2nd person :");
scanf("%d",&b);
y = power(g, b, p);
ka = power(y, a, p);
kb = power(x, b, p);
printf("Secret key for the 1st person is : %d\n", ka);
printf("Secret Key for the 2nd person is : %d\n", kb);
return 0; }
Output:
27
CNS(3161606) A1 190160116029
Practical:9
Aim: Implement Extended Euclidean algorithm for finding multiplicative
inverse in GF (2^n).
Program:
#include <stdio.h>
int modInverse(int a, int m) {
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
int q = a / m;
int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;}
if (x < 0)
x += m0;
return x; }
int main() {
int a,m;
printf("enter values of a and m");
scanf("%d %d",&a,&m);
printf("Modular multiplicative inverse is %d\n",modInverse(a, m));
return 0;
}
Output:
28
CNS(3161606) A1 190160116029
Practical:10
Aim: Implement RSA encryption-decryption algorithm.
THEORY:
29
CNS(3161606) A1 190160116029
30
CNS(3161606) A1 190160116029
31
CNS(3161606) A1 190160116029
Practical:11
Aim: Perform techniques SHA-1 has and digital signature algorithm with
cryptool.
32
CNS(3161606) A1 190160116029
5. After this click on Generate prime numbers. Keep the things as default and click on
Generate prime numbers.
33
CNS(3161606) A1 190160116029
6. As you can see 2 prime numbers are generated, just store this numbers by clicking on Store
key.
7. Now click on Create certificate in flowchart and fill the required information and click on
Create certificate and PSE.
34
CNS(3161606) A1 190160116029
35
CNS(3161606) A1 190160116029
The signature of the document is created using the hash function SHA-1.
36
CNS(3161606) A1 190160116029
Practical:12
Aim: Study and use the Wireshark for the various network protocols.
Wireshark: -
Wireshark is an open source network protocol analysis software program started by Gerald
Combs in 1998.
It is the most popular network analyzer that comes pre-installed in Kali Linux (It can
downloaded in Windows too). It can be used as the best tool for packet sniffing too.
It captures network traffic from Ethernet, Bluetooth, Wireless (IEEE.802.11), Token Ring,
Frame Relay connections, and more.
Widely-used network protocol analyzer. It lets you see what’s happening on your
network at a microscopic level.
Wireshark listens to a network connection in real time and then grabs entire streams of
traffic. Quite possibly tens of thousands of packets at a time.
It is supported in Windows, Linux, Mac OS, Solaris and others....
Packet sniffers: -
Packet sniffers also called a packet analyzer is a piece of hardware or software used to
monitor network traffic. Hackers use packet sniffers for less noble purposes, such as
spying on network user traffic and collecting passwords.
Packet sniffers can be used on both wired and wireless networks, their efficacy depends
on how much they are able to see as a result of network security.
Outward-facing sniffers scan incoming network traffic for specific of malicious code,
helping to prevent computer virus infections and limit the spread of malware.
37
CNS(3161606) A1 190160116029
Step-3: Now to launch Wireshark, open the Wireshark and click on the blue button of sharks
back like structure to start the network capturing.
Step-4: To stop the capturing the network traffic just click on the square red colored button.
Filters in WIRESHARK: -
Wireshark provides a display filter language that enables you to precisely control which packets
are displayed. They can be used to check for the presence of a protocol or field, the value of a
field, or even compare two fields to each other. These comparisons can be combined with logical
operators like “and” and “or” and parenthesis into complex expressions.
38
CNS(3161606) A1 190160116029
Filters: -
1. IP Address: ip.addr == 192.168.1.101 or 192.168.0.1/24
2. Source address: ip.src == 198.168.0.1
3. Destination: ip.dst == 192.168.0.1
4. Port Number: tcp.port == 80 or udp.port == 80
5. Protocol: dms, http, ftp, arp, ssh, telnet, icmp
6. MAC: eth.addr = 00:50:7f:c5:b6:78
7. Find files by type: frames contains (attachment|tar|exe|zip|pdf)
Colors in Wireshark:
Wireshark uses colors to help you identify the types at a glance.
i. Black with red letters – TCP packets with errors
ii. Green – HTTP Packets
iii. Light Blue – UDP Packets
iv. Pale Blue – ARP Packets
v. Lavender – ICMP Packets
vi. Black with green letters – ICMP Packets with errors
vii. Light Purple – TCP
viii. Black – Packet with errors
ix. Light Yellow – Windows specific traffic including Server Message Blocks and
NETBIOS.
x. Dark Yellow – Routing
xi. Dark gray – TCP SYN, FIN and ACK traffic.
39
CNS(3161606) A1 190160116029
40