Vous êtes sur la page 1sur 38

Page |1

J.N.T.U.H. COLLEGE OF ENGINEERING


KUKATPALLY, HYDERABAD 500 085

CertifiCate
This is to certify that Kallaypally Karthik of B.Tech IV year I Semester bearing
the Hall-Ticket number 13011A0513 has fulfilled his Network Security and
Cryptography LAB record for the academic year 2016-2017.

Signature of the Head of the Department

Signature of the staff member

Date of Examination_________________________

Internal Examiner

JNTUHCEH

External Examiner

Page |2

S.No.

Name of the Experiment

Write a program to
implement Caesar cipher

Write a program to
implement playfair
cipher

Write a program to
implement hill cipher

Write a program to
implement vigenere
cipher

Write a program to
implement simple DES

Write a program to
implement the RSA
algorithm

Write a program to
implement the Diffie
Hellman Key exchange
algorithm

Write a program to
implement rail fence
cipher

Write a program to
implement double
transposition cipher

10

WireShark program

JNTUHCEH

Date of
Experiment

Page Number

SIGN

Page |3

1) Write a program to implement Caesar cipher


Algorithm:
Encryption algorithm:
Given a plain text P = p1,p2,p3,,pN and a shift length N, the ciphered text
is formed as follows:
Ci = (Pi + N) % 26 where 1 <= i <= N
The cipher text is then given by C = c1,c2,c3,,cN
Decryption algorithm:
Given a cipher text C = c1,c2,c3,,cN we perform the following operation:
Pi = (Ci - N) % 26 where 1 <= i <= N

Program:
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<ctype.h>

char* ceaser(char *text,int k){


char* crypt=malloc(sizeof(int)*100);
int i=0;
while(text[i]!='\0'){
if(islower(text[i])){
int temp=text[i]-'a';
temp=(temp+k+26)%26;
crypt[i]=temp+'a';
}
else if(isupper(text[i])){
int temp=text[i]-'A';
temp=(temp+k+26)%26;
crypt[i]=temp+'A';
}
else if(isspace(text[i])){
crypt[i]='@';
}
else if(text[i]=='@'){
crypt[i]=' ';
}
i++;
}
crypt[i]='\0';
return crypt;
}
int main(){
int k;
printf("Enter k(number of shifts): ");
scanf("%d",&k);

JNTUHCEH

Page |4

getchar();
char *text=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
char *cipher=ceaser(text,k);
printf("Encrypted text: ");
puts(cipher);
char *decipher=ceaser(cipher,(-1)*k);
printf("Decrypted text: ");
puts(decipher);
return 0;
}

Output:

JNTUHCEH

Page |5

2) Write a program to implement Playfair cipher


Algorithm:
Using the key we first generate a key square. Suppose the key is monarchy.
Then the key square is:
m o n a r
c h y b d
e f g i k
l p q s t
u v w x z
Note that there is no 'j', it is combined with 'i'. We now apply the
encryption rules to encrypt the plaintext.
1. Remove any punctuation or characters that are not present in the key
square (this may mean spelling out numbers, punctuation etc.).
2. Identify any double letters in the plaintext and replace the second
occurence with an 'x' e.g. 'hammer' -> 'hamxer'.
3. If the plaintext has an odd number of characters, append an 'x' to the
end to make it even.
4. Break the plaintext into pairs of letters, e.g. 'hamxer' -> 'ha mx er'
5. The algorithm now works on each of the letter pairs.
6. Locate the letters in the key square, (the examples given are using the
key square above)
a. If the letters are in different rows and columns, replace the pair
with the letters on the same row respectively but at the other pair of
corners of the rectangle defined by the original pair. The order is
important the first encrypted letter of the pair is the one that
lies on the same row as the first plaintext letter. 'ha' -> 'bo', 'es'
-> 'il'
b. If the letters appear on the same row of the table, replace them with
the letters to their immediate right respectively (wrapping around to
the left side of the row if a letter in the original pair was on the
right side of the row). 'ma' -> 'or', 'lp' -> 'pq'
c. If the letters appear on the same column of the table, replace them
with the letters immediately below respectively (wrapping around to
the top side of the column if a letter in the original pair was on the
bottom side of the column). 'rk' -> 'dt', 'pv' -> 'vo'
During decryption we perform the same procedure in reverse fashion. The only
difference is we start with the cipher text which is the output of the
encryption stage, finally producing the plain text.

JNTUHCEH

