0% found this document useful (0 votes)
427 views

Information Security Lab Manual Pages

The document describes the implementation of the Hill cipher encryption technique in C language. It begins with representing each letter as a number from 0-25. To encrypt a message, blocks of letters are multiplied by an invertible matrix modulo 26. Decryption involves multiplying the cipher text blocks by the inverse of the encryption matrix. The algorithm involves reading the plain text and key, splitting the plain text into blocks of three letters, multiplying each block by the key matrix for encryption, and multiplying the cipher text blocks by the inverse key matrix for decryption.
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)
427 views

Information Security Lab Manual Pages

The document describes the implementation of the Hill cipher encryption technique in C language. It begins with representing each letter as a number from 0-25. To encrypt a message, blocks of letters are multiplied by an invertible matrix modulo 26. Decryption involves multiplying the cipher text blocks by the inverse of the encryption matrix. The algorithm involves reading the plain text and key, splitting the plain text into blocks of three letters, multiplying each block by the key matrix for encryption, and multiplying the cipher text blocks by the inverse key matrix for decryption.
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/ 21

CS6711 SECURITY LABORATO

TORY

EX. NO: 1(A)


IMPLE
EMENTATION OF CAESAR CIPHER
R

AIM:

To implement the simple su


substitution technique named Caesar cipher using
ing C language.

DESCRIPTION:

To encrypt a messagee w
with a Caesar cipher, each letter in the messag
sage is changed
using a simple rule: shift by thr
three. Each letter is replaced by the letter three letters
le ahead in
the alphabet. A becomes D, B bbecomes E, and so on. For the last letters, we can
ca think of the
alphabet as a circle and "wrapp around".
a W becomes Z, X becomes A, Y beco
comes B, and Z
becomes C. To change a messag
sage back, each letter is replaced by the one threee bbefore it.

EXAMPLE:

ALGORITHM:

STEP-1: Read the plain te


text from the user.
STEP-2: Read the key valu
alue from the user.
STEP-3: If the key is po
positive then encrypt the text by adding the key
k with each
character in thee pplain text.
STEP-4: Else subtract thee kkey from the plain text.
STEP-5: Display the ciphe
her text obtained above.

PROGRAM: (Caesar Cipher)


r)

#include <stdio.h>
#include <string.h>
#include<conio.h>
#include <ctype.h>
void main()
CS6711 SECURITY LABORATORY

{
char plain[10], cipher[10];
int key,i,length;
int result;
clrscr();
printf("\n Enter the plain text:");
scanf("%s", plain);
printf("\n Enter the key value:");
scanf("%d", &key);
printf("\n \n \t PLAIN TEXt: %s",plain);
printf("\n \n \t ENCRYPTED TEXT: ");
for(i = 0, length = strlen(plain); i < length; i++)
{
cipher[i]=plain[i] + key;
if (isupper(plain[i]) && (cipher[i] > 'Z'))
cipher[i] = cipher[i] - 26;
if (islower(plain[i]) && (cipher[i] > 'z'))
cipher[i] = cipher[i] - 26;
printf("%c", cipher[i]);
}
printf("\n \n \t AFTER DECRYPTION : ");
for(i=0;i<length;i++)
{
plain[i]=cipher[i]-key;
if(isupper(cipher[i])&&(plain[i]<'A'))
plain[i]=plain[i]+26;
if(islower(cipher[i])&&(plain[i]<'a'))
plain[i]=plain[i]+26;
printf("%c",plain[i]);
}
getch();
}

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

OUTPUT:

RESULT:

Thus the implementation of Caesar cipher had been executed successfully.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

EX. NO: 1(B)

IMPLEMENTATION OF PLAYFAIR CIPHER


AIM:

To write a C program to implement the Playfair Substitution technique.

DESCRIPTION:

The Playfair cipher starts with creating a key table. The key table is a 5×5 grid of
letters that will act as the key for encrypting your plaintext. Each of the 25 letters must be
unique and one letter of the alphabet is omitted from the table (as there are 25 spots and 26
letters in the alphabet).

To encrypt a message, one would break the message into digrams (groups of 2 letters)
such that, for example, "HelloWorld" becomes "HE LL OW OR LD", and map them out on
the key table. The two letters of the diagram are considered as the opposite corners of a
rectangle in the key table. Note the relative position of the corners of this rectangle. Then
apply the following 4 rules, in order, to each pair of letters in the plaintext:

