Showing posts with label Cryptography. Show all posts
Showing posts with label Cryptography. Show all posts

Mar 18, 2010

Monoalphabatic cipher encryption and decryption

//MONOALPHABETIC ENCRYPTION AND DECRYPTION
//copyright jayeshaminnew@gmail.com

//ENCRYPTION OF MONOALPHABAIC CIPHER
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<ctype.h>
void main()
{
FILE *f1,*f2,*f3;
int i,j=0,k,asc[50],l,temp,temp1,flag=0;
char plain[50],cipher[50],key[50],m;
clrscr();
f1=fopen("plain.txt","r");
while((l=getc(f1))!=EOF)
{
plain[j++]=l;
}
plain[j]=NULL;
fclose(f1);
j=0;
f2=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
{
key[j++]=l;
}
key[j]=NULL;
fclose(f2);
cout<<"plain text is\n"<<plain;
cout<<"key is \n"<<key;
//logic
k=0;
for(i=0;i<strlen(plain);i++)
{
asc[k++]=plain[i]-97;
}
k=0;
for(i=0;i<strlen(plain);i++)
{
if(asc[i]>=0 && asc[i]<=25)
cipher[k++]=key[asc[i]];
else
cipher[k++]=plain[asc[i]+97];
}
cout<<"\n"<<"cipher text is\n";
cipher[k]=NULL;
cout<<cipher;

f3=fopen("cipher.txt","w");
for(i=0;i<strlen(cipher);i++)
putc(cipher[i],f2);
getch();
}


//DECRYPTION OF MONOALPHABATIC CIPHER

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<ctype.h>
void main()
{
FILE *f1,*f2,*f3;
int i,j=0,k,asc[50],l,temp,temp1,flag=0;
char plain[50],cipher[50],key[50],m;
clrscr();
f1=fopen("cipher.txt","r");
while((l=getc(f1))!=EOF)
{
cipher[j++]=l;
}
cipher[j]=NULL;
fclose(f1);
j=0;
f2=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
{
key[j++]=l;
}
key[j]=NULL;
fclose(f2);
cout<<"cipher text is\n"<<cipher;
cout<<"\nkey is \n"<<key;
//logic
k=0;
for(i=0;i<strlen(cipher);i++)
{
for(j=0;j<strlen(key);j++)
{
if(cipher[i]==key[j])
break;
}
plain[k++]=j+97;
}
cout<<"\n"<<"plain text is\n";
plain[k]=NULL;
cout<<plain;

f3=fopen("plain.txt","w");
for(i=0;i<strlen(plain);i++)
putc(plain[i],f2);
getch();
}

Caesar cipher encryption and decryption

//CAESAR CIPHER ENCRYPTION
//copyright jayeshaminnew@gmail.com
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
//declaring variables
FILE *f1,*f2;
int i,j=0,k,l,key,temp,temp1,flag=0;
char plain[50],cipher[50],m;
clrscr();
//reading plain text
f1=fopen("plain.txt","r");
while((l=getc(f1))!=EOF)
plain[j++]=l;
plain[j]=NULL;
fclose(f1);
printf("your plain text is:%s\n",plain);
scanf("%d",&key,printf("enter key:"));
for(i=0;i<j;i++)
{
temp=plain[i];
temp1=temp;
temp1+=key;
if(temp1>122 && islower(temp))
temp1-=26;
else if(temp1>90 && isupper(temp))
temp1-=26;
else if(!((temp>=97 && temp<=122) || (temp>=65 && temp<=90)))
{
flag=1;
}
if(flag==0)
cipher[i]=temp1;
else
cipher[i]=temp;
flag=0;
}
cipher[i]=NULL;
//writing cipher text into file
f2=fopen("cipher.txt","w");
for(i=0;i<strlen(cipher);i++)
putc(cipher[i],f2);
fclose(f2);
printf("your cipher text is:%s",cipher);
getch();
}

//CAESAR CIPHER DECRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
void main()
{
FILE *f1,*f2;
int i,j=0,k,l,key,temp,temp1,flag=0;
char plain[50],cipher[50],meaning;
clrscr();
f1=fopen("cipher.txt","r");
while((l=getc(f1))!=EOF)
cipher[j++]=l;
cipher[j]=NULL;
fclose(f1);
j=0;
for(i=0;i<26;i++)
{
for(j=0;j<strlen(cipher);j++)
{
temp=cipher[j];
temp1=temp;
temp1-=i;
if(temp1<97 && islower(temp))
temp1+=26;
else if(temp1<65 && isupper(temp))
temp1+=26;
else if(!((temp>=97 && temp<=122) || (temp>=65 && temp<=90)))
{
flag=1;
}
else
{}
if(flag==0)
plain[j]=temp1;
else
plain[j]=temp;
flag=0;
}
plain[j]=NULL;
printf("Message:%s",plain);
printf("\nis it meaning ful msg?? Y/N?\n");
scanf("%s",&meaning);
if(meaning=='y' || meaning=='Y')
{
printf("your plain text is: %s",plain);
getch();
exit(0);
}
}
getch();
}

