You are on page 1of 36

INDIRA GANDHI DELHI TECHNICAL UNIVERSITY FOR WOMEN

APPLIED CRYPTOGRAPHY LAB


CODE (MIW-651)
PRACTICAL FILE

SUBMITTED TO: SUBMITTED BY:


MR. A.K. MOHAPATRA Shubhra Shah
ICT-3RD SEMESTER
(01403152017)

01403152017 Shubhra Shah Applied Cryptography Lab


LIST OF EXPERIMENTS
Course Code: MIW-651
Course Title: Applied Cryptography Lab

Experim
ent No. Title Remarks
1 Write a Program to implement the Caesar cipher.
2 Write a Program to implement the hill cipher.

3
Write a program to implement Diffie-Hellman key exchange algorithm.
4
Write the case study for Random numbers using PRNG.

5 Write a program to implement encryption and decryption using one time


pad cipher (OTP).
6
Write a program to implement RSA algorithm for encryption.
Write a program to implement permutation function of Data Encryption
7
Standard.
8
Verification tool for cryptographic protocol using AVISPA.

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 1

Aim: Write a Program to implement the Caesar cipher.


Theory: The Caesar Cipher technique is one of the earliest and simplest method of
encryption technique. It’s simply a type of substitution cipher, i.e., each letter of a given text
is replaced by a letter some fixed number of positions down the alphabet. For example with
a shift of 1, A would be replaced by B, B would become C, and so on. The method is
apparently named after Julius Caesar, who apparently used it to communicate with his
officials.
Thus to cipher a given text we need an integer value, known as shift which indicates the
number of position each letter of the text has been moved down.
The encryption can be represented using modular arithmetic by first transforming the letters
into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a
shift n can be described mathematically as:.

Examples :

01403152017 Shubhra Shah Applied Cryptography Lab


Program:

// A C++ program to illustrate Caesar Cipher Technique

Experiment 1

#include<stdio.h>
#include<conio.h>
int main()
{
char message[100], ch;
int i, key;
clrscr();
printf("\n****CEASER CYPHER****\nEnter a message to encrypt: ");
gets(message);
printf("\nEnter key: ");
scanf("%d", &key);
//encryption
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
// printf("%c ",ch);
if(islower(ch)){
ch = (ch + key-97)%26+97;
message[i] = ch;
}
else {
ch = (ch + key-65)%26+65;
message[i] = ch;
}
}
printf("\nEncrypted message: %s", message);
//decryption
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
//printf("%c ",ch);

if(islower(ch)){
ch = (ch - key-97)%26+97;
message[i] = ch;
}
else {
ch = (ch - key-65)%26+65;
message[i] = ch;
}
}
printf("\nDecrypted message: %s", message);
getch();
return 0;
}

01403152017 Shubhra Shah Applied Cryptography Lab


Output:

Result: Caesar Cipher is implemented as above.

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 2

Theory: Hill cipher is a polygraphic substitution cipher based on linear algebra.Each letter is
represented by a number modulo 26. Often the simple scheme A = 0, B = 1, …, Z = 25 is
used, but this is not an essential feature of the cipher. To encrypt a message, each block of
n letters (considered as an n-component vector) is multiplied by an invertible n × n matrix,
against modulus 26. To decrypt the message, each block is multiplied by the inverse of the
matrix used for encryption.

The matrix used for encryption is the cipher key, and it should be chosen randomly from the
set of invertible n × n matrices (modulo 26).

Examples:

Aim: Write a Program to implement the hill cipher.

#include<stdio.h>
#include<math.h>
#include<conio.h>

