WAP to Implement One Time Pad Algorithm

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char pt[50],key[50],ct[50];
int i,n;
printf("Enter Plain Text\n");
gets(pt);
printf("Enter Key\n");
gets(key);
if(strlen(pt)==strlen(key))
{

for(i=0;i<strlen(pt);i++)
{
pt[i]=pt[i]-96;
key[i]=key[i]-96;
ct[i]=pt[i]+key[i];

while(ct[i] > 25)
{
ct[i]=ct[i]-26;
}
}

for(i=0;i<strlen(pt);i++)
{
ct[i]+=96;
printf("%c",ct[i]);
}
}
else
{
printf("\nPlain Text and Key Have No Same Length");
}
getch();
return 0;
}

WAP to Implement Diffi-Helman Algorithm

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<MATH.H>
void main()
{
int  a,xa,xb,b,c,d,e;
clrscr();
int ya,yb,k1,k2,q;
printf("enter prime no q:=");
scanf("%d",&q);
printf("Enter prime no a where a is primitive root of q:=");
scanf("%d",&a);
printf("enter secret key xa:=");
scanf("%d",&xa);
printf("enter secret key xb:=");
scanf("%d",&xb);

b=pow(a,xa);
ya=b%q;
c=pow(a,xb);
yb=c%q;
printf("value of public key ya:=%d\n",ya);
printf("value of public key yb:=%d\n",yb);

d=pow(yb,xa);
k1=d%q;
e=pow(ya,xb);
k2=e%q ;
printf("value of k1:=%d\n", k1);
printf("value of k2:=%d\n",k2);
printf("here we get k1=k2 so keys are exchanged");
getch();
}

WAP to Implement 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;
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);
}
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++;
}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();
}

WAP to Implement Mono-alphabetic Algorithm

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,len;
char      c[50],p[50],k[26]={'m','e','h','p','x','a','w','q','y','d','i','s','z','b','o','g','c','u','f','j','v','t','k','n','l','r'},l[26]={'M','E','H','P','X','A','W','Q','Y','D','I','S','Z','B','O','G','C','U','F','J','V','T','K','N','L','R'};
clrscr();
printf("Enter the plain text:");
gets(p);
len=strlen(p);
for(i=0;i<len;i++)
{
if(p[i]<123 && p[i]>96)
{
j=p[i];
c[i]=k[j-97];
}
if(p[i]<91 && p[i]>64)
{
j=p[i];
c[i]=l[j-65];
}
}
c[i]=NULL;
printf("The cipher text is:\n %s",c);
getch();
}

WAP to Implement Creaser Cipher Algorithm

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

void enc(char p[]);
void dec(char c[]);
int k;
void main()
{
char p[20];
clrscr();
printf("enter the plaintext:");
scanf("%s",&p);
printf("enter the key:");
scanf("%d",&k);
enc(p);
getch();
}
void enc(char p[])
{
int i;
char c[50];
for(i=0;i<strlen(p);i++)
{
c[i]=p[i]+k;
if((c[i]>90 && c[i]<94)||(c[i]>122 && c[i]<126))
c[i]=p[i]+k-26;
}
c[i]=NULL;
printf("encrypted message: %s",c);
dec(c);
}
void dec(char c[])
{
int i;
char p[100];
for(i=0;i<strlen(c);i++)
{
p[i]=c[i]-k;
if((c[i]>64 && c[i]<69)||(c[i]>96 && c[i]<100))
p[i]=c[i]-k+26;
}
p[i]=NULL;
printf("\n decrypted message:%s",p);
}