Professional Documents
Culture Documents
#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 a, A, b, B, c, d, m, n, q, r = 1, t, x;
printf("m = ");
printf("n = ");
printf("\n\nWorking numbers\n\n");
printf("------------------------------------------------------------------\n");
A = b = 1;
a = B = 0;
c = m;
d = n;
while (r != 0)
{
q = (c / d);
r = (c % d);
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");
return 0;
Output
Program to implement CRT
#include <stdio.h>
#include <stdlib.h>
int num;
for ( ; ; )
{
}
free(arr);
}
register int i;
printf ("\nEnter divisor and residual for Chinese Remainder Theorem ");
}
}
return result;
return a * b / gcd ( a , b );
while (b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
Output
Program to implement RSA Algorithm
# include<iostream.h>
# include<conio.h>
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
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