Page |6

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char matrix[5][5];
int pos[26][2];
// int spaces[100];
void key_matrix(char *key){
int i,j;
int key_itr=0;
int flag[26];
for(i=0;i<26;i++){
flag[i]=0;
pos[i][0]=pos[i][1]=-1;
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
int set=0;
while(set==0){
if(key_itr<strlen(key)){
char val=key[key_itr];
if(val==' '){
key_itr++;
//spaces[key_itr]=1;
}
else{
if(flag[val-'a']==0 ){
if(val=='i'|| val=='j'){
matrix[i][j]=val;
flag['i'-'a']=1;
flag['j'-'a']=1;
pos['i'-'a'][0]=pos['j''a'][0]=i;
pos['i'-'a'][1]=pos['j''a'][1]=j;
key_itr++;
}
else{
matrix[i][j]=val;
flag[val-'a']=1;
pos[val-'a'][0]=i;
pos[val-'a'][1]=j;
key_itr++;
}
set=1;
}
else{
key_itr++;
}

JNTUHCEH

Page |7

}
}
else if(key[key_itr]=='\0'){
int k;
for(k=0;k<26;k++){
if(flag[k]==0){
matrix[i][j]='a'+k;
flag[k]=1;
pos[k][0]=i;
pos[k][1]=j;
if(k+'a'=='i'){
int j_pos=k+1;
flag[j_pos]=1;
pos[j_pos][0]=i;
pos[j_pos][i]=j;
}
break;
}
}
set=1;
}
}
}
}
}
char* crypt(char *text,int z){
char *temp;
temp=malloc(sizeof(char)*100);
int i=0,j=0;
while(text[i]!='\0'){
if(text[i]==' '){
i++;
continue;
}
else{
temp[j]=text[i];
i++;
j++;
}
}
temp[j]='\0';
if(strlen(temp)%2==1){
temp[j]='x';
j++;
temp[j]='\0';
}
//printf("%s\n",temp );
char *crypt_text;
crypt_text=(char *)malloc(sizeof(char)*100);

JNTUHCEH

Page |8

for(i=0;temp[i]!='\0';i+=2){
char p1=temp[i];
char p2;
if(temp[i]==temp[i+1])
p2='x';
else
p2=temp[i+1];
int p1_i=pos[p1-'a'][0];
int p1_j=pos[p1-'a'][1];
int p2_i=pos[p2-'a'][0];
int p2_j=pos[p2-'a'][1];
int c1_i,c1_j,c2_i,c2_j;
if(p1_i==p2_i && p1_j!=p2_j){
c1_i=c2_i=p1_i;
c1_j=(p1_j+z)%5;
c2_j=(p2_j+z)%5;
}
else if(p1_i!=p2_i && p1_j==p2_j){
c1_j=c2_j=p1_j;
c1_i=(p1_i+z)%5;
c2_i=(p2_i+z)%5;
}
else if(p1_i!=p2_i && p1_j!=p2_j) {
c1_i=p1_i;
c1_j=p2_j;
c2_i=p2_i;
c2_j=p1_j;
}
crypt_text[i]=matrix[c1_i][c1_j];
crypt_text[i+1]=matrix[c2_i][c2_j];
}
crypt_text[i]='\0';
return crypt_text;
}
void print_matrix(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%c ",matrix[i][j] );
}
printf("\n");
}
printf("-----------------------------------------------------\n\n");
}
int main(int argc, char const *argv[])
{
char *key=malloc(sizeof(char)*100);
char *text=malloc(sizeof(char)*100);
char *cipher,*decipher;

JNTUHCEH

Page |9

printf("Enter key: \n");


gets(key);
key_matrix(key);
print_matrix();
printf("\nEnter plain text: ");
gets(text);
cipher=crypt(text,1);
printf("\nCipher text: ");
puts(cipher);
decipher=crypt(cipher,-1);
printf("\nDecipher text: ");
puts(decipher);
return 0;
}

Output:

JNTUHCEH

P a g e | 10

3) Write a program to implement Hill cipher


