You are on page 1of 10

// Program for additive cipher

#include"conio.h"
#include"iostream.h"
#include"string.h"
#include"stdio.h"
void main()
{
char a[25],b[25],plaintxt[100],ciphrtxt[100],key;
int p=0,k=0,key1;
clrscr();
cout<<"\t\t\t..Output Of Additive Cipher Technique..\n";
for(int i=97,j=0;i<=122,j<=25;i++,j++)
{
a[j]=char(i);
b[j]=char(i-32);
cout<<"\n\t\t\t\t";
cout<<"|"<<" "<<a[j];
cout<<"|"<<" "<<b[j];
if(j<9)
cout<<"|"<<" "<<j+1<<"|";
else
cout<<"|"<<" "<<j+1<<"|";
} cout<<"\n\nEnter the Msg to be Encrypted :
";
gets(plaintxt);
cout<<"\nEnter the Key Value : ";
cin>>key1;
cout<<"\n---------------------";
for(i=0;i<strlen(plaintxt);i++)
{
for(k=0;k<26;k++)
{
if(plaintxt[i]==a[k]||plaintxt[i]==b[k])
{
p=k+1;
}
} if(p!=0)
{
cout<<"\nIndex of '"<<plaintxt[i]<<"' is : " <<p;
int pos=(p+key1)%26;
ciphrtxt[i]=pos;
} else
{
cout<<"\n\nNo result found\n";
}
} cout<<"\n---------------------";
cout<<"\n\nEncrypted Message is : ";
for(i=0;i<strlen(plaintxt);i++)
{
if(int(plaintxt[i])>=97&&int(plaintxt[i])<=122)
cout<<a[ciphrtxt[i]-1];
else if(plaintxt[i]>=65&&plaintxt[i]<=90)
cout<<b[ciphrtxt[i]-1];
else
{
}
}
getch();
}

// Program for affine cipher technique


#include"conio.h"
#include"iostream.h"
#include"string.h"
#include"stdio.h"
void main()
{
char a[25],b[25],plaintxt[100],ciphrtxt[100],temptxt[100];
int p=0,k=0,tempkey,key2;
clrscr();
cout<<"\t\t ..Output Of Affine Cipher Technique..\n";
for(int i=97,j=0;i<=122,j<=25;i++,j++)
{
a[j]=char(i);
b[j]=char(i-32);
cout<<"\n\t\t\t\t";
cout<<"|"<<" "<<a[j];
cout<<"|"<<" "<<b[j];
if(j<9)
cout<<"|"<<" "<<j+1<<"|";
else
cout<<"|"<<" "<<j+1<<"|";
} cout<<"\n\nEnter the Msg to be Encrypted :
";
gets(plaintxt);
cout<<"\nEnter the First Key Value : ";
cin>>tempkey;
// int tempkey=key;
cout<<"\nEnter the Second Key Value : ";
cin>>key2;
//int key2=key1;
cout<<"\n---------------------";
for(i=0;i<strlen(plaintxt);i++)
{
for(k=0;k<26;k++)
{
if(plaintxt[i]==a[k]||plaintxt[i]==b[k])
{
p=k+1;
}
} if(p!=0)
{
cout<<"\nIndex of '"<<plaintxt[i]<<"' is : " <<p;
int pos=(p*tempkey)%26;
temptxt[i]=pos;
} else
{
cout<<"\n\nNo result found\n";
}
ciphrtxt[i]=int((temptxt[i]+key2)%26);
}
/////////////////////////////////////////////////////////////////////////////
cout<<"\n---------------------";
cout<<"\n\nTemporary Message is : ";
for(i=0;i<strlen(plaintxt);i++)
{
if(int(plaintxt[i])>=97&&int(plaintxt[i])<=122)
cout<<a[temptxt[i]-1];
else if(plaintxt[i]>=65&&plaintxt[i]<=90)
cout<<b[temptxt[i]-1];
} cout<<"\n---------------------";
cout<<"\n\nEncrypted Message is : ";
for(i=0;i<strlen(plaintxt);i++)
{
if(int(plaintxt[i])>=97&&int(plaintxt[i])<=122)
cout<<a[ciphrtxt[i]-1];
else if(plaintxt[i]>=65&&plaintxt[i]<=90)
cout<<b[ciphrtxt[i]-1];
}
getch();
}

//program for subsitution cipher


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[100],b[100],c;
int i=0,n,temp=0,flag=0,key=3,len=0;
do{
clrscr();
cout<<"\t\t**CEASER CIPHER ENCRYPTION IMPLEMENTATION**";
cout<<"\n\n Enter the Message To be Encrypted : ";
gets(a);
cout<<"\nKey Value is 3";
len=strlen(a);
for(i=0;i<=len;i++)
{
n=int(a[i]);
flag=0;
temp=n+key;
if(temp>122)
{
temp=((temp-123)+97);
b[i]=char(temp);
flag=1;
} else if((temp>90)&&(temp<97))
{
temp=((temp-91)+65);
b[i]=char(temp);
flag=1;
} if(flag==0)
{
if(temp==32+key)
b[i]=char(32);
else
b[i]=char(temp);
}
temp=0;
}
printf("\nEncrypted message is : ");
for(i=0;i<=len-1;i++)
{
if(int(b[i])>0)
{
printf("%c",b[i]);
}
}
cout<<"\n\nWould You Like To Continue (y/n) :";
cin>>c;
}
while(c!='n');
}

//Program for Hash Function


