Xor Problem

This is a discussion on Xor Problem within the C++ Programming forums, part of the General Programming Boards category; I have written a simple program which encrypts text using XOR encryption.. I have put the source below... The problem ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Question 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

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    You need to to handle zero results ('A' xor 'A' = 0). These zeros will mess up your string length.
    I doubt, therefore I might not be

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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");

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    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..

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    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'.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21