1. If both letters are the same (or only one letter is left), add an "X" after the first letter
2. If the letters appear on the same row of your table, replace them with the letters to
their immediate right respectively
3. If the letters appear on the same column of your table, replace them with the letters
immediately below respectively
4. If the letters are not on the same row or column, replace them with the letters on the
same row respectively but at the other pair of corners of the rectangle defined by the
original pair.

EXAMPLE:

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

ALGORITHM:

STEP-1: Read the plain text from the user.


STEP-2: Read the keyword from the user.
STEP-3: Arrange the keyword without duplicates in a 5*5 matrix in the row order and
fill the remaining cells with missed out letters in alphabetical order. Note that
‘i’ and ‘j’ takes the same cell.
STEP-4: Group the plain text in pairs and match the corresponding corner letters by
forming a rectangular grid.
STEP-5: Display the obtained cipher text.

PROGRAM: (Playfair Cipher)


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5
void playfair(char ch1,char ch2, char key[MX][MX])
{
int i,j,w,x,y,z;
FILE *out;
if((out=fopen("cipher.txt","a+"))==NULL)
{
printf("File Currupted.");
}
for(i=0;i<MX;i++)
{
for(j=0;j<MX;j++)
{
if(ch1==key[i][j])
{
w=i;
x=j;
}
else if(ch2==key[i][j])
{
y=i;
z=j;
}}}
//printf("%d%d %d%d",w,x,y,z);
if(w==y)
{
x=(x+1)%5;z=(z+1)%5;
printf("%c%c",key[w][x],key[y][z]);
fprintf(out, "%c%c",key[w][x],key[y][z]);
}
else if(x==z)
{

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

w=(w+1)%5;y=(y+1)%5;
printf("%c%c",key[w][x],key[y][z]);
fprintf(out, "%c%c",key[w][x],key[y][z]);
}
else
{
printf("%c%c",key[w][z],key[y][x]);
fprintf(out, "%c%c",key[w][z],key[y][x]);
}
fclose(out);
}
void main()
{
int i,j,k=0,l,m=0,n;
char key[MX][MX],keyminus[25],keystr[10],str[25]={0};
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'}
;
clrscr();
printf("\nEnter key:");
gets(keystr);
printf("\nEnter the plain text:");
gets(str);
n=strlen(keystr);
//convert the characters to uppertext
for (i=0; i<n; i++)
{
if(keystr[i]=='j')keystr[i]='i';
else if(keystr[i]=='J')keystr[i]='I';
keystr[i] = toupper(keystr[i]);
}
//convert all the characters of plaintext to uppertext
for (i=0; i<strlen(str); i++)
{
if(str[i]=='j')str[i]='i';
else if(str[i]=='J')str[i]='I';
str[i] = toupper(str[i]);
}
j=0;
for(i=0;i<26;i++)
{
for(k=0;k<n;k++)
{
if(keystr[k]==alpa[i])
break;
else if(alpa[i]=='J')
break;
}
if(k==n)
{
keyminus[j]=alpa[i];j++;
}
}

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

//construct key keymatrix


k=0;
for(i=0;i<MX;i++)
{
for(j=0;j<MX;j++)
{
if(k<n)
{
key[i][j]=keystr[k];
k++;}
else
{
key[i][j]=keyminus[m];m++;
}
printf("%c ",key[i][j]);
}
printf("\n");
}
printf("\n\nEntered text :%s\nCipher Text :",str);
for(i=0;i<strlen(str);i++)
{
if(str[i]=='J')str[i]='I';
if(str[i+1]=='\0')
playfair(str[i],'X',key);
else
{
if(str[i+1]=='J')str[i+1]='I';
if(str[i]==str[i+1])
playfair(str[i],'X',key);
else
{
playfair(str[i],str[i+1],key);i++;
}}
}
getch();
}

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

OUTPUT:

RESULT:
Thus the Playfair cipher substitution technique had been implemented successfully.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATO
TORY

EX. NO: 1(C)


IMPLEM
MENTATION OF HILL CIPHER
AIM:

To write a C program to implement the hill cipher substitution technique


