Cryptography and Network Security: Practical File
Cryptography and Network Security: Practical File
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
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
3
Implementation of caesar cipher :
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******************/
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.
#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();
}
/********************OUTPUT*******************/
7
PROGRAM #3
#include<conio.H>
#include<stdio.h>
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.
#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();
}
F(n) = 96
Enter e : 5
Encrypted keyword : 66
Decrypted keyword : 19
12
PROGRAM #5
LCG Algorithm:
13
Implementation of Linear congruential generator.
/**********************************************
/* A Basic Linear Congruential Generator
/* ---------------------------------------
/**********************************************/
#include<iostream.h>
#include<time.h>
#include<conio.h>
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);
};
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
}
15
}
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;
}
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*****************/
18
PROGRAM #6
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
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;
}
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++;
}
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;
}
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;
}
22