-
Xor Problem
I have written a simple program which encrypts text using XOR encryption.. I have put the source below...
The problem is the encryption and decryption works well for small strings some time.. But some time it does not work for anything.. Some time partial strings are decrypted and some time encryptiuon does not happen in full etc.. Please have a look...
The function exor take the input from the varaible e and also takes the password or key as the argument..... and encrypts data.
The function dxor takes the encrypted text and key or password as argements and decrypts the data
Code:
# include <iostream.h>
# include <conio.h>
# include <string.h>
char text[3600];
char xtext[3600];
char dtext[3600];
int exor(char text[],char pass[])
{
int l,p;
l=strlen(text);
p=strlen(pass);
int m=0;
for(int i=0;i<l;i++)
{
if(m>p-1)
m=0;
xtext[i]=text[i]^pass[m];
m=m+1;
}
return 0;
}
int dxor(char text[],char pass[])
{
int l,p;
l=strlen(text);
p=strlen(pass);
int m=0;
for(int i=0;i<l;i++)
{
if(m>p-1)
m=0;
dtext[i]=text[i]^pass[m];
m=m+1;
}
return 0;
}
int main()
{
clrscr();
char e[3600];
int t=0;
for(int i=0;i<3600;i++)
{
e[i]=getche();
t=t+1;
if(e[i]==13)
break;
}
t=t-1;
for(int y=t;y<3600;y++)
{
e[y]=( NULL );
}
cout<<"\n\n";
exor(e,"this is the passowrd key");
cout<<xtext<<"\n";
dxor(xtext,"this is the password key");
cout<<dtext;
return 0;
}
SOme one help please
Thanx
-
You need to to handle zero results ('A' xor 'A' = 0). These zeros will mess up your string length.
-
You're using two different keys:
Code:
exor(e,"this is the passowrd key"); <--- password misspelled here
cout<<xtext<<"\n";
dxor(xtext,"this is the password key");
-
I corrected the key but it still does not work....
And handling of 0
wel
a xor a =0
so is
0 xor a = a
So it does not make a difference..... Some one help please..
-
In both functions, put (just after the strlen calls)
cout << "len1" << l << " len2" << p << endl;
If they're not the same, you have an a^a problem to fix
-
The problem is, char(0), a NUL, is a special character in the language. It is used to indicate the end of a char[]. You will have to do something about that, or the program will think it is done with the string when is hits a NUL, which is any time you do something like 'A'^'A'.