ues.

DESCRIPTION:

Each letter is represented


ted by a number modulo 26. Often the simple sch
cheme A = 0, B
= 1... Z = 25, is used, but this is not an essential feature of the cipher. To encry
crypt a message,
each block of n letters is multi
ltiplied by an invertible n × n matrix, against modulus
mo 26. To
decrypt the message, each bl
block is multiplied by the inverse of the ma
matrix used for
encryption. The matrix used ffor encryption is the cipher key, and it shou
ould be chosen
randomly from the set of inverti
rtible n × n matrices (modulo 26).

EXAMPLE:

ALGORITHM:

STEP-1: Read the plain


in text and key from the user.
STEP-2: Split the plain
in text into groups of length three.
STEP-3: Arrange the keyword
ke in a 3*3 matrix.
STEP-4: Multiply the tw
two matrices to obtain the cipher text of length th
three.
STEP-5: Combine all these
th groups to get the complete cipher text.

PROGRAM: (Hill Cipher)

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
unsigned int a[3]][3]={{6,24,1},{13,16,10},{20,17
7,15}};
unsigned int b[3]][3]={{8,5,10},{21,8,21},{21,12,
,8}};
int i,j, t=0;
unsigned int c[200],d[20];
char msg[20];
clrscr();
printf("Enter plaain text\n ");
scanf("%s",msg);
for(i=0;i<strlen((msg);i++)
{ c[i]=msg[i]-665;
CS6711 SECURITY LABORATORY

printf("%d ",c[i]);
}
for(i=0;i<3;i++)
{ t=0;
for(j=0;j<3;j++)
{
t=t+(a[i][j]*c[j]);
}
d[i]=t%26;
}
printf("\nEncrypted Cipher Text :");
for(i=0;i<3;i++)
printf(" %c",d[i]+65);
for(i=0;i<3;i++)
{
t=0;
for(j=0;j<3;j++)
{
t=t+(b[i][j]*d[j]);
}
c[i]=t%26;
}
printf("\nDecrypted Cipher Text :");
for(i=0;i<3;i++)
printf(" %c",c[i]+65);
getch();
return 0;
}

OUTPUT:

RESULT:

Thus the hill cipher substitution technique had been implemented successfully in C.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

EX. NO: 1(E)

IMPLEMENTATION OF RAIL FENCE – ROW & COLUMN

TRANSFORMATION TECHNIQUE

AIM:

To write a C program to implement the rail fence transposition technique.

DESCRIPTION:

In the rail fence cipher, the plain text is written downwards and diagonally on
successive "rails" of an imaginary fence, then moving up when we reach the bottom rail.
When we reach the top rail, the message is written downwards again until the whole plaintext
is written out. The message is then read off in rows.

EXAMPLE:

ALGORITHM:

STEP-1: Read the Plain text.


STEP-2: Arrange the plain text in row columnar matrix format.
STEP-3: Now read the keyword depending on the number of columns of the plain text.
STEP-4: Arrange the characters of the keyword in sorted order and the corresponding
columns of the plain text.
STEP-5: Read the characters row wise or column wise in the former order to get the
cipher text.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

PROGRAM: (Rail Fence)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
clrscr();
printf("\n\t\t RAIL FENCE TECHNIQUE");
printf("\n\nEnter the input string : ");
gets(a);
l=strlen(a);