#include"conio.h"
#include"iostream.h"
#include"math.h"
#include"string.h"
#include"stdio.h"
char *tobin(int x)
{
char *ans=new char[20];
int i=0;
while(x>=1)
{
if(x%2==1)
ans[i]='1';
else
ans[i]='0';
x/=2;
i++;
} if(x==1)
ans[i]='1', i++;
ans[i]='\0';
strrev(ans);
return(ans);
}
void main()
{
clrscr();
char *b=new char[20];
char *mdb=new char[10];
char *finalb=new char[200];
char *fd=new char[200];
char pt[100];
int g=0,c=0;
cout<<"Enter Your Msg Here : ";
gets(pt);
for(int i=0;i<strlen(pt);i++)
{
b=tobin(pt[i]);
mdb=(b);
int n=0;
c=strlen(b)+g;
for(int m=g;m<(c);m++)
{
finalb[m]=b[n];
n+=1;
g+=1;
}
cout<<"\nBinary Value of "<<pt[i]<<" is :";
cout<<" ";
for(int k=0;k<strlen(b);k++)
{
cout<<b[k];
}
}
mdb=strrev(mdb);
cout<<"\n\nXOR Function Starter is : ";
for(int k=0;k<strlen(mdb);k++)
{
cout<<mdb[k];
}
cout<<"\n\nFinal Binary of the Msg is : ";
for(int l=0;l<c;l++)
cout<<finalb[l];
int w=0;
cout<<"\nFinal Digest Value is : ";
for(int v=0;v<c;v++)
{
if(finalb[v]==mdb[w])
fd[v]='0';
else
fd[v]='1';
w+=1;
if((strlen(finalb))%(strlen(mdb))==0)
{
w=0;
} cout<<fd[v];
}
getch();
}

//Program For RSA Encryption


# include <iostream.h>
# include <string.h>
# include <fstream.h>
# include <math.h>
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# define ul unsigned long
ul isPrime(ul n)
{
ul b=1;
for(ul i=2;i<n;i++)
if(n%i==0)
{
b=0; break;
}
return b;
}
ul Power(ul x,ul y)
{
ul ans=1;
if(x==1)
return x;
for(ul i=1;i<=y;i++)
ans*=x;
return(ans);
} char *
ToBin(int x)
{
char *ans=new char[20];
int i=0;
while(x>=1)
{
if(x%2==1)
ans[i]='1';
else
ans[i]='0';
x/=2;
i++;
} if(x==1)
ans[i]='1',i++;
ans[i]='\0';
strrev(ans);
return(ans);
}
ul Crypt(int x,int key,int n) // (x^y)%z //Method 2
{
char *B=new char[20];
B=ToBin(key);
ul ans=1;
int c=1;
for(ul i=0;i<strlen(B);i++)
{
c=2*c;
ans=(ans*ans)%n;
if(B[i]=='1')
{
c=c+1;
ans=(ans*x)%n;
}
}
return(ans);
}
void main()
{
clrscr();
randomize();
/* K E Y G E N E R A T I O N */
ul p,q,n,n1,e,d;
Beg: // Reading 2 random prime-numbers from 20 to 50
p=2+rand()%200;
q=2+rand()%200;
if(p==q||!isPrime(p)||!isPrime(q)) goto Beg;
n = p * q;
n1 = (p-1) * (q-1);
ul x=n1+1;
for(e=2;e<=x/2;e++)
{
if(x%e==0)
break;
}
d=x/e;
if(p==1||q==1||e==1||d==1||e==d||q==e||q==d||p==e||p==d)
goto Beg;
ul P1,P2,Cypher;
B1:
cout<<"Enter The PlainText (number only) : ";
cin>>P1;
Cypher=Crypt(P1,e,n); //Encrypting using public-key 'e'
P2=Crypt(Cypher,d,n);//Decrypting using private key 'd'
cout<<"\np = "<<p<<endl;
cout<<"\nq = "<<q<<endl;
cout<<"\nn =(p*q) = "<<n<<endl;
cout<<"\nn1=(p-1)*(q-1)= "<<n1<<endl;
cout<<"\ne = "<<e<<endl;
cout<<"\nd = "<<d<<endl;
cout<<"\nPublic Key : { "<<e<<","<<n<<" }"<<endl;
cout<<"\nPrivate Key : { "<<d<<","<<n<<" }"<<endl;
cout<<"\nPlainText Before Encryption is : "<<P1<<endl;
cout<<"\nCypherText is : "<<Cypher<<endl;
cout<<"\nPlainText After Decryption is : "<<P2<<endl;
getch();
}

//program for subsitution cipher


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[100],b[100],c;
int i=0,n,temp=0,flag=0,key=0,len=0;
do{
clrscr();
cout<<"\t\t**CEASER CIPHER ENCRYPTION IMPLEMENTATION**";
cout<<"\n\n Enter the Message To be Encrypted : ";
//for(int k=0;k<4;k++)
// {
gets(a);
//}
cout<<"\nEnter the Key Value :";
cin>>key;
len=strlen(a);
//printf("string length : %d",len);
for(i=0;i<=len;i++)
{
n=int(a[i]);
//char b[20];
flag=0;
temp=n+key;
if(temp>122)
{
temp=((temp-123)+97);
b[i]=char(temp);
flag=1;
//printf("pass if loop1");
} else if((temp>90)&&(temp<97))
{
temp=((temp-91)+65);
b[i]=char(temp);
flag=1;
//printf("pass if loop2");
} if(flag==0)
{
if(temp==32+key)
b[i]=char(32);
else
b[i]=char(temp);
}
temp=0;
}
printf("\nEncrypted message is : ");
for(i=0;i<=len-1;i++)
{
if(int(b[i])>0)
{
printf("%c",b[i]);
// temp=int(b[i]);
//printf("\n%d",temp);
}
} cout<<"\n\nWould You Like To Continue (
y/n) :
";
cin>>c;
}//do
while(c!='n');
//getch();
}

You might also like