Anda di halaman 1dari 7

/* Program for Diffie Hellmen Key generation and vernam cipher */

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
char input[20],cipher[20];
// Public Numbers p and g...
int i,p=23, g=9;
// 1st person's private key
int a=4;
// 2nd person's private key
int b=3;
// 1st person computes public value...
double x = (int)pow(g,a)%p;
// 2nd person computes public value...
double y = (int)pow(g,b)%p;
// 1st person creates the Secret key for himself
double encKey1 = (int)pow(y,a)%p;
int encKey = (int)encKey1;
// 2nd person creates the Secret key for himself
double decKey1 = (int)pow(x,b)%p;
int decKey = (int)decKey1;
// Vernam Cipher
printf("Enter Text : ");
scanf("%s",input);
printf("\nEncrypted : ");
for(i=0;i<strlen(input);i++)
{
cipher[i]=input[i]^encKey;
printf("%c",cipher[i]);
}
cipher[i]='\0';
// 2nd person decrypts the ciphertext using decKey
printf("\nDecrypted : ");
for(i=0; i< strlen(cipher); i++ )
{
input[i] = (int)cipher[i]^decKey; // using decKey
printf("%c",input[i]);
}
}
/*output */

/*Program to implement Hill Cipher */


#include<stdio.h>
#include<string.h>
int main(){
unsigned int a[3][3]={{6,24,1},{13,16,10},{20,17,15}};
unsigned int b[3][3]={{8,5,10},{21,8,21},{21,12,8}};
int i,j;
unsigned int c[20],d[20];
char msg[20];
int determinant=0,t=0;;
printf("Enter plain text\n ");
scanf("%s",msg);
for(i=0;i<strlen(msg);i++)
{
c[i]=msg[i]-65;
printf("%d ",c[i]);
}
for(i=0;i<3;i++)
{
t=0;
for(j=0;j<3;j++)
{
t=t+(a[i][j]*c[j]);
}
d[i]=t%26;
}
printf("\nEncrypted Cipher Text :");
for(i=0;i<3;i++)
printf(" %c",d[i]+65);
for(i=0;i<3;i++)
{
t=0;
for(j=0;j<3;j++)
{
t=t+(b[i][j]*d[j]);
}
c[i]=t%26;
}
printf("\nDecrypted Cipher Text :");
for(i=0;i<3;i++)
printf(" %c",c[i]+65);
return 0;
}
/*Output */

/* Program to implement Modified Ceaser Cipher */


#include<stdio.h>
int strlen(char a[])
{
int i=0;
for(i=0;a[i]!='\0';i++);
return i;
}
int main()
{
char text[20];
int i,key;
printf("Enter String\n");
scanf("%s",text);
printf("Enter key\n");
scanf("%d",&key);
for(i=0;i<strlen(text);i++)
{
text[i]=text[i]+key;
if(text[i]>122)
{
text[i]=text[i]-26;
}
else if(text[i]>90 && text[i]<97)
text[i]=text[i]-26;
else if(text[i]>57 && text[i]<65)
text[i]=text[i]-10;
else
{}
}
printf("Cipher Text is %s",text);
for(i=0;i<strlen(text);i++)
{
text[i]=text[i]-key;
if(text[i]<97)
text[i]+=26;
}
printf("Decrypted Text : %s",text);
return 0;
}
/* Output */

/* Program to implement Columnar Cipher */


#include<stdio.h>
#include<string.h>
void main()
{
char k[20]={'\0'},pt[30]={'\0'},key[30]={'\0'},mat[10][10]={'\0'},ct[30]={'\0'};
int i=0,j=0,no[10]={0},ctr,z,c=0,a=0;
//clrscr();
printf("Enter the plaintext:\n");
gets(pt);
printf("Enter the key:\n");
gets(k);
strcpy(key,k);
strcat(k,pt);
strcpy(pt,k);
printf("Enter the ordering starting from 0 to the (keylength-1):\n");
for(i=0;i<strlen(key);i++)
{
scanf("%d",&no[i]);
}
j=0;
ctr=j;
printf("The matrix is:\n");
for(i=0;i<strlen(pt);i++)
{
for(j=j;ctr<strlen(key);j++)
{
if(pt[j]!='\0')
{
mat[i][ctr]=pt[j];
printf("%c",mat[i][ctr]);
ctr++;
}
else
{
goto out;
}
}
ctr=0;
if(i==0) printf("\n");
printf("\n");
}
out:
printf("\n\nThe Encrypted text is:\n");
for(i=0;i<strlen(key);i++)
{
for(j=0;j<strlen(key);j++)

{
if(no[i]==j)
{
for(z=1;z<10;z++)
{
if(isalpha(mat[z][j]))
{
printf("%c",mat[z][j]);
ct[a]=mat[z][j];
a++;
}
}
}
}
}
printf("\n\nDecrypted Ciphertext is:\n");
for(a=0;a<30;a++)
{
if(isalpha(ct[a]) || isspace(ct[a]))
c++;
}
for(i=1;i<c;i++)
{
for(j=0;j<strlen(key);j++)
{
if(isalpha(mat[i][j]))
printf("%c",mat[i][j]);
else
break;
}
}
// getch();
}
/* Output */

/* Program to implement Rail Fence Cipher */


#include<stdio.h>
#include<string.h>
int main()
{
char text[50],t[50],c[50];
int k=0,l,i,j,count=0;
printf("Enter Plain Text:\n");
gets(text);
for(i=0,j=0;text[i]!='\0';i++)
if(text[i]!=' '){
t[j]=text[i];j++;
}
t[j]='\0';
l=strlen(t);
i=count;j=0;
while(count<=3)
{
if(i>=l)
i=++count;
c[j]=t[i];
/*if(k==4)
{
k=0;yy
c[j]=' ';
j++;
}*/
//printf("%c",c[i]);
i=i+4;
j++;k++;
}
c[l]='\0';
printf("\nCipher text %s %d",c,l);
printf("\nLenght is %d %d ",strlen(c),strlen(t));
puts(c);
return 0;
}
/* Output */

/* Program to implement Vignar Cipher */


#include<stdio.h>
int strlen(char a[])
{
int i=0;
for(i=0;a[i]!='\0';i++);
return i;
}
int main()
{
char text[20];
int i,j;
int k[]={4,8,6,9,6,6,1,9};
printf("Enter String\n");
scanf("%s",text);
for(i=0,j=0;i<strlen(text);i++,j++)
{
if(j==8)
j=0;
text[i]=text[i]+k[j];
if(text[i]>122)
{
text[i]=text[i]-26;
}
else if(text[i]>90 && text[i]<97)
text[i]=text[i]-26;
else if(text[i]>57 && text[i]<65)
text[i]=text[i]-10;
else
{}
}
printf("Cipher Text is %s",text);
for(i=0,j=0;i<strlen(text);i++,j++)
{
if(j==8)
j=0;
text[i]=text[i]-k[j];
if(text[i]<97)
text[i]+=26;
}
printf("\nDecrypt Text is %s",text);
return 0;
}
/* Output */

Anda mungkin juga menyukai