Algorithm:
In this cipher, the key is chosen to be a matrix of size 2 X 2. The
determinant of the matrix must not be zero as well as its modulo with respect
to the size of the alphabet (in our case 26) must not be zero. In those cases
the inverse of the matrix doesnt exist and hence we rule out those cases.
In order to encrypt the plain text message P we perform the following
operation:
C = K . P
Where K is a 2 X 2 Matrix key
P is the input message processed in blocks of size 2
C is the output cipher text block of size 2
In order to perform decryption we simply invert the key matrix K by finding
its inverse k-1 and performing the following operation:
P = K-1 . C

Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int key[2][2]={{3,3},{2,5}};
int ikey[2][2]={{15,17},{20,9}};
char* crypt(char *text,char type){
char *temp=(char*)malloc(sizeof(char)*100);
int i=0,k=0;
while(text[k]!='\0'){
if(text[k]==' '){
k++;
}
else{
temp[i]=text[k];
i++;
k++;
}
}
if(i%2==1){
temp[i]='x';
i++;
}
temp[i]='\0';
i=0;
char *cipher=(char *)malloc(sizeof(char)*100);
while(temp[i]!='\0'){

JNTUHCEH

P a g e | 11

int j,k;
if(type=='e'){
for(k=0;k<2;k++){
int sum=0;
for(j=0;j<2;j++)
sum+=key[k][j]*(temp[i+j]-'a');
cipher[i+k]=sum%26+'a';
}
}
else if(type=='d'){
for(k=0;k<2;k++){
int sum=0;
for(j=0;j<2;j++)
sum+=ikey[k][j]*(temp[i+j]-'a');
cipher[i+k]=sum%26+'a';
}
}
i+=2;
}
cipher[i]='\0';
return(cipher);
}
int ext_euclid(int a,int m){
int x=1;//ax=1mod(m) or (ax)mod(m)=1
while(1){
if((a*x)%m==1)
return x;
x++;
}
}
void get_key(){
char k[4];
while(1){
printf("Enter key(4 letters): ");
gets(k);
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
key[i][j]=(k[i*2+j]-'a')%26;
int det=key[0][0]*key[1][1]-key[0][1]*key[1][0];
det%=26;
if(det==0){
printf("Key cannot be used.Enter a new key\n");
continue;
}
else{
int det_inverse=ext_euclid(det,26);
char adj[2][2];
adj[0][0]=key[1][1];

JNTUHCEH

P a g e | 12

adj[0][1]=(26-key[0][1])%26;
adj[1][0]=(26-key[1][0])%26;
adj[1][1]=key[0][0];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
ikey[i][j]=(det_inverse*adj[i][j])%26;
break;
}
}
}
int main(void) {
get_key();
char *text=(char *)malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
char *cipher=crypt(text,'e');
printf("Encrypted text: ");
puts(cipher);
char *decipher=crypt(cipher,'d');
printf("Decrypted text: ");
puts(decipher);
return 0;
}

Output:

JNTUHCEH

P a g e | 13

4) Write a program to implement Vigenere cipher


Algorithm:
Vigenere cipher is a polyalphabetic cipher. In this cipher, encryption is
done by performing different substitutions on the input elements. The order
of substitutions is determined by the corresponding elements of the Key.
By using the following operation we perform encryption:
Ci = (Pi + Ki mod m) mod 26
Where P is the plain text
C is the cipher text
K is the input key
m is the length of the key
The decryption is just the reverse of the encryption and can be done as
follows:
Pi = (Ci - Ki mod m) mod 26

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* encrypt(char *text,char *key){
int keylen=strlen(key);
char *cipher=malloc(sizeof(char)*100);
int i=0,j=0,k=0;//text,key,cipher
while(text[i]!='\0'){
if(text[i]==' '){
i++;
}
else{
cipher[k]=((text[i]-'a')+(key[j]-'a'))%26+'a';
k++;
i++;
j++;
j%=keylen;
}
}
cipher[k]='\0';
return cipher;
}
char* decrypt(char *text,char *key){
int keylen=strlen(key);
char *cipher=malloc(sizeof(char)*100);
int i=0,j=0;//text,key,cipher
while(text[i]!='\0'){
cipher[i]=(text[i]-key[j]+26)%26+'a';

JNTUHCEH

P a g e | 14

i++;
j++;
j%=keylen;
}
cipher[i]='\0';
return cipher;
}
int main(){
char *text,*key;
text=malloc(sizeof(char)*100);
key=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
printf("Enter key: ");
gets(key);
char *cipher=encrypt(text,key);
printf("Encrypted text: ");
puts(cipher);
char *decipher=decrypt(cipher,key);
printf("Decrypted text: ");
puts(decipher);
// return 0;
}

Output:

JNTUHCEH

P a g e | 15

5) Write a program to implement simple DES (Data Encryption Standard)


Algorithm:
Encryption: Takes an 8-bit block of plaintext and a 10-bit key as input and
produces an 8-bit of cipher.
Decryption: Takes an 8-bit block of cipher and the same 10-bit key as input
and produces an 8-bit of original plaintext.
Both substitution and transposition operations are used. It is a complex,
multi-phase algorithm.
The five functions of the simplified DES are:
IP: Initial permutation
fk: Key-dependent scrambler
Use a 8-bit key
Perform both permutation and substitution
SW(simple permutation function)
Swap the two halves of data
fk (different key)
IP-1: Inverse permutation