Polyalphabatic cipher encryption and decryption

//POLYALPHABATIC CIPHER ENCRYPTION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int start=98,i,temp=97,j,k,m,n,l;
char plain[100],key[100],cipher[100],arr[27][27],ch1,ch2;
FILE *f1,*f2;
clrscr();
//code for matrix generation
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
{
if(temp>122 )
temp=32;
arr[i][j]=temp++;
if(temp==33)
temp=97;
}
temp=start++;
}
//printing matrix
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
printf("%c",arr[i][j]);
printf("\n");
}
i=0;
f1=fopen("plain.txt","r");
while((l=getc(f1))!=EOF)
plain[i++]=l;
plain[i]=NULL;
fclose(f1);
i=0;
f1=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
key[i++]=l;
key[i]=NULL;
fclose(f1);
temp=0;
printf("\nplain text: %s\n",plain);
for(i=strlen(key);i<strlen(plain);i++)
{
if(temp>strlen(key))
temp=0;
key[i]=key[temp++];
}
key[i]=NULL;
printf("key:        %s\n",key);
for(i=0;i<strlen(plain);i++)
{
if(plain[i]!=32)
m=plain[i]-97;
else
m=26;
n=key[i]-97;
cipher[i]=arr[m][n];
}
cipher[i]=NULL;
f1=fopen("cipher.txt","w");
for(i=0;i<strlen(cipher);i++)
putc(cipher[i],f1);
fclose(f1);
printf("cipher text:%s",cipher);
getch();


}

//POLYALPHABATIC CIPHER DECRYPTION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int start=98,i,temp=97,j,k,m,n,l;
char plain[100],key[100],cipher[100],arr[27][27],ch1,ch2;
FILE *f1,*f2;
clrscr();
//code for matrix generation
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
{
if(temp>122 )
temp=32;
arr[i][j]=temp++;
if(temp==33)
temp=97;
}
temp=start++;
}
//printing matrix
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
printf("%c",arr[i][j]);
printf("\n");
}
i=0;
f1=fopen("cipher.txt","r");
while((l=getc(f1))!=EOF)
cipher[i++]=l;
cipher[i]=NULL;
fclose(f1);
i=0;
f1=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
key[i++]=l;
key[i]=NULL;
fclose(f1);
temp=0;
printf("\ncipher text:%s\n",cipher);
for(i=strlen(key);i<strlen(cipher);i++)
{
if(temp>strlen(key))
temp=0;
key[i]=key[temp++];
}
key[i]=NULL;
k=0;
printf("key:        %s\n",key);
for(i=0;i<strlen(cipher);i++)
{
for(j=0;j<27;j++)
{
if(cipher[k]==arr[key[i]-97][j])
break;
}
plain[k++]=arr[0][j];
}
plain[k]=NULL;
f1=fopen("plain.txt","w");
for(i=0;i<strlen(plain);i++)
putc(plain[i],f1);
fclose(f1);
printf("plain text: %s\n",plain);
getch();
}

Mar 10, 2010

Playfair cipher encyption and decryption