void main()
{
char aa[26] = "abcdefghijklmnopqrstuvwxyz";
char pt[10];
int m=3, d, q = 0, i, j, k[2][2],ki[2][2], p[4], pp[4], t[5], a=26, x,det;
// int k1[2][2], k2[2][2], det;
// clrscr();
printf("*****HILL CYPHER****\nenter the plaintext(3 letters in small) :");
scanf("%s", pt);
printf("\n%s\n",pt);
// m = strlen(pt);

for (i = 0; i < 3; i++) {


for (j = 0; j < 3; j++) {
printf("Enter the #X# matrix for the key(It should in Inversible):");
scanf("%d",&k[i][j]);
}

01403152017 Shubhra Shah Applied Cryptography Lab


//printf("\n the key is :"); //

for (i = 0; i < 3; i++) {


for (j = 0; j < 3; j++) {
printf("%d ",k[i][j]);
}
printf("\n");
}

for (i = 0; i < m; i++) {


for (j = i; j < 26; j++)

{
if (pt[i] == aa[j])

{
t[q] = j % 26;
++q;
}
}
}

// Encryption
i= p[0] = ((k[0][0] * t[0]) + (k[0][1] * t[1]) +(k[0][2] * t[2]))%26;
j= p[1] = ((k[1][0] * t[0]) + (k[1][1] * t[1])+(k[1][2] * t[2]))%26;
q= p[2] = ((k[2][0] * t[0]) + (k[2][1] * t[1])+(k[2][2] * t[2]))%26;
printf("\n Encrypted string is :%c%c%c", aa[i],aa[j],aa[q]);

// finding determinant
for(i = 0; i < 3; i++)
det = det + (k[0][i] * (k[1][(i+1)%3] * k[2][(i+2)%3] - k[1][(i+2)%3] * k[2][(i+1)%3]));
printf("\n\ndeterminant: %f\n", det);

// finding multiplicative inverse of a=26 and determinant=det

if (gcd(a,det)==1)
{
x=imod(a,det);
// printf("The Modular Multiplicative Inverse is: %d\n",imod(a,det));
}
//else
// printf("The numbers are not relatively prime.\n");

//finding inverse of matrix ie inverse key

printf("\nInverse matrix is: \n");


for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
{
ki[i][j]=(((k[(j+1)%3][(i+1)%3] * k[(j+2)%3][(i+2)%3]) - (k[(j+1)%3][(i+2)%3] *
k[(j+2)%3][(i+1)%3]))*x)%26;
printf("%d\t",ki[i][j]);

01403152017 Shubhra Shah Applied Cryptography Lab


}
printf("\n");
}

// DEcryption
pp[0] = ((ki[0][0] * p[0]) + (ki[0][1] * p[1]) +(ki[0][2] * p[2]))%26;
pp[1] = ((ki[1][0] * p[0]) + (ki[1][1] * p[1])+(ki[1][2] * p[2]))%26;
pp[2] = ((ki[2][0] * p[0]) + (ki[2][1] * p[1])+(ki[2][2] * p[2]))%26;
printf("\n cyper text :%s", p);

for (i = 0; i < m; i++) {


printf("n Decrypted string is :%c", aa[pp[i]]);
}
getch();
}
int gcd(int a, int n){
int gcd, i;
for(i=1; i <= a && i <= n; ++i){
if(a%i==0 && n%i==0)
gcd = i;
}
return gcd;
}

int imod(int a,int n){


int c,i=1;
while(1){
c = n * i + 1;
if(c%a==0){
c = c/a;
break;
}
i++;
}
return c;
}

01403152017 Shubhra Shah Applied Cryptography Lab


Result: Hence Hill Cipher is implemented as above

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 3

Aim: Write a program to implement Diffie-Hellman key exchange algorithm.

Theory: The Diffie-Hellman algorithm is being used to establish a shared secret that can be
used for secret
communications while exchanging data over a public network using the elliptic curve to
generate points and get the secret key using the parameters.

For the sake of simplicity and practical implementation of the algorithm, we will consider only
4 variables one prime P and G (a primitive root of P) and two private values a and b.
P and G are both publicly available numbers. Users (say Alice and Bob) pick private values
a and b and they generate a key and exchange it publicly, the opposite person received the
key and from that generates a secret key after which they have the same secret key to
encrypt.

