Top Banner
G.L. Bajaj Institute of Technology and Management, Greater Noida Cryptography and Network Security PRACTICAL FILE TIT-751 SUBMITTED TO SUBMITTED BY 1
28

Cryptography Practical File

Oct 24, 2014

Download

Documents

Ankush
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cryptography Practical File

G.L. Bajaj Institute of Technology and Management, Greater Noida

Cryptography and Network SecurityPRACTICAL FILE

TIT-751

SUBMITTED TO SUBMITTED BY Ms. Puja Bharti Name: R.No.- B.TECH (IT 4th YEAR)

1

Page 2: Cryptography Practical File

INDEX

Sl.No. Name of Practicals PageNo. DateRemarks & Signature

1.Caesar Cipher implementation 3-5

2.Eucledian Algorithm 6-7

3.

Euclidean Algorithm for 10 numbers stored in array

8-9

4.Implementation of RSA algorithm 10-12

5.Implementation of LCG algorithm 13-18

6.Implementation of miller – rabin algorithm

19-22

7.

2

Page 3: Cryptography Practical File

PROGRAM 1

Caesar Cipher Algorithm:

Earliest known use of substitution cipher was by Julius Caesar.1. assign a numerical equivalent to each letter

a = 0b = 1c = 2......y = 24z = 25.

2. For each plain text letter p, substitute the cipher text letter CAs C = E(3,p) = ( p+3) mod 26.

3. general Caesar algorithm is C = E(k,p) = (p+k) mod26

P = D(k,C) = (C- k)mod26

3

Page 4: Cryptography Practical File

Implementation of Caesar cipher :

/*caesar cipher */#include<stdio.h>#include<conio.h>

void main(){

int i, a, b;char array [5];clrscr();printf("Enter the characters to be encrypted \n");

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

scanf("%c", &array[i]);}printf("Enter the value to be added");scanf("%b", &a);

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

if(array[i] >=96||array[i]<=122){

array[i] = array[i] +a;}else if(array[i] >122){

b = array[i]%122;array[i] = (b-1) + 97;}

printf("%c", array[i]);}

getch();}

4

Page 5: Cryptography Practical File

/*******************OUTPUT******************/

Enter the character to be encoded abcdef Enter the value to be added3 defghi

5

Page 6: Cryptography Practical File

PROGRAM #2

Eucledian algorithm

EUCLID(x,y)1. X x; Y y;2. if Y=0 return X = gcd(x,y)3. B = X mod Y4. X Y5. Y B6. goto step 2.

Implementation of Eucledian algorithm in C

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

int gcd(int,int);

void main(){

int a,b,GCD;clrscr();printf("Enter the two numbers whose GCD must be calculated\n");scanf("%d \n%d", &a,&b);

if(a==0)GCD = b;else if(b==0)GCD = a;elseGCD = gcd(a,b);

6

Page 7: Cryptography Practical File

printf("The GCD of the given numbers is \n%d", GCD);getch();

}

int gcd(int x, int y) {

int m, n;if(y==0)m=x;else{n = x%y;m = gcd(y,n);

} return(m); }

/********************OUTPUT*******************/

Enter the number whose GCD is to be calculated5535The GCD of the given numbers is 5

7

Page 8: Cryptography Practical File

PROGRAM #3

Finding GCD of 10 numbers stored in array using Eucledian algorithm.

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

int gcd(int x,int y);

void main(){int x,y,i,j,sml;int a[10],b[9];clrscr();printf("enter ten numbers");for(i=0;i<10;i++)

{scanf("%d",&a[i]);}

for(j=0;j<9;j++){

b[j]=gcd(a[j],a[j+1]);}sml=b[0];for(i=0;i<9;i++){if(sml>a[i]){sml=b[i];}}printf("the gcd of 10 entered numbers is %d",sml);getch();

8

Page 9: Cryptography Practical File

}int gcd(int x,int y){if(x==0)return y;if(y==0)return x;return(gcd((y%x),x));

}

9

Page 10: Cryptography Practical File

PROGRAM #4

RSA algorithm.

1. Select two prime number a & b.2. Calculate m=a*b.3. Calculate Ф(m)=(a-1)*(b-1).4. Select y such that y relatively prime to Ф(m) and y<Ф(m).5. Determining x such that x*y ≡ 1(mod Ф(m)).6. cipher text C = My mod m , {M = message}7. Plaintext M = Cx mod m.

/* C program for the Implementation Of RSA Algorithm */

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

int phi,M,n,e,d,C,FLAG;

int check(){int i;for(i=3;e%i==0 && phi%i==0;i+2){FLAG = 1;return;}FLAG = 0;}

void encrypt(){int i;C = 1;

10

Page 11: Cryptography Practical File

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 p,q,s;clrscr();printf("Enter Two Relatively Prime Numbers\t: ");scanf("%d%d",&p,&q);n = p*q;phi=(p-1)*(q-1);printf("\n\tF(n)\t= %d",phi);do{printf("\n\nEnter e\t: ");scanf("%d",&e);check();}while(FLAG==1);d = 1;do{s = (d*e)%phi;d++;

11

Page 12: Cryptography Practical File

}while(s!=1);d = d-1;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();printf("\n\nEnter the Cipher text\t: ");scanf("%d",&C);decrypt();getch();}