/*Ciphering*/
for(i=0,j=0;i<l;i++)
{
if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{
if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence :");
printf("\n%s",c);

/*Deciphering*/
if(l%2==0)
k=l/2;
else
k=(l/2)+1;
for(i=0,j=0;i<k;i++)
{
d[j]=c[i];
j=j+2;
}
for(i=k,j=1;i<l;i++)
{
d[j]=c[i];
j=j+2;
}
d[l]='\0';
printf("\nText after decryption : ");
printf("%s",d);
getch();
}

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

OUTPUT:

RESULT:

Thus the rail fence algorithm had been executed successfully.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

EX. NO: 2(B) IMPLEMENTATION OF RSA

AIM:

To write a C program to implement the RSA encryption algorithm.

DESCRIPTION:

RSA is an algorithm used by modern computers to encrypt and decrypt messages. It


is an asymmetric cryptographic algorithm. Asymmetric means that there are two different
keys. This is also called public key cryptography, because one of them can be given to
everyone. A basic principle behind RSA is the observation that it is practical to find three
very large positive integers e, d and n such that with modular exponentiation for all
integer m:

(me)d = m (mod n)

The public key is represented by the integers n and e; and, the private key, by the
integer d. m represents the message. RSA involves a public key and a private key. The public
key can be known by everyone and is used for encrypting messages. The intention is that
messages encrypted with the public key can only be decrypted in a reasonable amount of
time using the private key.

EXAMPLE:

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

ALGORITHM:

STEP-1: Select two co-prime numbers as p and q.


STEP-2: Compute n as the product of p and q.
STEP-3: Compute (p-1)*(q-1) and store it in z.
STEP-4: Select a random prime number e that is less than that of z.
STEP-5: Compute the private key, d as e * mod-1(z).
STEP-6: The cipher text is computed as messagee * mod n.
STEP-7: Decryption is done as cipherdmod n.

PROGRAM: (RSA)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int
p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void main()
{
clrscr();
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0)
{
printf("\nWRONG INPUT\n");
getch();
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
{
printf("\nWRONG INPUT\n");
getch();
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getch();
}
int prime(long int pr)
{
int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
if(pr%i==0)
return 0;
}
return 1;
}
void ce()
{
int k;
k=0;
for(i=2;i<t;i++)
{
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q)
{
e[k]=i;
flag=cd(e[k]);
if(flag>0)
{
d[k]=flag;
k++;
}
if(k==99)
break;
} } }
long int cd(long int x)
{
long int k=1;
while(1)
{
k=k+t;
if(k%x==0)
return(k/x);
} }
void encrypt() {
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

while(i!=len) {
pt=m[i];
pt=pt-96;
k=1;
for(j=0;j<key;j++)
{ k=k*pt;
k=k%n;
}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt()
{
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1)
{
ct=temp[i];
k=1;
for(j=0;j<key;j++)
{
k=k*ct;
k=k%n;
}
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for(i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

OUTPUT:

RESULT:

Thus the C program to implement RSA encryption technique had been implemented
successfully

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

EX. NO: 2(C)

IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE


ALGORITHM
AIM:

To implement the Diffie-Hellman Key Exchange algorithm using C language.

DESCRIPTION:

Diffie–Hellman Key Exchange establishes a shared secret between two parties that
can be used for secret communication for exchanging data over a public network. It is
primarily used as a method of exchanging cryptography keys for use in symmetric encryption
algorithms like AES. The algorithm in itself is very simple. The process begins by having the
two parties, Alice and Bob. Let's assume that Alice wants to establish a shared secret with
Bob.

EXAMPLE:

ALGORITHM:

STEP-1: Both Alice and Bob shares the same public keys g and p.
STEP-2: Alice selects a random public key a.
STEP-3: Alice computes his secret key A as ga mod p.
STEP-4: Then Alice sends A to Bob.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

STEP-5: Similarly Bob also selects a public key b and computes his secret key as B
and sends the same back to Alice.
STEP-6: Now both of them compute their common secret key as the other one’s secret
key power of a mod p.

PROGRAM: (Diffie Hellman Key Exchange)

#include<stdio.h>
#include<conio.h>
long long int power(int a, int b, int mod)
{
long long int t;
if(b==1)
return a;
t=power(a,b/2,mod);
if(b%2==0)
return (t*t)%mod;
else
return (((t*t)%mod)*a)%mod;
}
long int calculateKey(int a, int x, int n)
{
return power(a,x,n);
}
void main()
{
int n,g,x,a,y,b;
clrscr();
printf("Enter the value of n and g : ");
scanf("%d%d",&n,&g);
printf("Enter the value of x for the first person : ");
scanf("%d",&x);
a=power(g,x,n);
printf("Enter the value of y for the second person : ");
scanf("%d",&y);
b=power(g,y,n);
printf("key for the first person is :
%lld\n",power(b,x,n));
printf("key for the second person is :
%lld\n",power(a,y,n));
getch();
}

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS6711 SECURITY LABORATORY

OUTPUT:

RESULT:
Thus the Diffie-Hellman key exchange algorithm had been successfully implemented
using C.

VVIT DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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