Status
Not open for further replies.
I wrote an encryption/decryption prog in c++ using the xor operator(im just a n00b)

suppose my input is

my name is dheeraj kumar.

then my encrypted form is

㵧≥㙁刁Ԁ幅㸵ጕȕൽĜᰄ鉏

when i decrypt it the output is

my name is dheeraj kumar.ÿš

i dont know how i got the extra 2 characters in the end, i even debugged it but to no avail, so someone help me out here.
i did this as a school project, and i need to submit it on friday, so someone please help fast.

here is my code:

Code:
#include <fstream.h>
#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>
//#include "C:\Turbo\TC\Project\Code\3dSP.cpp"
//#include "C:\Turbo\TC\Project\Mouse.cpp"

class program
{

 // V A R I A B L E   D E C L A R A T I O N

 // S t r e a m s

 ifstream FileRead;
 ofstream FileWrite;

 // V a r i a b l e s

 char Input;
 char TempFileName;
 char TempChar;
 char *TempText;
 char *Password;
 char *FinalText;
 char *filread;
 char *filwrite;
 int x;


 // F U N C T I O N S
 public:

 void EncFile()
 {
   clrscr();

   Input=' ';
   TempChar=' ';
   TempText=" ";
   Password=" ";
   FinalText=" ";
   filread=" ";
   filwrite=" ";
   int sum=0;
   char *output= " ";
   char *TempFileName=" ";
   // File to be Encrypted of Decrypted
   cout << "File to be encrypted/decrypted? : ";
   gets(TempFileName);
   FileRead.open(TempFileName, ios::in | ios::binary);

   // Output of the file
   cout << "Output file? : ";
   gets(output);
   FileWrite.open(output, ios::out | ios::binary);

   // Password
   cout << "What's the Password? (max.8) : ";
   gets(Password);
   for(int i=0;Password[i]!='\0';i++)
      sum+=Password[i];
   sum%=100;
   cout<<endl<<"Processing, please wait......." << endl;

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ Password[x]; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output
    x++; // next char in password

    if (Password[x] == '\0') // if char is blank go back to first char
       x=0;

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ sum; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ Password[x]; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output
    x++; // next char in password

    if (Password[x] == '\0') // if char is blank go back to first char
       x=0;

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ sum; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ sum; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ Password[x]; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output
    x++; // next char in password

    if (Password[x] == '\0') // if char is blank go back to first char
       x=0;

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ sum; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output

    TempChar = 0;
    Input = 0;
   }

   FileRead.seekg(0);
   FileWrite.seekp(0);

   for(i=0; !FileRead.eof(); i++)
   {
    //float percent = (i/filsize)*100;
    //gotoxy(0,5);
    //cout<<"File Encryption Process : "<<percent<<" %"<<endl;
    Input = FileRead.get(); // Get character
    TempChar = Input ^ Password[x]; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output
    x++; // next char in password

    if (Password[x] == '\0') // if char is blank go back to first char
       x=0;

    TempChar = 0;
    Input = 0;
   }

   FileRead.close();
   FileWrite.close();
   cout<<endl <<"Encryption Process Completed"<<endl;
   cout<<"Press any key to continue...";
   getch();

 }

 void menu()
 {
  clrscr();
  for(int i=0;i<479;i++)
  {  if(i%8==0)
     outtextxy(0,i,"º                                                                              º");
  }
//outtextxy(0,0,"É������������������������������������������������������������������������������»");
  //outtextxy(0,8,"º                                                                              º");
  getch();
 }
};

void main()
{
 program prog;
 //int gdriver = DETECT, gmode, errorcode;
 //initgraph(&gdriver, &gmode, "");
 prog.EncFile();
 //prog.menu();

}


basically what i am doing is:

get a password from the user

xor the whole text with the password
find the sum of the letters' ascii number and xor the whole text with that
xor with password
xor with sum
xor with sum
with password
sum again
the password again

repeat the process to decrypt it.

and please omit the commented code.

thanks
Dheeraj Kumar
 

LordDJ

Broken In
already a thread for C++ queries *www.thinkdigit.com/forum/viewtopic.php?t=32408

Are you a C coder migrating to C++? Cos i looks like it. No constructor. Also looks like a big program with all graphics and co. So why don't you allocate memory for variables rather than leaving them on the stack. This leads to leaks, big time when you write complicated programs, esp projects.
 

sakumar79