The following diagram illustrates the steps taken by the simplified DES (Data
Encryption Standard) in detail.

JNTUHCEH

P a g e | 16

Program:
#include <stdio.h>
#include <stdlib.h>
const
const
const
const

int
int
int
int

initial_permutation[8] = {2,6,3,1,4,8,5,7};
inverse_permutation[8] = {4,1,3,5,7,2,8,6};
expansion_permutation[8] = {4,1,2,3,2,3,4,1};
s1[4][4] = {
{1,0,3,2},
{3,2,1,0},
{0,2,1,3},
{3,1,3,2}
};
const int s2[4][4] = {
{0,1,2,3},
{2,0,1,3},
{3,0,1,0},
{2,1,0,3}
};
const int p10[10] = {3,5,2,7,4,10,1,9,8,6};
const int p8[8] = {6,3,7,4,8,5,10,9};
const int p4[4] = {2,4,3,1};
int k1[8], k2[8];

void generate_keys(int *key)


{
int permuted_key[10], ls1[10], ls2[10];
int i;
// Generat permuted key
for(i = 0;i < 10;++i)
permuted_key[i] = key[p10[i] - 1];
// Left shift
for(i = 0;i <
ls1[i] =
for(i = 5;i <
ls1[i] =

the permuted key by 1 position (do each half separately)


5;++i)
permuted_key[(i + 1) % 5];
10;++i)
permuted_key[5 + (i - 4) % 5];

// Using p8 generate the key K1


for(i = 0;i < 8;++i)
k1[i] = ls1[p8[i] - 1];
// Left shift
for(i = 0;i <
ls2[i] =
for(i = 5;i <

JNTUHCEH

ls1 array by 2 positions (do each half separately)


5;++i)
ls1[(i + 2) % 5];
10;++i)

P a g e | 17

ls2[i] = ls1[5 + (i - 3) % 5];


// Using p8 generate the key K2
for(i = 0;i < 8;++i)
k2[i] = ls2[p8[i] - 1];
}
int* perform_encryption(int *plain_text,int *k)
{
int l0[4], r0[4];
int i;
// split plain_text into l0 and r0
for(i = 0;i < 4;++i)
l0[i] = plain_text[i];
for(i = 0;i < 4;++i)
r0[i] = plain_text[4 + i];
// Expand r0 into 8 bits
int expanded_r0[8];
for(i = 0;i < 8;++i)
expanded_r0[i] = r0[expansion_permutation[i] - 1];
// EP xor K
int ep_xor_k[8];
for(i = 0;i < 8;++i)
ep_xor_k[i] = expanded_r0[i] ^ k[i];
// Map values from the s boxes
int s[4], row, column;
row = 2 * ep_xor_k[0] + 1 * ep_xor_k[3];
column = 2 * ep_xor_k[1] + 1 * ep_xor_k[2];
int first = s1[row][column];
row = 2 * ep_xor_k[4] + 1 * ep_xor_k[7];
column = 2 * ep_xor_k[5] + 1 * ep_xor_k[6];
int second = s2[row][column];
switch(first)
{
case 0: s[0]
case 1: s[0]
case 2: s[0]
case 3: s[0]
}
switch(second)
{
case 0: s[2]
case 1: s[2]
case 2: s[2]

JNTUHCEH

=
=
=
=

0;
0;
1;
1;

s[1]
s[1]
s[1]
s[1]

=
=
=
=

0;
1;
0;
1;

break;
break;
break;
break;

= 0; s[3] = 0; break;
= 0; s[3] = 1; break;
= 1; s[3] = 0; break;

P a g e | 18

case 3: s[2] = 1; s[3] = 1; break;


}
// Permute 's' using p4 and xor with l0
int f[4];
for(i = 0;i < 4;++i)
f[i] = l0[i] ^ s[p4[i] - 1];
int *cipher_text = malloc(sizeof(int) * 8);
for(i = 0;i < 4;++i)
cipher_text[i] = r0[i];
for(i = 4;i < 8;++i)
cipher_text[i] = f[i - 4];
return cipher_text;
}
int* perform_decryption(int *cipher_text,int *k)
{
// Split cipher text into r0 and f
int i;
int r0[4], f[4];
for(i = 0;i < 4;++i)
r0[i] = cipher_text[i];
for(i = 0;i < 4;++i)
f[i] = cipher_text[4 + i];
// Generate expanded r0
int expanded_r0[8];
for(i = 0;i < 4;++i)
expanded_r0[i] = r0[expansion_permutation[i] - 1];
for(i = 4;i < 8;++i)
expanded_r0[i] = r0[expansion_permutation[i] - 1];
// XOR expanded r0 with key K
int ep_xor_k[8];
for(i = 0;i < 8;++i)
ep_xor_k[i] = expanded_r0[i] ^ k[i];
// Map values from the s boxes
int s[4], row, column;
row = 2 * ep_xor_k[0] + 1 * ep_xor_k[3];
column = 2 * ep_xor_k[1] + 1 * ep_xor_k[2];
int first = s1[row][column];
row = 2 * ep_xor_k[4] + 1 * ep_xor_k[7];
column = 2 * ep_xor_k[5] + 1 * ep_xor_k[6];
int second = s2[row][column];
switch(first)
{

JNTUHCEH

P a g e | 19

case
case
case
case

0:
1:
2:
3:

s[0]
s[0]
s[0]
s[0]

}
switch(second)
{
case 0: s[2]
case 1: s[2]
case 2: s[2]
case 3: s[2]
}

=
=
=
=

0;
0;
1;
1;

s[1]
s[1]
s[1]
s[1]

=
=
=
=

0;
1;
0;
1;

break;
break;
break;
break;

=
=
=
=

0;
0;
1;
1;

s[3]
s[3]
s[3]
s[3]

=
=
=
=

0;
1;
0;
1;

break;
break;
break;
break;

// Permute 's' with p4


int permuted_s[4];
for(i = 0;i < 4;++i)
permuted_s[i] = s[p4[i] - 1];
// XOR the permuted 's' with 'f' inorder to get l0
int l0[4];
for(i = 0;i < 4;++i)
l0[i] = permuted_s[i] ^ f[i];
// Combine the l0 and r0
int *decipher_text = malloc(sizeof(int) * 8);
for(i = 0;i < 4;++i)
decipher_text[i] = l0[i];
for(i = 0;i < 4;++i)
decipher_text[i + 4] = r0[i];
return decipher_text;
}
int* encrypt(int *plain_text)
{
int permuted_text[8], i;
for(i = 0;i < 8;++i)
permuted_text[i] = plain_text[initial_permutation[i] - 1];
int *first = perform_encryption(permuted_text,k1);
int *second = perform_encryption(first,k2);
free(first);
int *cipher_text = malloc(sizeof(int) * 8);
for(i = 0;i < 8;++i)
cipher_text[i] = second[inverse_permutation[i] - 1];
free(second);
return cipher_text;
}