01403152017 Shubhra Shah Applied Cryptography Lab


Example:

01403152017 Shubhra Shah Applied Cryptography Lab


Program:

/* This program calculates the Key for two persons using the Diffie-Hellman Key exchange
algorithm */

#include <stdio.h>
#include <stdlib.h>

// Function to compute a^m mod n

int compute(int a, int m, int n)


{
int r;
int y = 1;
while (m > 0)
{
r = m % 2;
// fast exponention
if (r == 1)
y = (y*a) % n;
a = a*a % n;
m = m / 2;
}
return y;
}

// C program to demonstrate Diffie-Hellman algorithm


int main()
{
int p = 23; // modulus, public parameter
int g = 5; // base, public parameter

int a, b; // a - Alice's Secret Key, b - Bob's Secret Key.


int A, B; // A - Alice's Public Key, B - Bob's Public Key
int keyA,keyB;
printf("*****DIFFIE HELLMAN KEY EXCHANGE****\nThe public parameters are
(23,5)\n");
// choose secret integer for Alice's Pivate Key (only known to Alice)

a=rand();
printf(" \nprivate key for Alice:%d\n",a);
// Calculate Alice's Public Key (Alice will send A to Bob)

A = compute(g, a, p);

// choose secret integer for Bob's Pivate Key (only known to Bob)
b=rand();
printf("\n private key for Bob:%d\n",b);
// Calculate Bob's Public Key (Bob will send B to Alice)

B = compute(g, b, p);

// Alice and Bob Exchanges their Public Key A & B with each other
// Find Secret key

01403152017 Shubhra Shah Applied Cryptography Lab


keyA = compute(B, a, p);

keyB = compute(A, b, p);

printf("Alice's Secret Key is %d\nBob's Secret Key is %d", keyA, keyB);

getch();
return 0;
}

Result: Hence Key is generated for two person using Diffie-Hellman key exchange
algorithms as above program.

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 4

Aim: Write the case study for Random numbers using PRNG.

Random number generation is the generation of a sequence of numbers or symbols that


cannot be reasonably predicted better than by a random chance, usually through a hardware
random-number generator (RNG).There are two principal methods used to generate random
numbers.
● The first method measures some physical phenomenon that is expected to be
random and then compensates for possible biases in the measurement process. Example
sources include measuring atmospheric noise, thermal noise, and other external
electromagnetic and quantum phenomena. For example, cosmic background radiation or
radioactive decay as measured over short timescales represents sources of natural entropy.
● The second method uses computational algorithms that can produce long sequences
of apparently random results, which are in fact completely determined by a shorter initial
value, known as a seed value or key. As a result, the entire seemingly random sequence
can be reproduced if the seed value is known. This type of random number generator is
often called a pseudorandom number generator (PSRN). This type of generator typically
does not rely on sources of naturally occurring entropy, though it may be periodically seeded
by natural sources. This generator type is non-blocking, so they are not rate-limited by an
external event, making large bulk reads a possibility.

1. Properties of Random Numbers

● A sequence of random numbers, must have two important properties:


1. uniformity, i.e. they are equally probable every where
2. independence, i.e. the current value of a random variable has no relation with
the previous values
● C program to generate pseudo-random numbers using rand and random function
(Turbo C compiler only). As the random numbers are generated by an algorithm used
in a function they are pseudo-random, this is the reason that word pseudo is used.
Function rand() returns a pseudo-random number between 0 and RAND_MAX.
RAND_MAX is a constant which is platform dependent and equals the maximum
value returned by rand function

Aim: Write a program to print pseudo random numbers in range [0, 100]. So we calculate
rand() % 100 which will return a number in [0, 99] so we add 1 to get the desired range

01403152017 Shubhra Shah Applied Cryptography Lab


Program:

#include <stdio.h>
#include <stdlib.h>

// Driver program
int main(void)
{
// This program will create same sequence of
// random numbers on every program run
int i;

printf("\n")
for(i = 0; i<5; i++)
printf(" %d ", rand());
return 0;
}

Output

Result: If you rerun this program, you will get the same set of numbers.

To get different numbers every time you can use: srand(unsigned int seed) function; here
seed is an unsigned integer. So you will need a different value of seed every time you run
the program for that you can use current time which will always be different so you will get a
different set of numbers. By default, seed = 1 if you do not use srand function.

Program:

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

int main(void)

{ // This program will create different sequence of random numbers on every program run
Use current time as seed for random generator

01403152017 Shubhra Shah Applied Cryptography Lab


srand(time(0));

int i;

printf("\n");

for(i = 0; i<5; i++)

printf(" %d ", rand());

return 0;

Output

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 5

Aim: Write a program to implement encryption and decryption using one time pad cipher
(OTP).

Theory: Invented in 1917 by Gilbert Vernam, an engineer at AT&T Corporation in the USA.
It is Stream cipher with symmetric secret key
Key length = message length
It has been proven that OTP is impossible to crack if it is used correctly. It has the perfect
secrecy property and allows very fast encryption and decryption. However, the secret key
must be at least as long as the message, what makes it quite inconvenient to use while
sending large electronic information.

Both data encryption and decryption by using OTP takes place in the same way. All bytes of
the message (or of the ciphertext) are added XOR to bytes of the secret key.

The bytes are added one by one, and each addition produces one output byte:

mi XOR ki = ci
ci XOR ki = mi

Using the same key repeatedly


Each part of the secret key can be used only once for encrypting exactly one part of the
message (of course, of the same length). Using the same key bytes more than once, allows
the attacker to discover the two original messages summed by XOR:

M1 XOR K = C1
M2 XOR K = C2
C1 XOR C2 = M1 XOR K XOR M2 XOR K = M1 XOR M2

Having two original messages summed by XOR, the intruder can try to broke the cipher, by
using attacks based on language and encoding features.

Providing no integrity
It is possible to modify the ciphertext in such a way, that the receiver would not be able to
detect that. What is worse, the changes have a predictable impact on the message. If the
attackers know the structure of the message, they are able to change only the desired parts
of the message.
(m -> enc(m, k) -> m XOR k
(m XOR k) XOR p = m XOR k XOR p
m XOR k XOR p -> dec(m XOR p, k) -> m XOR p

where p is the modification added by the attacker.

Secret Sharing
OTP allows to share the secret key among a number of people. Then, the encrypted text can
be decoded only when all those parties use their parts of the key. Each person will know
only one subkey.

To encrypt a text of size n by using a secret key sharing by m people, it is required to


prepare m*n key characters. As a result, each subkey will contain n characters and will allow
to encrypt a text up n-character long.

For example, if a secret key is shared among three parties, it will be required to have all
three subkeys XORed with the ciphertext in order to recover the original message.

01403152017 Shubhra Shah Applied Cryptography Lab


K = K1 XOR K2 XOR K3
C = M XOR K1 XOR K2 XOR K3
M = C XOR K1 XOR K2 XOR K3

Block Diagram:

Mathematical Function:
The only operation during the OTP encryption and decryption is Exclusive Or (XOR). The
key bytes are added XOR to the data bytes, one after another.
Each time, all the 8 bits in the first byte are added XOR to the 8 bits in the second bytes.

Program:

//C Program to Implement the One Time Pad Algorithm.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
//All the text which ever entered is converted to upper and without spaces
int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
char str[100],key[100],cipher[100];

01403152017 Shubhra Shah Applied Cryptography Lab


printf("*****ONE TIME PAD*******\nEnter a string text to encrypt\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
//obtaining numerical plain text ex A-0,B-1,C-2
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
printf("Enter key string of random text\n");
gets(key);
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
//obtaining numerical one time pad(OTP) or key
for(i=0;i<strlen(key);i++)
{
numkey[i]=key[i]-'A';
}

for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]+numkey[i];
}
//To loop the number within 25 i.e if addition of numstr and numkey is 27 then numcipher
should be 1
for(i=0;i<strlen(str);i++)
{
if(numcipher[i]>25)
{
numcipher[i]=numcipher[i]-26;
}
}
printf("One Time Pad Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
printf("%c",(numcipher[i]+'A'));
}
printf("\n");
return 0;
getch();
}

01403152017 Shubhra Shah Applied Cryptography Lab


Output:

Result: Encryption and decryption using one time pad cipher (OTP) is implemented as
above.

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 6

Aim: Write a program to Implement RSA algorithm.


Theory: RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means
that it works on two different keys i.e. Public Key and Private Key. As the name describes
that the Public Key is given to everyone and Private key is kept private.
An example of asymmetric cryptography :

1. A client (for example browser) sends its public key to the server and requests for some
data.
2. The server encrypts the data using client’s public key and sends the encrypted data.
3. Client receives this data and decrypts it.

Since this is asymmetric, nobody else except browser can decrypt the data even if a third
party has public key of browser.
The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer.
The public key consists of two numbers where one number is multiplication of two large
prime numbers. And private key is also derived from the same two prime numbers. So if
somebody can factorize the large number, the private key is compromised. Therefore
encryption strength totally lies on the key size and if we double or triple the key size, the
strength of encryption increases exponentially. RSA keys can be typically 1024 or 2048 bits
long, but experts believe that 1024 bit keys could be broken in the near future. But till now it
seems to be an infeasible task.

Generating Public Key :

Generating Private Key :

01403152017 Shubhra Shah Applied Cryptography Lab


Now we are ready with our – Public Key ( n = 3127 and e = 3) and Private Key(d = 2011)
Now we will encrypt “HI” :

Program:

// C program for RSA asymmetric cryptographic algorithm. For demonstration values are
// relatively small compared to practical application
#include<stdio.h>
#include<math.h>

// Returns gcd of a and b


int gcd(int a, int h)
{
int temp;

01403152017 Shubhra Shah Applied Cryptography Lab


while (1)
{
temp = a%h;
if (temp == 0)
return h;
a = h;
h = temp;
}
}
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result

x = x % p; // Update x if it is more than or


// equal to p

while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;

// y must be even now


y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}

