Professional Documents
Culture Documents
html
Cieaser Cipher
#include <stdio.h>
#include <conio.h>
void main()
{
int i, c;
char str[100];
printf("Enter the Text Message : ");
gets(str);
for (i = 0; i < strlen(str); i++)
{
switch (str[i])
{
case 'x':
str[i] = 'a';
continue;
case 'y':
str[i] = 'b';
continue;
case 'z':
str[i] = 'c';
continue;
case 'X':
str[i] = 'A';
continue;
case 'Y':
str[i] = 'B';
continue;
case 'Z':
str[i] = 'C';
continue;
}
if (str[i] >= 'a' && str[i] < 'x')
str[i] = str[i] + 3;
else if (str[i] >= 'A' && str[i] < 'X')
str[i] = str[i] + 3;
}
printf("Message After Encryption : \n");
puts(str);
for (i = 0; i < strlen(str); i++)
{
switch (str[i])
{
case 'a':
str[i] = 'x';
continue;
case 'b':
str[i] = 'y';
continue;
case 'c':
str[i] = 'z';
continue;
case 'A':
str[i] = 'X';
continue;
case 'B':
str[i] = 'Y';
continue;
case 'C':
str[i] = 'Z';
continue;
}
if (str[i] >= 'd' && str[i] <= 'z')
str[i] = str[i] - 3;
else if (str[i] >= 'D' && str[i] < 'Z')
str[i] = str[i] - 3;
}
printf("Message After Decryption : \n");
puts(str);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
void line(int a)
{
int i;
printf("\n");
for(i=0;i<a;i++)
{
printf("*");
}
printf("\n");
}
char s[30];
void main()
{
char c[27],k[30],temp[40],matrix[5][5],first,second;
int i,index,j,t,t1,p,p1,gd,gm;
initgraph(&gm,&gd, "c:\\tc\\bgi");
clrscr();
cleardevice();
//-------------------------ABCD in c[]--------------------------------setcolor(2);
outtextxy(400,460,"Prepared By:Khimani Chirag");
setcolor(2);
outtextxy(150,10,"Welcome To Playfair Cipher Encryption");
for(i=97;i<=122;i++)
{
c[i-97]=i;
}
//--------------------------------------------------------------------printf("\n\n");
line(50);
printf("enter the plain text: ");
gets(s);
printf("\nenter key for playfair cipher: ");
gets(k);
line(50);
//---------------------------put c[]<--* when key----------------------for(i=0;k[i]!='\0';i++)
{
index=k[i]-97;
if(c[index]=='*')
{
k[i]='*';
}
else
{
c[index]='*';
}
}
//------------------------------put key[] in variable------------------i=0;
j=0;
while(k[i] !='\0')
{
if(k[i]!='*' )
{
temp[j]=k[i];
j++;
}
i++;
}
//-----------------------------put c[] in temp variable-----------------i=0;
while(c[i] !='\0')
{
if(c[i]!='*' && c[i]!='j' )
{
temp[j]=c[i];
j++;
}
i++;
}
printf("\nyour matrix is:\n");
line(20);
//-----------------------------matrix printing--------------------------t=0;
for(i=0;i<5;i++)
{
printf("* ");
for(j=0;j<5;j++)
{
printf("%c ",temp[t]);
matrix[i][j]=temp[t];
t++;
}
printf(" *");
if(i!=4)
printf("\n");
}
line(20);
printf("\nyour cipher text is: ");
//-----------------------------------------------------------------------i=0;
if(strlen(s)%2!=0)
{
while(s[i]!='\0')
{
i++;
}
s[i]='x';
s[i+1]='\0';
}
for(index=0;index<strlen(s);index+=2)
{
first=s[index];
second=s[index+1];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(matrix[i][j]==first)
{
t=i;
t1=j;
}
if(matrix[i][j]==second)
{
p=i;
p1=j;
}
}
}
if(p==t)
{
if(p1==4)
{
p1=-1;
}
if(t1==4)
{
t1=-1;
}
printf("%c%c",matrix[t][t1+1],matrix[p][p1+1]);
}
else if(p1==t1)
{
if(p==4)
{
p=-1;
}
if(t==4)
{
t=-1;
}
printf("%c%c",matrix[t+1][t1],matrix[p+1][p1]);
}
else
{
printf("%c%c",matrix[t][p1],matrix[p][t1 ]);
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
clrscr();
printf("\nEnter the input string : ");
gets(a);
l=strlen(a);
/*Ciphering*/
for(i=0,j=0;i<l;i++)
{
if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{
if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence :");
printf("\n%s",c);
/*Deciphering*/
if(l%2==0)
k=l/2;
else
k=(l/2)+1;
for(i=0,j=0;i<k;i++)
{
d[j]=c[i];
j=j+2;
}
for(i=k,j=1;i<l;i++)
{
d[j]=c[i];
j=j+2;
}
d[l]='\0';
printf("\nText after decryption : ");
printf("%s",d);
getch();
}
Here is source code of the C++ Program to Implement the Vigenere Cypher. The C++
program is successfully compiled and run on a Linux system. The program output is also
shown below.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
#include <iostream>
#include <string>
using namespace std;
class Vigenere
{
public:
string key;
Vigenere(string key)
{
for (int i = 0; i < key.size(); ++i)
{
if (key[i] >= 'A' && key[i] <= 'Z')
this->key += key[i];
else if (key[i] >= 'a' && key[i] <= 'z')
this->key += key[i] + 'A' - 'a';
}
}
string encrypt(string text)
{
string out;
for (int i = 0, j = 0; i < text.length(); ++i)
{
char c = text[i];
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;
out += (c + key[j] - 2 * 'A') % 26 + 'A';
j = (j + 1) % key.length();
}
return out;
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
}
string decrypt(string text)
{
string out;
for (int i = 0, j = 0; i < text.length(); ++i)
{
char c = text[i];
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;
out += (c - key[j] + 26) % 26 + 'A';
j = (j + 1) % key.length();
}
return out;
}
};
int main()
{
Vigenere cipher("VIGENERECIPHER");
string original =
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
string encrypted = cipher.encrypt(original);
string decrypted = cipher.decrypt(encrypted);
cout << original << endl;
cout << "Encrypted: " << encrypted << endl;
cout << "Decrypted: " << decrypted << endl;
}
RSA Algorithm
/* C program for the Implementation Of 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);
}
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();
}
#include <stdio.h>
#include <math.h>
void main()
{
int q,alpha,xa,xb,ya,yb,ka,kb, x,y,z,count,ai[20][20];
printf("Enter a Prime Number \"q\":");
scanf("%d",&q);
printf("Enter a No \"xa\" which is lessthan value of q:");
scanf("%d",&xa);
printf("Enter a No \"xb\" which is lessthan value of q:");
scanf("%d",&xb);
for(x=0;x<q-1;x++) //Primitive Root Calculation
for(y=0;y<q-1;y++)
ai[x][y] = ((int)pow(x+1,y+1))%q;
for(x=0;x<q-1;x++)
{
count = 0;
for(y=0;y<q-2;y++)
{
for(z=y+1;z<q-1;z++)
if(ai[x][y] == ai[x][z])
{
count = 1;
break;
}
if(count == 1)
break;
}
if (count == 0 )
{
alpha = x+1;
break;
}
}
printf("alpha = %d\n",alpha);
ya = ((int)pow(alpha,xa))%q; yb = ((int)pow(alpha,xb))%q;
ka = ((int)pow(yb,xa))%q; kb = ((int)pow(yb,xb))%q;
printf("ya = %d\nyb = %d\nka = %d\nkb = %d\n",ya,yb,ka,kb);
if(ka == kb) printf("The keys exchanged are same");
else printf("The keys exchanged are not same");
}