JNTUHCEH

P a g e | 20

int* decrypt(int *cipher_text)


{
int permuted_text[8], i;
for(i = 0;i < 8;++i)
permuted_text[i] = cipher_text[initial_permutation[i] - 1];
int *first = perform_decryption(permuted_text,k2);
int *second = perform_decryption(first,k1);
free(first);
int *decipher_text = malloc(sizeof(int) * 8);
for(i = 0;i < 8;++i)
decipher_text[i] = second[inverse_permutation[i] - 1];
free(second);
return decipher_text;
}
int main()
{
int plain_text[8], key[10];
int i;
printf("Enter 8 bit plain text: ");
for(i = 0;i < 8;++i)
scanf("%d", &plain_text[i]);
printf("Enter 10 bit key: ");
for(i = 0;i < 10;++i)
scanf("%d", &key[i]);
generate_keys(key);
int *cipher_text = encrypt(plain_text);
printf("Ciphered text: ");
for(i = 0;i < 8;++i)
printf("%d", cipher_text[i]);
int *decipher_text = decrypt(cipher_text);
printf("\nDeciphered text: ");
for(i = 0;i < 8;++i)
printf("%d", decipher_text[i]);
return 0;
}

JNTUHCEH

P a g e | 21

Output:

JNTUHCEH

P a g e | 22

6) Write a program to implement the RSA algorithm


Algorithm:
RSA involves a public key and private key. The public key can be known to
everyone, it is used to encrypt messages. Messages encrypted using the public
key can only be decrypted with the private key. The keys for the RSA
algorithm are generated the following way:
1. Choose two different large random prime numbers p and q
2. Calculate n = p * q
n is the modulus for the public key and the private keys
3. Calculate the totient: phi(n) = (p - 1)(q - 1)
4. Choose an integer e such that 1 < e < phi(n),
and e is coprime to phi(n)
e is released as the public key exponent
5. Compute d to satisfy the congruence relation de = 1 (mod phi(n)) ie: de
= 1 + k * phi(n) for some integer k.
d is kept as the private key exponent
Encrypting messages:
First turn the message M into a number m smaller than n by using an agreedupon reversible protocol known as a padding scheme. He then computes the
ciphertext c corresponding to:
C = me mod n
Decrypting messages:
Similarly decryption can be done by applying the following operation:
M = cd mod n

