Information Security
Information Security
Aim:Write a program for Ceaser cipher ,Playfair & Railfence encryption and decryption
using files
Procedure:
Algorithm Encryption:
3. Read one by one character of file-1 and call encrypt function write the cipher character in
file2.
Algorithm Decryption:
Note: Use an integer digit from 1-26 key. The same key is used for Encryption and
Decryption.
Encrypt function:
if character is between A to Z .
code = character + key;
cipher_character = to_char(code);
Decryption function:
if character is between A to Z .
code=code+26;
original_character = to_char(code);
Code:
#include<stdio.h>
void decrypt(){
char message[100], ch;
int i, key;
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
message[i] = ch;
}
}
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\n", message);
decrypt();
return 0;
}
Output:-
1.b
Procedure:
To Generate the key matrix take any random key of any length and form a 5X5matrix. Go on
filling the rows of the matrix with the key characters (if repeating character occurs then
ignore it). Fill the remaining matrix with alphabets from A to Z(except those already occurred
in the key). For example for the key “monarchy” we have the matrix as follow
Step 2: Encrypt the data using encryption rule and key matrix
To Encrypt the data take two characters at time from plain text file and encrypt itusing one of
the following rules.
Encryption rules
1) Repeating plain text letters that would fall in the same pair are separated withfiller
letter,such as x.( i.e. Balloon becomes Ba, lx, lo, on).
2) If both the characters are in the same raw then replace each with the character toits right,
with
3) If both the characters are in the same column then replace each with the characterbelow it,
withthe bottom character followed by the top, in the matrix.
4) Otherwise each plain text letter is replaced by the letter that lies in its own rowand the
column occupied by the other plain text letter
- Encryption of AR as RM
- Encryption of MU as CM
- Encryption of BP as IM
For this solution we have to implement the following functions given below.
2) Matrix generation.
CODE:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MX 5
char keystr[10];
int ind = 0;
int *removeDuplicate(int n)
int i;
int j;
if (keystr[i] == keystr[j])
break;
if (j == i)
keystr[ind++] = keystr[i];
return 0;
int i, j, w, x, y, z;
FILE * out;
printf("File Currupted.");
}
if (ch1 == key[i][j])
w = i;
x = j;
y = i;
z = j;
if (w == y)
x = (x + 1) % 5;
z = (z + 1) % 5;
printf("%c%c", key[w][x], key[y][z]);
else if (x == z)
w = (w + 1) % 5;
y = (y + 1) % 5;
else
fclose(out);
int i, j, w, x, y, z;
FILE * out;
printf("File Currupted.");
if (ch1 == key[i][j])
w = i;
x = j;
y = i;
z = j;
}
if (w == y)
x = (x - 1) % 5;
z = (z - 1) % 5;
else if (x == z)
w = (w - 1) % 5;
y = (y - 1) % 5;
else
fclose(out);
}
int main()
int i, j, k = 0, m = 0, n;
char alpa[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'};
printf("\nEnter key:");
gets(keystr);
n = strlen(keystr);
removeDuplicate( n);
n = ind;
keystr[i] = toupper(keystr[i]);
gets(str);
//convert all the characters of plaintext to uppertext
str[i] = toupper(str[i]);
j = 0;
if (k == n)
keyminus[j] = alpa[i];
j++;
}
}
k = 0;
if (k < n)
key[i][j] = keystr[k];
k++;
else
key[i][j] = keyminus[m];
m++;
printf("\n");
}
printf("1 encription\n2 decription");
int z;
scanf("%d",&z);
if(z==1)
else
else
i++;
}
}
else if(z==2)
else
else
i++;
}
}
return 0;
Output:
1.3
AIM:- Implementation of encryption and decryption using railfence cipher
Procedure:
Rail Fence cipher is a Transposition cipher. Encryption is the result by changing the position
of the message. In this particular scheme the message is written in two rows. That is the first
character is written in the first row, second character is written in the second row and so on.
To get the cipher read the message off, row by row, first row followed by second row.
Algorithm Encryption:
Algorithm Decryption:
3. k=strlen(CT)/2
4. i=0,j=0;
5. PT[i]=CT[i]
6. PT[i+1]=CT[k]
7. i++,j++,k++
Code:-
#include<stdio.h>
#include<string.h>
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = msg[i];
row = row + k;
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = '*';
k= k * (-1);
row = row + k;
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("%c", railMatrix[row][col++]);
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
int main(){
char msg[100];
int key,k;
gets(msg);
scanf("%d",&k);
if(k==1)
scanf("%d",&key);
encryptMsg(msg, key);
}
else if(k==2)
scanf("%d",&key);
decryptMsg(msg, key);
else
return 0;
Output:-
2. AIM: Implementation of encryption and decryption using S-DES algorithm.
Procedure:
S-DES is a simplified version of DES. S-DES algorithm is used for the academic purpose.
S-DES uses bit wise operation on message letters to encrypt the data so it is more powerful
against the cryptanalysis attacks. This algorithm takes 8-bitof the message as input, also takes
10 bit key and produces 8 bit cipher text. This algorithm has two rounds. It generates 2, 8-bit
keys that are to be used in each round. Following figure shows the functional details of
S-DES.
Code:
#include <stdio.h>
int l[4],r[4],keys[2][8],ct[8];
void sbox(int sip[],int p[],int sbno,int i)
{
int sbox[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int rw,c,sop;
rw = sip[3]+sip[0]*2;
c = sip[2]+sip[1]*2;
sop = sbox[sbno][rw][c];
for(;sop!=0;sop/=2)
p[i--]=sop%2;
}
void cmp_fun(int round)
{
int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
int slip[4],srip[4];
int p[4]={0},p4[]={2,4,3,1},np[4];
for(i=0;i<8;i++)
epd[i]=r[EP[i]-1];
for(i=0;i<8;i++)
if(i<4)
slip[i] = epd[i]^keys[round][i];
else
srip[i-4] = epd[i]^keys[round][i];
sbox(slip,p,0,1);
sbox(srip,p,1,3);
for(i=0;i<4;i++)
np[i]=p[p4[i]-1];
for(i=0;i<4;i++)
l[i] = l[i]^np[i];
}
void left_shift(int keyip[],int nob)
{
int t1,t2,i;
while(nob>0)
{
t1=keyip[0],t2=keyip[5];
for(i=0;i<9;i++)
if(i<4)
keyip[i] =keyip[i+1];
else if(i>4)
keyip[i] = keyip[i+1];
keyip[4]=t1,keyip[9]=t2;
nob--;
}
}
void gen_keys()
{
int key[10],i,keyip[10];
int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9};
printf("Enter Key :");
for(i=0;i<10;i++)
scanf("%d", &key[i]);
for(i=0;i<10;i++)
keyip[i] = key[p10[i]-1];
left_shift(keyip,1);
printf("\nKey1 :");
for(i=0;i<8;i++){
keys[0][i] = keyip[p8[i]-1];
printf("%d",keys[0][i]);
}
left_shift(keyip,2);
printf("\nKey2 :");
for(i=0;i<8;i++){
keys[1][i] = keyip[p8[i]-1];
printf("%d",keys[1][i]);
}
}
void En_De(int pt[],int c)
{
int ip[]={2,6,3,1,4,8,5,7},ipi[]={4,1,3,5,7,2,8,6},t[8],i;
for(i=0;i<8;i++)
if(i<4)
l[i]=pt[ip[i]-1];
else
r[i-4] = pt[ip[i]-1];
cmp_fun(c);
for(i=0;i<4;i++)
r[i]=l[i]+r[i],l[i]=r[i]-l[i],r[i]=r[i]-l[i];
printf("\n\n");
cmp_fun(!c);
for(i=0;i<8;i++)
if(i<4) t[i]=l[i];
else t[i]=r[i-4];
for(i=0;i<8;i++)
ct[i] = t[ipi[i]-1];
}
void main()
{
int pt[8]={0},i;
printf("Enter plain text binary bits:");
for(i=0;i<8;i++)
scanf("%d",&pt[i]);
gen_keys();
En_De(pt,0);
printf("\nCipher Text :");
for(i=0;i<8;i++)
printf("%d",ct[i]);
En_De(ct,1);
printf("\nPlain Text (After Decrypting):");
for(i=0;i<8;i++)
printf("%d",ct[i]);
}
Output:
3.
AIM: Implement RSA asymmetric (public key and private key) EncryptionImplement
Euclidean and Extended Euclidean algorithm for calculating the GCD.
Algorithm:
Key generation:
-1
Step5: Determinedsuchthatd=e modФ(n)
Encryption:
Code:
#include<stdio.h>
int phi,M,n,e,d,C,FLAG;
gcd(int a,int b)
{
int temp = 0;
while(b != 0)
{
temp = a;
a = b;
b = temp % b;
}
return a;
}
int isprime(int n)
{
int i,count=1;
for(i=2;i<n;i++)
{
if(n%i==0)
count=0;
break;
}
return count;
}
int check()
{
int i;
if((e<=1)||(e>=phi))
{
FLAG = 1;
return;
}
if(gcd(e,phi)!=1)
{
FLAG = 1;
return;
}
if(isprime(e)==0)
{
FLAG = 1;
return;
}
FLAG = 0;
return;
}
void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}
void main()
{
int i,p,q,s;
for(i=1;i<phi;i++)
{
if((e*i)%phi==1)
{
d=i;
break;
}
}
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
decrypt();
}
Output:
Experiment-4:-
Aim:-Demonstrate the usage of Wireshark to identify abnormal activity in network Communication
Wireshark is a free application you use to capture and view the data traveling back and forth on your network. It
provides the ability to drill down and read the contents of each packet and is filtered to meet your specific needs.
It is commonly used to troubleshoot network problems and to develop and test software. This open source
protocol analyzer is widely accepted as the industry standard, winning its fair share of awards over the years.
Originally known as Ethereal, Wireshark has a user-friendly interface that can display data from hundreds of
different protocols on all major network types. Data packets can be viewed in real time or analyzed offline.
Wireshark supports dozens of capture/trace file formats supported including CAP and ERF. Integrated decryption
tools allow you to view encrypted packets for several popular protocols including WEP and WPA/WPA2..
Downloading and installing Wireshark:- Wireshark can be downloaded at no cost from the Wire
Shark Foundation Website for both macOS and Windows operating systems. Unless you are an advanced user,
it is recommended that you only download the latest stable release. During the Windows setup process, you
should choose to install WinPcap if prompted, as it includes a library required for live data capture.The
application is also available for Linux and most other UNIX-like platforms Including Red Hat, Solaris, and
FreeBSD. The binaries required for these operating systems can be found toward the bottom of the download
page in the Third-Party Pacakages section. You can also download Wireshark's source code from this page.
Wireshark_Colourised:-
Wireshark_capturing Packets:
Wireshark_tcp:
Wireshark_protocol:
Experiment-5:-
Download the Nmap installer:-This can be found for free from the developer’s website. It is
highly recommended that you download directly from the developer to avoid any potential
viruses or fake files. Downloading the Nmap installer includes Zenmap, the graphical
interface for Nmap which makes it easy for newcomers to perform scans without having to
learn command lines.The Zenmap program is available for Windows, Linux, and Mac OS X.
You can find the installation files for all operating systems on the Nmap website.
Nmap_setup:-If you left your settings at default during installation, you should be able to
see an icon for it on your desktop. If not, look in your Start menu. Opening Zenmap will start
the program.
Nmap_target:-The active results of the scan will be displayed in the Nmap Output tab. The
time the scan takes will depend on the scan profile you chose, the physical distance to the
target, and the targets network configuration.
Nmap_read results:-. Once the scan is finished, you’ll see the message “Nmap done” at the
bottom of the Nmap Output tab. You can now check your results, depending on the type of
scan you performed. All of the results will be listed in the main Nmap Output tab, but you can
use the other tabs to get a better look at specific data.
Nmap_ports/Hosts:Ports/Hosts:- This tab will show the results of your port scan, including
the services for those ports.
Nmap_topology:-This shows the traceroute for the scan you performed. You can see how
many hops your data goes through to reach the target.
Nmap_Host Details:-This shows the traceroute for the scan you performed. You can see
how many hops your data goes through to reach the target.
Nmap_scans:- This tab stores the commands of your previously-run scans. This allows you
to quickly re-scan with a specific set of parameters