C++ Implementation of Substitution Cipher

#include<iostream>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
int main()
{
FILE *in, *op;
int key;
int t1;
char c;
int ope;

in = fopen("anip.txt","r");
op = fopen("anop.txt","w");

cout<<"\n What do you want to perform:";
cout<<"\n 1. Encryption";
cout<<"\n 2. Decryption";
cin>>ope;

cout<<"\n Enter Key:";
cin>>key;

if(ope == 1)
	//Encryption
	while(!feof(in))
	{
		c = fgetc(in);

		if(c==-1)
		break;
		if(isupper(c))
		{
		t1 = (int) c-(int) 'A';
		t1 = (t1+key)%26;
		t1 = t1 + (int) 'A';
		fputc((char) t1,op);
		}
		else if(islower(c))
		{
		t1 = (int) c-(int) 'a';
		t1 = (t1+key)%26;
		t1 = t1 + (int) 'a';
		fputc((char) t1,op);
		}
		else{
		fputc(c,op);
		}
	}
else
	//decryption
	while(!feof(in))
	{
		c = fgetc(in);

		if(c==-1)
		break;
		if(isupper(c))
		{
		t1 = (int) c-(int) 'A';
		t1 = (t1-key+26)%26;
		t1 = t1 + (int) 'A';
		fputc((char) t1,op);
		}
		else if(islower(c))
		{
		t1 = (int) c-(int) 'a';
		t1 = (t1-key+26)%26;
		t1 = t1 + (int) 'a';
		fputc((char) t1,op);
		}
		else{
		fputc(c,op);
		}
	}

//ending
fclose(in);
fclose(op);

cout<<"\n Done";
getch();
return 0;
}

Input Output for Encryption

Program:

Input File:

Output File:


Input Output for Decryption

Program:

Input File:

Output File:

Leave a Reply

Your email address will not be published. Required fields are marked *