Program:
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<time.h>

int expWithMod(int a,int b,int n){


//(a^b)%n
a=a%n;
int p=1;
for(int i = 1; i <= b; i++) {
p *= a;
p = p%n;
}
//printf("%d\n",p );
return p;
}
JNTUHCEH

P a g e | 23

int gcd(int a, int b){


while(a!=b){
if(a>b){
a=a%b;
if(a==0){
return b;
}
}
else{
b=b%a;
if(b==0){
return a;
}
}
}
return a;
}
int coprime(int t){
for(int i=2;i<t;i++){
if(gcd(i,t)==1)
return i;
}
}
int ext_euclid(int e,int t){
int d=1;
while(1){
if((e*d)%t==1)
return d;
d++;
}
}
int *encrypt(char *text,int e,int n){
int *cipher=malloc(sizeof(int)*100);
int i=0;
while(text[i]!='\0'){
cipher[i]=expWithMod(text[i],e,n);
i++;
}
cipher[i]='\0';
return cipher;
}
char *decrypt(int *text,int d,int n){
char *decipher=malloc(sizeof(char)*100);
int i=0;
while(text[i]!='\0'){
decipher[i]=expWithMod(text[i],d,n);
i++;
}
decipher[i]='\0';

JNTUHCEH

P a g e | 24

return decipher;
}
int main(){
int p=61,q=53;
int n=p*q;
int t=(p-1)*(q-1);
//printf("p: %d,q: %d,n:%d,t:%d",p,q,n,t);
int e=coprime(t);
int d=ext_euclid(e,t);
//printf(",e: %d,d: %d",e,d);
char *text=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
int *cipher=encrypt(text,e,n);
printf("Encrypt text: ");
for(int i=0;i<strlen(text);i++){
printf("%d ",cipher[i] );
}
char *decipher=decrypt(cipher,d,n);
printf("\nDecrypted text: ");
puts(decipher);
return 0;
}

Output:

JNTUHCEH

P a g e | 25

7) Write a program to implement Diffie Hellman Key exchange algorithm


Algorithm:
Input a prime number n.
Find the primitive root of the prime number n. Call it g.
Now, choose a random number XA at user A such that XA < n. Similarly, choose
a random number XB at user B such that XB < n.
At user A calculate YA as follows:
YA = gXA mod n
And send YA to user B
At user B calculate YB as follows:
YB = gXB mod n
And send YB to user A
Now calculate the shared key values at user A and user B respectively as
follows:
KSa = YB ^ XA mod n
KSb = YA ^ XB mod n

Program:
#include
#include
#include
#include

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

int gcd(int a, int b){


while(a!=b){
if(a>b){
a=a%b;
if(a==0){
return b;
}
}
else{
b=b%a;
if(b==0){
return a;
}
}
}
return a;
}
bool unique(int arr[],int len){

JNTUHCEH

P a g e | 26

for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
if(j!=i){
if(arr[i]==arr[j])
return false;
}
}
}
return true;
}
int expWithMod(int a,int b,int n){
//(a^b)%n
a=a%n;
int p=1;
for(int i = 1; i <= b; i++) {
p *= a;
p = p%n;
}
//printf("%d\n",p );
return p;
}
int primitive_root(int p){
for(int i=2;i<p;i++){
if(gcd(i,p)==1){
int a[p-1];
for(int j=0;j<p-1;j++)
a[j]=expWithMod(i,j,p);
if(unique(a,p-1))
return i;
}
}
return -1;
}
int main(){
int p=11;
printf("Enter prime number(p) known to both parties: \n");
scanf("%d",&p);
getchar();
int g=primitive_root(p);
printf("p:%d g:%d\n",p,g );
int xA=6;
int yA=expWithMod(g,xA,p);
int xB=16;
int yB=expWithMod(g,xB,p);
int zA=expWithMod(yB,xA,p);
int zB=expWithMod(yA,xB,p);
printf("private_a: %2d private_b: %2d\n",xA,xB );
printf("public_a: %2d public_b: %2d\n",yA,yB );

JNTUHCEH

P a g e | 27

printf("secret_a:
return 0;
}

Output:

JNTUHCEH

%2d secret_b:

%2d \n",zA,zB );

P a g e | 28

8) Write a program to implement Rail fence cipher