//PLAYFAIR CIPHER ENCRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
int i,m=0,flag=0,j,n,row1,row2,col1,col2;
char ch,d,mat[5][5],key[10],k[10],c[30],a[10],ct[10],pt[10];
clrscr();
fp=fopen("key.txt","r");
fscanf(fp,"%s",key);
fclose(fp);
printf("The key is: %s",key);
//finding new key removing duplicates...
for(i=0;i<strlen(key);)
{
if(key[i]=='j')
key[i]=105;
flag=0;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
flag=1;
}
if(flag==1)
i++;
else
k[m++]=key[i++];  //new array for key
}
k[m]=NULL;
printf("\nThe new key is  :\n%s",k);//printing new key
//store key in matrix
m=0;
for(ch=97;ch<=122;)
{
flag=0;
for(i=0;i<strlen(k);i++)
{
if(k[i]==ch)
flag=1;
}
if(flag==1)
ch++;
else if(ch=='j')
ch++;
else
{
c[m++]=ch;
ch++;
}
}
c[m]=NULL;
//construction of 2-D 5*5 matrix
m=0;
n=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
mat[i][j]=k[m++];
if(m>strlen(k))
mat[i][j]=c[n++];
}
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
printf("\t %c",mat[i][j]);
printf("\n");
}
//retrieve plain-text from file
i=0;
fp=fopen("plain.txt","r");
fscanf(fp,"%s",pt);
fclose(fp);
printf("\nThe plain-text is: %s",pt);
//seperation of digram
j=0;
printf("\nAfter Processing Digrams: ");
for(i=0;i<strlen(pt);)
{        if(pt[i]==pt[i+1])
{
a[j++]=pt[i];
a[j++]='x';
i+=1;
}
else
{
a[j++]=pt[i];
a[j++]=pt[i+1];
i+=2;
}
}
a[j]='\0';
printf("%s",a);
printf("\n");
for(i=0;i<strlen(a);i+=2)//Loop for conversion to cipher text
{
for(j=0;j<5;j++)// matrix row
{
for(m=0;m<5;m++)//matrix column
{
//Find position of 1st element of Digram in matrix
if(a[i]==mat[j][m])
{
row1=j;
col1=m;
}
//Find position of 2nd element of Digram in matrix
if(a[i+1]==mat[j][m])
{
row2=j;
col2=m;
}
//Condition For both elements in same row of matrix
if(row1==row2)
{
ct[i]=mat[row1][(col1+1)%5];
ct[i+1]=mat[row1][(col2+1)%5];
}
//Condition For both elements in same column of matrix
else if(col1==col2)
{
ct[i]=mat[(row1+1)%5][col1];
ct[i+1]=mat[(row2+1)%5][col2];
}
else
{
ct[i]=mat[row1][col2];
ct[i+1]=mat[row2][col1];
}
}
}
}
ct[i]='\0';
fp=fopen("cipher.txt","w");
printf("\nCipher Text: ");
//store cipher text in a file
for(i=0;i<strlen(ct);i++)
putc(ct[i],fp);
fclose(fp);
printf("%s",ct);      //print cipher text
getch();
}


//PLAYFAIR CIPHER DECRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
int i,m=0,flag=0,j,n,row1,row2,col1,col2;
char ch,d,mat[5][5],key[10],k[10],c[30],a[10],ct[10],pt[10];
clrscr();
fp=fopen("key.txt","r");
fscanf(fp,"%s",key);
fclose(fp);
printf("The key is: %s",key);
for(i=0;i<strlen(key);)
{
if(key[i]=='j')
{
key[i]='i';
}
flag=0;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
flag=1;
}

if(flag==1)
i++;
else
k[m++]=key[i++];  //new array for key
}
k[m]=NULL;
// printing new key after elimination of duplicates
printf("\nThe new key is  :\n%s",k);
//seperation of key letters from a-z & later stored in an array
m=0;
for(ch=97;ch<=122;)
{
flag=0;
for(i=0;i<strlen(k);i++)
{
if(k[i]==ch)
flag=1;
}
if(flag==1)
ch++;
else if(ch=='j')
ch++;
else
{
c[m++]=ch;
ch++;
}
}
c[m]=NULL;
//construction of 2-D 5*5 matrix
m=0;
n=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
mat[i][j]=k[m++];
if(m>strlen(k))
mat[i][j]=c[n++];
}
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("\t %c",mat[i][j]);
}
printf("\n");
}

//retrieve cipher-text from file

i=0;
fp=fopen("cipher.txt","r");
fscanf(fp,"%s",ct);
fclose(fp);
printf("\nThe Cipher-text is: %s",ct);



printf("\n");
for(i=0;i<strlen(ct);i+=2)//conversion to plain text
{
for(j=0;j<5;j++) //matrix row
{
for(m=0;m<5;m++)//matrix column
{
//Find position of 1st element of Digram in matrix
if(ct[i]==mat[j][m])
{
row1=j;
col1=m;
}
//Find position of 2nd element of Digram in matrix
if(ct[i+1]==mat[j][m])
{
row2=j;
col2=m;
}
//Condition For both elements in same row of matrix
if(row1==row2)
{
pt[i]=mat[row1][(col1-1+5)%5];
pt[i+1]=mat[row1][(col2-1+5)%5];
}
//Condition For both elements in same column of matrix
else if(col1==col2)
{
pt[i]=mat[(row1-1+5)%5][col1];
pt[i+1]=mat[(row2-1+5)%5][col2];
}
else
{
pt[i]=mat[row1][col2];
pt[i+1]=mat[row2][col1];
}
}
}
}
pt[i]='\0';
fp=fopen("plain.txt","w");
printf("\nPlain Text: ");
//store plain text in a file
for(i=0;i<strlen(pt);i++)
  putc(pt[i],fp);
fclose(fp);
printf("%s",pt);      //print plain text
fclose(fp);
getch();
}