/*************** OUTPUT *****************/

Enter Two Relatively Prime Numbers : 7 17

F(n) = 96

Enter e : 5

Public Key : {5,119}Private Key : {77,119}

Enter The Plain Text : 19

Encrypted keyword : 66

Enter the Cipher text : 66

Decrypted keyword : 19

12

Page 13: Cryptography Practical File

PROGRAM #5

LCG Algorithm:

A linear congruential generator (LCG) represents one of the oldest and best-known pseudorandom number generator algorithms. The theory behind them is easy to understand, and they are easily implemented and fast.

The generator is defined by the recurrence relation:

where Xn is the sequence of pseudorandom values, and

 — the "modulus" — the "multiplier"

 — the "increment" (the special case of c = 0 corresponds to Park–Miller RNG)

 — the "seed" or "start value"

are integer constants that specify the generator.

The period of a general LCG is at most m, and for some choices of a much less than that. The LCG will have a full period if and only if:

1.   and   are relatively prime,2.   is divisible by all prime factors of  ,3.   is a multiple of 4 if   is a multiple of 4.[2]

13

Page 14: Cryptography Practical File

Implementation of Linear congruential generator.

/**********************************************/* A Basic Linear Congruential Generator/* ---------------------------------------/**********************************************/#include<iostream.h>#include<time.h>#include<conio.h>

//This class will give satisfy basic// random number considerations for// games an general apps.class BasicLCG {

private:unsigned long iCurrent;

public:BasicLCG();BasicLCG(unsigned long);void seed(unsigned long iSeed);unsigned long nextNumber(); //get the next random numberunsigned short int nextInt();unsigned char nextChar();int nextBit();double nextDouble();int inRange(int min, int max);

};

//Just a little test code to print some numbersint main(){ BasicLCG rng(time(NULL)); int i; clrscr();

14

Page 15: Cryptography Practical File

//Lets see some bits... for( i=1; i<81; i++) {

cout << rng.nextBit() <<"\t"; } cout <<"\n"; for( i=1; i<41; i++) {

cout << (int)rng.nextChar() <<"\t"; } cout <<"\n"; for( i=1; i<41; i++) {

cout << rng.nextInt() <<"\t"; } cout <<"\n"; for( i=1; i<41; i++) {

cout << rng.nextNumber() <<"\t"; } cout <<"\n\n"; for( i=1; i<41; i++) {

cout << rng.nextDouble() <<"\t"; } cout <<"\n\n"; for( i=1; i<41; i++) {

cout << rng.inRange(10, 5) <<"\t"; }

return 0;}

BasicLCG::BasicLCG(){ iCurrent = 0; //Chose a default seed}

BasicLCG::BasicLCG(unsigned long iSeed){ iCurrent = iSeed;

15

Page 16: Cryptography Practical File

}

void BasicLCG::seed(unsigned long iSeed){ iCurrent = iSeed;}

unsigned long BasicLCG::nextNumber(){ unsigned long iOutput; unsigned long iTemp; int i; //take the top two bits //This will shorten our period to (2^32)/16=268,435,456 //Which seems like plenty for(i=0; i<16; i++) {

//Since this is mod 2^32 and our data type is 32 bits long// there is no need for the MOD operator.iCurrent = (3039177861 * iCurrent + 1);iTemp = iCurrent >> 30;iOutput = iOutput << 2;iOutput = iOutput + iTemp;

} return iOutput;}

unsigned short int BasicLCG::nextInt(){ unsigned short int iOutput; unsigned long iTemp; int i; //No need to limit ourselves... for(i=0; i<8; i++) {

16

Page 17: Cryptography Practical File

//Since this is mod 2^32 and our data type is 32 bits long// there is no need for the MOD operator.iCurrent = (3039177861 * iCurrent + 1);iTemp = iCurrent >> 30;iOutput = iOutput << 2;iOutput = iOutput + (short int)iTemp;

} return iOutput;}

unsigned char BasicLCG::nextChar(){ unsigned char cOutput; unsigned long iTemp; int i; for(i=0; i<4; i++) {

iCurrent = (3039177861 * iCurrent + 1);iTemp = iCurrent >> 30;cOutput = cOutput << 2;cOutput = cOutput + (char)iTemp;

} return cOutput;}

int BasicLCG::nextBit(){ iCurrent = (3039177861 * iCurrent + 1); return iCurrent >> 31;}

double BasicLCG::nextDouble(){ return (double)nextNumber()/0xFFFFFFFF;}int BasicLCG::inRange(int iMin, int iMax)

17

Page 18: Cryptography Practical File

{ int Diff; //IF the user put them in backwards then swap them if (iMax<iMin) {

//Integer swapiMax = iMax ^ iMin; //iMax holds iMax ^ iMiniMin = iMax ^ iMin; //iMin= (iMax ^ iMin) ^ iMin = iMax (original)iMax = iMax ^ iMin; //iMax= (iMax ^ iMin) ^ iMax = iMin (original)

} Diff = iMax - iMin + 1; return (int) (nextDouble()*Diff)+iMin;}

