You are on page 1of 16

Program to implement Caesar Cipher

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

void main()
{
clrscr();
char plain[100], cipher[100];
printf( "Enter the plain text (max 100 words)”) ;
gets(plain);
for (int i=0; i < strlen(plain);i++)
{
if(plain[i]==' ')
{
cipher[i]=plain[i];
}
else if(plain[i]==120||plain[i]==121||plain[i]==122)
{
cipher[i]=plain[i]-23;
}
else
{
cipher[i]=plain[i]+3;
}
}
Printf("\n\nENCRYPTED TEXT:\n");
for(I = 0; I < strlen(plain); i++)
{
cout<<cipher[i];
}
Printf("\n\nDECRYPTED TEXT:\n");
for(I = 0; I < strlen(plain); i++)
{
if(cipher[i]==' ')
{
cout<<cipher[i];
}
else if(cipher[i]==97||cipher[i]==98||cipher[i]==99)
{
plain[i]=cipher[i]+23;
cout<<plain[i];
}
else
{
plain[i]=cipher[i]-3;
cout<<plain[i];
}
}
getch();
}

Output
Program to implement Rail fence

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

void main()
{
clrscr();
char normal[100], plain[100], cipher1[50], cipher2[50];
int c1len=0, c2len=0;
cout << "Enter the plain text (max 100 words):: ";
gets(normal);
for(int i=0,j=0;i<strlen(normal);i++)
{
if(normal[i]!=' ')
{
plain[j]=normal[i];
j++;
}
}
int len = j;
int a = len%2;
if (a == 0)
{ c1len=len/2;
c2len=len/2;
for(i=0,j=0;i<len-1,j<c1len;i+=2,j++)
{
cipher1[j]=plain[i];
}
for(i=1,j=0;i<len,j<c2len;i+=2,j++)
{
cipher2[j]=plain[i];
}
}
else
{
c1len = (len/2) + 1;
c2len = len/2;
for (i = 0, j = 0; I < len , j < c1len; I += 2, j++)
{
cipher1[j]=plain[i];
}
for(i = 1, j = 0; I < len-1, j < c2len; I += 2, j++)
{
cipher2[j]=plain[i];
}
}
cout << "\n\nEncrypted Text:\n";
for(i=0;i<c1len;i++)
{
cout<<cipher1[i];
}
for(i=0;i<c2len;i++)
{
cout<<cipher2[i];
}
cout<<"\n\nDecrypted Text:\n";
for(i=0;i<c2len;i++)
{
cout<<cipher1[i]<<cipher2[i];
}
if (a==1)
{
cout << cipher1[c1len-1];
}
getch();
}

Output
Program to implement Extended Euclid
#include <stdio.h>

#include<string.h>

int main (int argc, const char * argv[]) {

int a, A, b, B, c, d, m, n, q, r = 1, t, x;

printf("\n\n\n\nThis program will find the Greatest Common Factor of two


numbers\n");

printf("m = ");

scanf ("%d", &m);

printf("n = ");

scanf ("%d", &n);

printf("\n\nWorking numbers\n\n");

printf("A\t a\t B\t b\t c\t d\t q\t r\n");

printf("------------------------------------------------------------------\n");

A = b = 1;

a = B = 0;

c = m;

d = n;

//a' <- b <- 1, a <- b' <- 0, c <- m, d <- n

while (r != 0)

{
q = (c / d);

r = (c % d);

printf("%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n",

A, a, B, b, c, d, q, r);

x = d;

c = d;

d = r;

t = A;

A = a;

a = (t - (q * a));

t = B;

B = b;

b = (t - (q * b));

printf("------------------------------------------------------------------\n");

printf("\nThe Greatest Common Divisor of %d and %d is %d\n\n", m, n, x);

return 0;

Output
Program to implement CRT
#include <stdio.h>
#include <stdlib.h>

typedef struct remainder { int divisor; int residual; } remainder;

int main ( void )


{
    
    void getData ( remainder * arr, int const * const num );

    int cRT ( remainder * arr, int const * const num );

    int num;

    remainder * arr = NULL;

    for (  ;  ;  )
    {

               printf ("Enter the quantity of data to be computed ");

        scanf ("%i", &num);

       

        if ( num <= 0 )


        {

            return 0;

        }

        arr = (remainder*)calloc (num, sizeof(remainder));

        getData(arr, &num);


        printf ("\nThe solution is %i.\n\n", cRT(arr, &num));

        free(arr);

    }

void getData ( remainder * arr, int const * const num )


{

    register int i;

    for ( i = 0; i < *num; i++ )


    {

        printf ("\nEnter divisor and residual for Chinese Remainder Theorem ");

        scanf ("%i %i", &arr[i].divisor, &arr[i].residual);

    }

int cRT ( remainder * arr, int const * const num )


{

    register int i, add = arr->divisor, result = arr->residual;

    int lcm ( int a, int b );

    for ( i = 1 ; i < *num ; add = lcm(add, arr[i++].divisor) )


    {

        for (  ; result % arr[i].divisor != arr[i].residual ; result += add );

    }
    return result;

int lcm ( int a, int b )


{

    int gcd ( int a, int b );

    return a * b / gcd ( a , b );

int gcd ( register int a, register int b )


{

    register int temp;

    while (b != 0)
    {

        temp = b;

        b = a % b;

        a = temp;

    }

    return a;
}

Output
Program to implement RSA Algorithm
# include<iostream.h>
# include<conio.h>

int calculateD (int phi, int e)


{
int s;
int d = 1;
do
{
s = (d * e) % phi;
d++;
} while (s != 1);
return (d - 1);
}

char GenEncMsg(long message, int e, int n)


{
long cipher = 1;
for (int i = 0; i < e; i++)
cipher = cipher * message % n;
cipher = cipher % n;
return (char)cipher;
}

char GenDecMsg(long cipher, int d, int n)


{
long message = 1;
for (int i = 0; i < d; i++)
message = message * cipher % n;
message = message % n;
return (char)message;
}

void main()
{
clrscr();
int e, p, q, n, phi, d;
char msgChar, cphChar;
long message, cipher;
cout << "Enter to prime numbers:: ";
cin >> p >> q;
cout << "Enter the value of e:: ";
cin >> e;
n = p *q;
phi = (p-1)*(q-1);
d = calculateD(phi, e);
cout << "Enter message:: ";
cin >> msgChar;
message = msgChar;
cphChar = GenEncMsg(message, e, n);
cout << "Encrypted Message = " << cphChar << endl;
cipher = cphChar;
cout << "Decrypted Message = " << GenDecMsg(cipher, d, n);
getch();
}

Output

Program to implement Diffie–Hellman key exchange


# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
# include<math.h>
long A, B, K1, K2;

int compA (int g, int n)


{
randomize();
int x = rand()%10;
A = pow(g,x);
A %= n;
return x;
}

int compB(int g, int n)


{
randomize();
int y = rand()%10;
B = pow(g,y);
B %= n;
return y;
}

void key_A(int x, int n)


{
K1 = pow(B,x);
K1 %= n;
}

void key_B(int y, int n)


{
K2 = pow(A,y);
K2 %= n;
}

main()
{
clrscr();
int n, g;
cout << "Enter the value of \"g\" and \"n\":: ";
cin >> g >> n;
int t1 = compA(g,n);
int t2 = compB(g,n);
key_A(t1,n);
key_B(t2,n);
cout << "Key at A, K1 = " << K1;
cout << endl << "Key at B, K2 = " << K2;
getch();
return 0;
}

Output
INDEX

S.N Practical Date Sign


o

1 Program to implement Caesar cipher

2 Program to implement Rail-fence algorithm

3 Program to implement Extended Euclid

4 Program to implement CRT

5 Program to implement RSA algorithm

6 Program to implement Diffie-Hellman key


exchange algorithm

You might also like