HERE IS SIMPLE XOR PROGRAM

WHEN I TRY TO COMPARE TWO SAME STRINGS ENCRYPTED WITH SAME KEY THEN IT SAYS COMPARISON IS WRONG

HERE IS CODE

#include<conio.h>
#include<iostream.h>
#include<windows.h>
#include<string.h>

using namespace std;

int main()
{
  char st[100];
   char key[100];
  
    cout<<"ENter string ::";
    cin>>st;
    cout<<"\n";
    
    cout<<"Enter key::";
    cin>>key;
    cout<<"\n";
    
    int i;
    
    int j;
    int kk=strlen(st);
    for(i=0,j=0;i<=kk;i++)
    {
    st[i]=st[i]^key[j++];
    
   if(j==strlen(key))
    {
    j=0;
}


}

cout<<"ENCRYPTED TEXT::"<<st<<"\n";

char cm[100];
cout<<"Enter for compare::";
cin>>cm;
cout<<"\n";
int k1=strlen(cm);
    for(i=0,j=0;i<=k1;i++)
    {
    cm[i]=cm[i]^key[j++];
    
   if(j==strlen(key))
    {
    j=0;
}


}


cout<<cm;
if(strcmp(cm,st)==0)
{
                    cout<<"OK";
                    }
                    
                    else
                    {
                        cout<<"\nWROMG\n";
                        }
                        getch();
return 0;
}

tell me where i am wrong

By SHOUTING at us. Please don't shout again.

Are the lengths of cm and st the same?
Output each char in each string as an integer and see if they look the same.

yeah they are same even same value

tell me where i am wrong

The flaws are in your encryption routines, here's a fix:

int kk=strlen(cm);
for(i=0,j=0;i<kk;i++) /* changed <= to < */
{
  cm[i]=cm[i]^key[j++];
  if(j==strlen(key))
  {
    j=0;
  }
}

Do the same for your second encryption routine and your program will work the way you intended.

Why does this work?
In your program as it now stands your encryption routine encodes the nul-terminator, which isn't a good thing. If you know what you do, then there's nothing wrong with overwriting a nul-terminator*, but if you do, then you should keep that into account for future operations on that character array, you can't use standard library functions anymore to operate on the string, because such routines make use of the nul-terminator sentinel value to recognize the end of a character array. Since you've overwritten your nul-terminator with a character of your encryption key**, your program will invoke undefined behavior at the time you display your encrypted string using cout. So the logical conclusion is that you should loop one less, that is why you should change: for(i=0,j=0;i<=kk;i++) to: for(i=0,j=0;i<kk;i++) .

* - However I think it's not very useful anyway.
** - [I]nul-byte[/I] [B]XOR[/B] [I]character of encryption key[/I] will reveal that character of the encryption key.

yeah they are same even same value

Prove it because I don't believe it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.