Thread: Xor Encryption Issue

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Xor Encryption Issue

    Is this a valid xor encryption function?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void xorEncrypt(string& str, string key)
    {
        int strLen = str.length() - 1;
    
        for(int x=0; x <= strLen; x++)
        {
            str[x] = str[x]^key[x];
        }
    }
    
    int main()
    {
        string key = "key";
        string text="This my text...";
    
        xorEncrypt(text,key);
        cout<<text<<endl;
    
        xorEncrypt(text,key);
        cout<<text<<endl;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'd do something like this:
    Code:
    void xorEncrypt(string& str, const string& key)
    {
        int strLen = str.length();
        int keyLen = key.length();
    
        for(int x=0; x < strLen; x++)
        {
            str[x] = str[x] ^ key[x % keyLen];
        }
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    str[x] = str[x] ^ key[x % keyLen];
    I dont understand. How does the modulo operator help?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What happens to your code, when the key is shorter than the text?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Works fine, but when the key string is one or two characters long my CPU beeps.
    Why does that happen?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void xorEncrypt(string& str, const string& key)
    {
        int strLen = str.length();
        int keyLen = key.length();
    
        for(int x=0; x < strLen; x++)
        {
            str[x] = str[x] ^ key[x % keyLen];
        }
    }
    
    
    int main()
    {
        string key = "ke";
        string text="Hi, my name is John Williams.";
    
        xorEncrypt(text,key);
        cout<<text<<endl;
    
        xorEncrypt(text,key);
        cout<<text<<endl;
    }
    edit: I found out that what cause my CPU to beep is not actually the fact that the key string is 1 or 2 characters long.... it's something else i cant seem to figure out,,, could it a problem in my cpu? (btw i'm using codeblock for compiling)


    edit2: i'm positive that whats causing my cpu to beep is exactly in this line:
    str[x] = str[x] ^ key[x % keyLen];
    Because when i make it a comment, the CPU doesnt beep
    Last edited by dhuan; 11-16-2010 at 11:59 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It beeps because the result of the encryption contains non-printable characters, including the bell character.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Learning Issue
    By TimHarrington in forum C Programming
    Replies: 48
    Last Post: 09-06-2010, 04:33 AM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM