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 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include<fstream.h>
#include<conio.h>
#include<string.h>
class encoder
{char ch,ch1,filename[25], key;
public:
encoder(int);
void encrypt()
{cout<<">>>"; //for show
ch1=ch^key;
cout<<"\b\b\b"; //for show
}
void decrypt()
{char a,b,c,d,e; // temporary variables
cout<<">>>"; //for show
a=ch|key;
b=ch^key;
c=a|(~b);
d=b&c;
e=c&(~d);
ch1=~e;
cout<<"\b\b\b"; //for show
}
};
encoder::encoder(int x)
{ cout<<"\nEnter the file name: ";
cin.getline(filename,25); // collecting file name
int l=strlen(filename);
if(filename[l-4]!='.')
strcat(filename,".txt");
fstream file;
file.open(filename,ios::in|ios::out|ios::nocreate); //opening file
if(!file)
cout<<"\nFile not found!!";
else
{
cout<<"Enter KEY:";
key=getche(); //reading encryption key
cout<<endl;
int p,g; // debug
while(1)
{ if(file.eof()!=0) //eof detection
break;
file>>ch; //reading from file
p=file.tellp(); // debug
g=file.tellg(); // debug
cout<<endl<<"g="<<g<<"\tp="<<p; // debug
file.seekp(-1,ios::cur); //correction term
//file.seekg(-,ios::cur); alternate correction term
if(x==1)
encrypt();
else
decrypt();
file<<ch1; //writing to file
cout<<"\tch="<<ch<<"\tch1="<<ch1; // debug
}
cout<<"\nOperation Sucessful!!";
file.close();
}
}
int choice()
{ // User Menu
char x='e';
clrscr();
cout<<"--------------------\n";
cout<<" Encoder 1.22\n";
cout<<"--------------------\n";
cout<<"Encrypt->e\nDecrypt->d\n";
x=getch();
clrscr();
cout<<"--------------------\n";
cout<<" Encoder 1.22\n";
cout<<"--------------------\n";
if(x=='e'||x=='E')
{cout<<" Encryption Mode\n--------------------\n";
encoder e(1);
}
else
{cout<<" Decryption Mode\n--------------------\n";
encoder e(0);
}
return 0;
}
int main()
{char y='y';
do
{choice();
cout<<"\nContinue ?"; // to repeat or not to repeat
y=getche();
}while(y=='y'||y=='Y');
return 0;
}
| |