Algorithm:
First we take the input text P and the number of rails for the algorithm r.
Now, in order to encrypt we write the letters in the plain text P in a zigzag manner along the r rails (from top to bottom and then to top again).
And read them out horizontally.
For decryption we follow the exact opposite procedure. We write down the
cipher text in the horizontal rows. And then read them out in reverse zig-zag
manner along the rail fence.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
char* remove_spaces(char *text){
char *temp=malloc(sizeof(char)*100);
int i=0,j=0;
while(text[i]!='\0'){
if(text[i]!=' '){
temp[j]=text[i];
j++;
}
i++;
}
temp[j]='\0';
return temp;
}
char* encrypt(char *text,int rails){
char *temp=remove_spaces(text);
int itr=0;//cipher itr
char *cipher=(char*)malloc(sizeof(char)*100);
int i,count=0;// count=number of blocks
for(i=0;i<rails;i++){
int block_itr=0;
if(i==0){
while(block_itr<strlen(temp)){
char c=temp[block_itr+i];
cipher[itr]=c;
itr++;
block_itr+=(rails-1)*2;
count++;
}
}
else if(i==(rails-1)){

JNTUHCEH

P a g e | 29

int j;
for(j=0;j<count;j++){
char c=temp[block_itr+i];
if(block_itr+i>=strlen(temp)){
c='@';
}
cipher[itr]=c;
itr++;
block_itr+=(rails-1)*2;
}
}
else{
int j;
for(j=0;j<count;j++){
int jump=(rails-1)*2-(i*2);
int c1=block_itr+i;
int c2=block_itr+i+(jump);
if(c1>=strlen(temp)){
cipher[itr]='@';
cipher[itr+1]='@';
}
else if(c2>=strlen(temp)){
cipher[itr]=temp[c1];
cipher[itr+1]='@';
}
else{
cipher[itr]=temp[c1];
cipher[itr+1]=temp[c2];
}
itr+=2;
block_itr+=(rails-1)*2;
//printf("%c %c ",c1,c2 );
}
}
}
cipher[itr]='\0';
return cipher;
}
char* decrypt(char *text,int rails){
char *cipher=(char*)malloc(sizeof(text));
int text_itr=0,cipher_itr=0;
int i,blank_count=0;
int blocks=strlen(text)/(2*(rails-1));
for(i=0;i<rails;i++){
if(i==0){
cipher_itr=i;
int j;
for(j=0;j<blocks;j++){
char c=text[text_itr];

JNTUHCEH

P a g e | 30

cipher[cipher_itr]=c;
//printf("%c",cipher[cipher_itr]);
cipher_itr+=(rails-1)*2;
text_itr++;
}
}
else if(i==(rails-1)){
cipher_itr=i;
int j=0;
for(j=0;j<blocks;j++){
char c=text[text_itr];
if(c=='@'){
text_itr++;
blank_count++;
}
else{
cipher[cipher_itr]=c;
//printf("%c",cipher[cipher_itr]);
if(j!=rails-1)
cipher_itr+=(rails-1)*2;
text_itr++;
}
}
}
else{
cipher_itr=i;
int j=0;
for(j=0;j<blocks;j++){
char c1=text[text_itr];
char c2=text[text_itr+1];
if(c1=='@'){
text_itr+=2;
blank_count++;
}
else if(c2=='@'){
cipher[cipher_itr]=c1;
//printf("%c",cipher[cipher_itr]);
text_itr+=2;
blank_count++;
}
else{
cipher[cipher_itr]=c1;
//printf("%c",cipher[cipher_itr]);
int jump=(rails-1)*2-i*2;
cipher[cipher_itr+jump]=c2;
if(j!=rails-1)
cipher_itr+=(rails-1)*2;
//printf("%c",cipher[cipher_itr]);
text_itr+=2;

JNTUHCEH

P a g e | 31

}
}
}
}
cipher[cipher_itr]='\0';
return(cipher);
}
void print_array(char *text){
int count=0;
for(int i=0;i<strlen(text);i++){
if(count==5){
printf(" ");
count=0;
}
if(islower(text[i])){
printf("%c",text[i]);
count++;
}
}
printf("\n");
}
int main(){
int rails=0;
printf("Enter rail-count: ");
scanf("%d",&rails);
getchar();
char *text=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
char *cipher=encrypt(text,rails);
print_array(cipher);
char *decipher=decrypt(cipher,rails);
print_array(decipher);
return 0;
}
Output:

