Xor Encryption Issue

This is a discussion on Xor Encryption Issue within the C++ Programming forums, part of the General Programming Boards category; Is this a valid xor encryption function? Code: #include <iostream> #include <string> using namespace std; void xorEncrypt(string& str, string key) ...

  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,399
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 10: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, 08: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

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