int imod(int a,int n){


int c,i=1;
while(1){
c = n * i + 1;
if(c%a==0){
c = c/a;
break;
}
i++;
}
return c;
}

// Code to demonstrate RSA algorithm


int main()
{

int p = 3;
int q = 11;
int e = 3; //e stands for encrypt
int d; //d stands for decrypt
int n, phi; //public key
int msg = 4,c,m; // Message to be encrypted
n = p*q; //public key
phi = (p-1)*(q-1);

01403152017 Shubhra Shah Applied Cryptography Lab


printf("*******RSA Algorithim Impementation*********\np=3;q=11 prime numbers have
been chosen\n public keys are n= %d \n",n);

printf("computed phi= %d\n",phi);


//other part of public key
while (e < phi)
{
// e must be co-prime to phi and smaller than phi.
if (gcd(e, phi)==1)
{ printf("\n e and phi are co-prime \n");
d=imod(e,phi);// Private key (d stands for decrypt) calculate d such that it satisfies
break; }
else
e++;
}

printf("public key e=%d , pvt key d=%d\n",e,d);

n = p*q; //public key


printf("Message data = %d", msg);

// Encryption c = (msg ^ e) % n
c = power(msg, e,n);

printf("\nEncrypted data = %u\n", c);

// Decryption m = (c ^ d) % n
m = power(c, d,n);
printf("\npower m=%u\n",m);

printf("\nOriginal Message Sent = %d", m);


getch();
return 0;
}

01403152017 Shubhra Shah Applied Cryptography Lab


Output:

Result: RSA algorithm is implemented as above.

Experiment No. 7

Aim: Write a program to implement Intial Permutation and Final(Reverse) Permutation of


Data Encryption Standard.

Theory: The Data Encryption Standard (DES) is a symmetric-key block cipher published by
the National Institute of Standards and Technology (NIST).

DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The block
size is 64-bit. Though, key length is 64-bit, DES has an effective key length of 56 bits, since
8 of the 64 bits of the key are not used by the encryption algorithm (function as check bits
only). General Structure of DES is depicted in the following illustration –

01403152017 Shubhra Shah Applied Cryptography Lab


Since DES is based on the Feistel Cipher, all that is required to specify DES is −

 Round function
 Key schedule
 Any additional processing − Initial and final permutation
Initial and Final Permutation

The initial and final permutations are straight Permutation boxes (P-boxes) that are inverses
of each other. They have no cryptography significance in DES. The initial and final
permutations are shown as follows –

01403152017 Shubhra Shah Applied Cryptography Lab


Round Function
The heart of this cipher is the DES function, f. The DES function applies a 48-bit key to the
rightmost 32 bits to produce a 32-bit output.

 Expansion Permutation Box − Since right input is 32-bit and round key is a 48-bit,
we first need to expand right input to 48 bits. Permutation logic is graphically
depicted in the following illustration –

01403152017 Shubhra Shah Applied Cryptography Lab


 XOR (Whitener). − After the expansion permutation, DES does XOR operation on
the expanded right section and the round key. The round key is used only in this
operation.

 Substitution Boxes. − The S-boxes carry out the real mixing (confusion). DES uses
8 S-boxes, each with a 6-bit input and a 4-bit output. Refer the following illustration –

01403152017 Shubhra Shah Applied Cryptography Lab


 There are a total of eight S-box tables. The output of all eight s-boxes is then
combined in to 32 bit section.

 Straight Permutation − The 32 bit output of S-boxes is then subjected to the


straight permutation with rule shown in the following illustration:

01403152017 Shubhra Shah Applied Cryptography Lab


Program:

#include<stdio.h>
int main()
{
int IPkey[8]={1,6,7,8,5,2,3,4};
int FPkey[8]={1,6,7,8,5,2,3,4};
int i,cnt;
char input[9], output[9],output2[9];
//clrscr();
printf("\n****Implementing DES Intial Permutation and reverse Permutation\nEnter your 8
bits input:");
scanf("%s",input);
input[8]='\0';
printf("Your input is:%s\n", input);
printf("IP key used : ");

for(i=0; i<8; i++){


printf("%d",IPkey[i]);
}
printf("\n");

for(i=0; i<8; i++)


{
cnt=IPkey[i];

output[i]=input[cnt-1];
}
printf("\nAfter Initial Permutation ");
output[8]='\0';

printf(" output is %s", output);


printf("\nIP key used : ");
for(i=0; i<8; i++){
printf("%d",FPkey[i]);
}
printf("\n");

for(i=0; i<8; i++)


{
cnt=FPkey[i];

output2[i]=output[cnt-1];
}
printf("\nAfter Final(reverse) Permutation ");
output2[8]='\0';

printf(" output is %s", output2);


getch();
return 0;
}

01403152017 Shubhra Shah Applied Cryptography Lab


Output:

Result: Hence the algorithm of Data Encryption Standard is implemented as above program.

01403152017 Shubhra Shah Applied Cryptography Lab


Experiment No. 8

Aim: Verification tool for cryptographic protocol using AVISPA.

Program:

01403152017 Shubhra Shah Applied Cryptography Lab


01403152017 Shubhra Shah Applied Cryptography Lab
01403152017 Shubhra Shah Applied Cryptography Lab
01403152017 Shubhra Shah Applied Cryptography Lab
01403152017 Shubhra Shah Applied Cryptography Lab

You might also like