/*************OUTPUT*****************/

104 231 158 44 31 25 205 174 227 164 112 129 56 199 159 79 23 63 154 113 132 219 34 225 208 225 22 227 137 4 99 233 161 227 254 83 173 65 48 9 35184 64309 46178 26147 26817 42481 65326 11479 7342 39 14563 16493 12040 36384 33542 33001 41403 47481 4341 61169 59629 58342 57368 868 57151 21474 23993 27031 53251 46618 24011 64130 50766 53931 6445 22696 62799 41294 53963 63426 2446037193 2584944802 1083807419 3957454358 3005483111 967385103 1887406366 3510907291 1766644213 3778351129 392963168 815746678 2293430940 2179885386 371803871 792459468 2622646329 601311482 1775676468 836489552 3933509522 3888674175 2395135682 321961117 3784802356 1317630582 369087465 768367268 3087433631 653822650 2772813284 1432054709 464759273 4166845075 2814608007 2230044548 3852479767 101048510 106490291 1546551415

18

Page 19: Cryptography Practical File

PROGRAM #6

Miller – Rabin Algorithm

TEST(n)1. Find integers k, q, with k>0 , q odd, so that (n-1 = 2kq);2. select a random integer a, 1 < a < n-1;3. if aq mod n = 1 then return (“inconclusive”);4. for j = 0 to k-1 do 5. if a2^j q mod n ≡ n-1 then return(“inconclusive”);6. return(“composite”);

Implementation in c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "c:\tc\bin\integer.h"

#define COMPOSITE 0#define PRIME 1

integer modular_exponent(integer base, integer power, integer modulus) { int i, bit; integer result = create_integer(modulus.num_components + 1); integer temp = create_integer(modulus.num_components*2 + 1); set_zero_integer(result); result.c[0] = 1;

19

Page 20: Cryptography Practical File

for(i=power.num_components - 1; i>=0; i--) {for(bit=COMPONENT_BITS-1; bit>=0; bit--) { multiply_integer(result, result, temp); mod_integer(temp, modulus, result); if ((power.c[i] & (1 << bit)) != 0) {

multiply_integer(result, base, temp);mod_integer(temp, modulus, result);

}}

}

free_integer(temp); return result;}

int miller_rabin_pass(integer a, integer n) { int i, s, result; integer a_to_power, d, one, n_minus_one; integer temp = create_integer(n.num_components*2 + 1);

one = create_integer(1); set_zero_integer(one); one.c[0] = 1; n_minus_one = create_integer(n.num_components); subtract_integer(n, one, n_minus_one);

s = 0; d = create_integer(n_minus_one.num_components); copy_integer(n_minus_one, d); while ((d.c[0] % 2) == 0) {

shift_right_one_integer(d);s++;

}

a_to_power = modular_exponent(a, d, n);

20

Page 21: Cryptography Practical File

if (compare_integers(a_to_power, one) == 0) { result=PRIME; goto exit; } for(i=0; i < s-1; i++) {

if (compare_integers(a_to_power, n_minus_one) == 0) { result=PRIME; goto exit; }

multiply_integer(a_to_power, a_to_power, temp);mod_integer(temp, n, a_to_power);

} if (compare_integers(a_to_power, n_minus_one) == 0) { result=PRIME; goto exit; } result = COMPOSITE;

exit: free_integer(temp); free_integer(a_to_power); free_integer(one); free_integer(n_minus_one); return result;}

void random_integer(integer max, integer result) { int i, most_sig = max.num_components - 1; while (max.c[most_sig] == 0) most_sig--; for (i=0; i<most_sig; i++) {

result.c[i] = rand() % (MAX_COMPONENT + 1); } result.c[most_sig] = rand() % max.c[most_sig];}

int miller_rabin(integer n) { integer a = create_integer(n.num_components); int repeat; for(repeat=0; repeat<20; repeat++) {

do {

21

Page 22: Cryptography Practical File

random_integer(n, a);} while (is_zero_integer(a));if (miller_rabin_pass(a, n) == COMPOSITE) { return COMPOSITE;}

} return PRIME;}

int main(int argc, char* argv[]) { srand(time(NULL)); if (strcmp(argv[1], "test") == 0) {

integer n = string_to_integer(argv[2]);puts(miller_rabin(n) == PRIME ? "PRIME" : "COMPOSITE");

} else if (strcmp(argv[1], "genprime") == 0) {integer max = create_integer(atoi(argv[2])/COMPONENT_BITS);integer p = create_integer(max.num_components);set_zero_integer(max);max.c[max.num_components-1] = MAX_COMPONENT;do { random_integer(max, p); if ((p.c[0] % 2) == 0) continue; if (mod_small_integer(p, 3) == 0) continue; if (mod_small_integer(p, 5) == 0) continue; if (mod_small_integer(p, 7) == 0) continue;

} while (miller_rabin(p) == COMPOSITE);puts(integer_to_string(p));

} return 0;}

22