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

Cryptography and Network Security: Practical File

The document contains programs implementing various cryptography and network security algorithms. Program 1 implements the Caesar cipher algorithm to encrypt text by shifting letters by a key value. Program 2 implements the Euclidean algorithm to calculate the greatest common divisor of two numbers. Program 3 finds the GCD of 10 numbers stored in an array using the Euclidean algorithm. Program 4 implements the RSA encryption algorithm to encrypt and decrypt messages using public and private keys. Program 5 implements a linear congruential generator pseudorandom number generator.

Uploaded by

anxena
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views

Cryptography and Network Security: Practical File

The document contains programs implementing various cryptography and network security algorithms. Program 1 implements the Caesar cipher algorithm to encrypt text by shifting letters by a key value. Program 2 implements the Euclidean algorithm to calculate the greatest common divisor of two numbers. Program 3 finds the GCD of 10 numbers stored in an array using the Euclidean algorithm. Program 4 implements the RSA encryption algorithm to encrypt and decrypt messages using public and private keys. Program 5 implements a linear congruential generator pseudorandom number generator.

Uploaded by

anxena
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 22

Cryptography and Network Security

PRACTICAL FILE
TIT-751

SUBMITTED TO SUBMITTED BY

1
INDEX

Remarks &
Sl.No. Name of Practicals PageNo. Date
Signature
Caesar Cipher
1. implementation 3-5
Eucledian
2. Algorithm 6-7

Euclidean
Algorithm for 10
3. 8-9
numbers stored in
array
Implementation of
4. RSA algorithm 10-12

Implementation of
5. LCG algorithm 13-18

Implementation of
6. miller – rabin 19-22
algorithm

7.

2
PROGRAM 1

Caesar Cipher Algorithm:

Earliest known use of substitution cipher was by Julius Caesar.


1. assign a numerical equivalent to each letter
a=0
b=1
c=2
.
.
.
.
.
.
y = 24
z = 25.

2. For each plain text letter p, substitute the cipher text letter C
As
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
Implementation of caesar cipher :

/*ceasar cipher ….. …………*/


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

void main()
{
int a, i, d;
char arr[5];
clrscr();
printf("Enter the character to be encoded\n");

for(i=0;i<=5;i++)
{
scanf("%c", &arr[i]);
}
printf("Enter the value to be added");
scanf("%d", &a);

for(i=0;i<=5;i++)
{
if(arr[i] >=96||arr[i]<=122)
{
arr[i] = arr[i] +a;
}
else if(arr[i] >122)
{
d = arr[i]%122;
arr[i] = (d-1) + 97;
}
printf("%c", arr[i]);
}
getch();
}

4
/*******************OUTPUT******************/

Enter the character to be encoded


abcdef
Enter the value to be added3
defghi

5
PROGRAM #2

Eucledian algorithm

EUCLID(a,b)
1. A  a; B  b;
2. if B=0 return A = gcd(a,b)
3. R = A mod B
4. A  B
5. B  R
6. goto step 2.

Implementation of Eucledian algorithm in C

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

int gcd(int,int);

void main()
{
int x,y,GCD;
clrscr();
printf("Enter the number whose GCD is to be calculated\n");
scanf("%d \n%d", &x,&y);

if(x==0)
GCD = y;
else if(y==0)
GCD = x;
else
GCD = gcd(x,y);

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

int gcd(int a, int b)


{
int g, k;
if(b==0)
g=a;
else{
k = a%b;
g = gcd(b,k);
}
return(g);
}

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

Enter the number whose GCD is to be calculated


55
35
The GCD of the given numbers is
5

7
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
}
int gcd(int x,int y)
{
if(x==0)
return y;
if(y==0)
return x;
return(gcd((y%x),x));

9
PROGRAM #4

RSA algorithm.

1. Select two prime number p & q.


2. Calculate n=p*q.
3. Calculate Ф(n)=(p-1)*(q-1).
4. Select e such that e relatively prime to Ф(n) and e<Ф(n).
5. Determining d such that d*e ≡ 1(mod Ф(n)).
6. cipher text C = Me mod n , {M = message}
7. Plaintext M = Cd mod n .

/* 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
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
}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
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
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 number
unsigned short int nextInt();
unsigned char nextChar();
int nextBit();
double nextDouble();
int inRange(int min, int max);
};

//Just a little test code to print some numbers


int main()
{
BasicLCG rng(time(NULL));
int i;
clrscr();

14
//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
}

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
//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
{
int Diff;
//IF the user put them in backwards then swap them
if (iMax<iMin)
{
//Integer swap
iMax = iMax ^ iMin; //iMax holds iMax ^ iMin
iMin = 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
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
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
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
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

You might also like