Technomancer
Its been a while since I did any debugging or used C++, so I cannot give a direct solution to the problem... However, I will give a suggestion that usually helps me debug programs. Use WATCHES and BREAKPOINTS. If you do not use an IDE that has facility of using watches and breakpoints, use a lot of print statements at strategic locations to ensure you get info on the flow of commands... Then, try with a simple text to encrypt and simple password (single characters each) and track how the variables change... Manually work out your logic and verify at each step that the calculated value and printed value of watched variables is the same....

Arun

EDIT: Ive snipped my comment on Mavericks post following explanation from owner of post... My apologies...
 

puja399

In the zone
@dheeraj_kumar

After watching ur code a couple of times, I couldn't help commenting on it. ur code is too confusing/ repeatative and I'm unable to correct it. But what what I figured from the explanation u appended to ur post, the encryption routine, as u call it, but I think there is no encryption after all!!! After XORing the string with 2 numbers repeatatively, the effects cancels out one another and at the end the original string comes back. Test this with a calculator!!!
say, Original number = 144, Sum=100 and pass=99, then
144 XOR 99=243
243 XOR 100=151
151 XOR 99=244
244 XOR 100=144
144 XOR 100=244
244 XOR 99 =151
151 XOR 100=243
243 XOR 99=144

So u see, ur original number comes back and there is no encryption at all!!! The error u get is certainly from miscoding somewhere.

U said u didn't migrate from C, and I belive u. C programmers don't write such *#$@% looking code. But I figure u have some java background perhaps.
 
@Puja399

What you said is completely correct. In fact, i have been stumped by this since when i wrote this prog....I was expecting to get back the string i entered while a garbage string popped on my face...lolz....then decided to run the prog again on the so-called encrypted file and got back the string i entered at first...with 2 extra characters...so i dont know whose fault it is....logically, its wrong, but i decided to call it a "simple xor encryption" for my project sake....in fact, no-one at my skool understood what i exactly did in my proj but were convinced mine was the coolest of all....maybe its just my born ability to write oh-so-complicated and illogical programs which never fail to amaze people xD
 

LordDJ

Broken In
Dude what I suggest is you rewrite the encryption part with a clear (read CLEAR) mind! I didn't go thru the code cos I found it too confusing. But Puja pretty much sums it;)
 

aadipa

Padawan
don't know what logic you wanted to use but i guess that my modified code will do the same work which you wanted, without that 2 extra chars

Code:
#include <fstream.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

class program
{

 // V A R I A B L E   D E C L A R A T I O N

 // S t r e a m s
 ifstream FileRead;
 ofstream FileWrite;

 // V a r i a b l e s
 char Input;
 char TempFileName;
 char TempChar;
 char *TempText;
 char *Password;
 char *FinalText;
 char *filread;
 char *filwrite;
 int x;


 // F U N C T I O N S
 public:

 void EncFile()
 {
   clrscr();

   Input=' ';
   TempChar=' ';
   TempText=" ";
   Password=" ";
   FinalText=" ";
   filread=" ";
   filwrite=" ";
   int sum=0;
   char *output= " ";
   char *TempFileName=" ";

   // File to be Encrypted of Decrypted
   cout << "File to be encrypted/decrypted? : ";
   gets(TempFileName);
   FileRead.open(TempFileName, ios::in | ios::binary);

   // Output of the file
   cout << "Output file? : ";
   gets(output);
   FileWrite.open(output, ios::out | ios::binary);

   // Password
   cout << "What's the Password? (max.8) : ";
   gets(Password);
   int i=0;
   while(Password[i]!='\0')
      sum+=Password[i++];

   sum%=100;
   cout<<endl<<"Processing, please wait......." << endl;

   FileRead.seekg(0);
   FileWrite.seekp(0);

   Input = FileRead.get(); // Get character
   while(!FileRead.eof())  {
    TempChar = (Input ^ Password[x]) ^ sum; // Xor Character
    FileWrite << TempChar; // place encrypted/decrypted char in output
    x++; // next char in password

    if (Password[x] == '\0') // if char is blank go back to first char
       x=0;

    TempChar = 0;
    Input = FileRead.get(); // Get character
   }

   FileRead.close();
   FileWrite.close();
   cout<<endl <<"Encryption Process Completed"<<endl;
   cout<<"Press any key to continue...";
   getch();
 }
};

void main()
{
 program prog;
 prog.EncFile();
}

EDIT: Note that I am reading the input file before checking for end of file in the hile loop which in your case was source of those extra chars.
 
Status
Not open for further replies.
Top Bottom