JNTUHCEH

P a g e | 32

9) Write a program to implement Double transposition cipher


Algorithm:
In Double transposition cipher, we perform the agreed upon operation twice
thereby removing the statistical frequencies of the plain text elements from
the cipher text elements.
We fill in the columns of the matrix by using the plain text elements. Using
the input key K1 we extract the columns in ascending order. We perform this
operation twice. First with key K1 and next with key K2.
Decryption is done in the reverse order .i.e. first with K2 and later with
K1.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* remove_spaces(char *text){
char *temp=malloc(sizeof(char)*100);
int i=0,j=0;
while(text[i]!='\0'){
if(text[i]!=' '){
temp[j]=text[i];
j++;
}
i++;
}
temp[j]='\0';
return temp;
}
int* perm(char *text){
int len=strlen(text);
int i,j;
int *order=malloc(sizeof(int)*len);
for(i=0;i<len;i++)
order[i]=i;
for(i=0;i<len-1;i++){
for(j=i+1;j<len;j++){
if(text[i]>text[j]){
char temp=text[i];
text[i]=text[j];
text[j]=temp;
int tempInt=order[i];
order[i]=order[j];
order[j]=tempInt;
}
}

JNTUHCEH

P a g e | 33

}
return order;
}
char *trans(char *text,char *key,char filler){
int text_len=strlen(text);
int key_len=strlen(key);
int no_of_rows;
if(text_len%key_len==0)
no_of_rows=text_len/key_len;
else
no_of_rows=text_len/key_len+1;
char matrix[no_of_rows][key_len];
int i,j;
int itr=0;
//filling matrix
for(i=0;i<no_of_rows;i++){
for(j=0;j<key_len;j++){
if(itr<text_len){
matrix[i][j]=text[itr];
}
else{
matrix[i][j]=filler;
}
itr++;
}
}
char *cipher=malloc(sizeof(char)*100);
int *order=perm(key);
itr=0;
for(j=0;j<key_len;j++){
for(i=0;i<no_of_rows;i++){
cipher[itr]=matrix[i][order[j]];
itr++;
}
}
cipher[itr]='\0';
return cipher;
}
char *untrans(char *text,char* key,char filler){
int text_len=strlen(text);
int key_len=strlen(key);
int no_of_rows;
if(text_len%key_len==0)
no_of_rows=text_len/key_len;
else
no_of_rows=text_len/key_len+1;
char matrix[no_of_rows][key_len];
int i,j;
int itr=0;

JNTUHCEH

P a g e | 34

//filling matrix
int *order=perm(key);
for(j=0;j<key_len;j++){
for(i=0;i<no_of_rows;i++){
matrix[i][order[j]]=text[itr];
itr++;
}
}
char *cipher=malloc(sizeof(char)*100);
itr=0;
for(i=0;i<no_of_rows;i++){
for(j=0;j<key_len;j++){
if(matrix[i][j]!=filler){
cipher[itr]=matrix[i][j];
itr++;
}
}
}
cipher[itr]='\0';
return cipher;
}
char* encrypt(char *text,char* key1,char *key2){
char* cipher=trans(remove_spaces(text),remove_spaces(key1),'X');
cipher=trans(cipher,remove_spaces(key2),'Y');
return cipher;
}
char* decrypt(char *text,char *key1,char *key2){
char* cipher=untrans(text,remove_spaces(key2),'Y');
cipher=untrans(cipher,remove_spaces(key1),'X');
return cipher;
}
int main(){
char *key1=malloc(sizeof(char)*100);
char *key2=malloc(sizeof(char)*100);
char *text=malloc(sizeof(char)*100);
printf("Enter key1: ");
gets(key1);
printf("Enter key2: ");
gets(key2);
printf("Enter plain text: ");
gets(text);
char* cipher=encrypt(text,key1,key2);
printf("Encrypted text: ");
puts(cipher);
char* decipher=decrypt(cipher,key1,key2);
printf("Decrypted text: ");
puts(decipher);
}

JNTUHCEH

P a g e | 35

Output:

JNTUHCEH

P a g e | 36

10) Wireshark program


WireShark is a
Packet sniffer/protocol analyzer
Open Source Network Tool
Latest version of the ethereal tool

WireShark with Traffic

JNTUHCEH

P a g e | 37

HEX Window

Follow TCP Stream


Red coloured text: Message packets we send
Blue coloured text: Message packets we receive

JNTUHCEH

P a g e | 38

Placement of WireShark

JNTUHCEH

Vous aimerez peut-être aussi