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();
}

No comments:

Post a Comment