Thread: Xor encryptiong clarification

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    Xor encryptiong clarification

    For this simple XOR encryption, I want to XOR each byte so I used a char type for plain text and key. This is how I think the logic is applied, but the ciphertext comes out as the club character which is not part of ASCII, but Unicode.

    First my code:
    Code:
    int main(int argc, char* argv[])//NB:SYNTACTIC SUGAR <=> char** argv[] (but former more natural to understand: an array of ptrs of a single byte (char) )
    {
    	char plain_txt[] = "AB";
    	char key[] = "DEGH";
    		
    	cout << plain_txt << endl;//output (plain txt): AB
    	
    	for ( size_t i = 0; i < 2; ++i )//I thought I'm XORing each byte
    		plain_txt[i] ^= key[i];
    	
    	cout << plain_txt[0] << " , " << plain_txt[1] << endl;//output: ♣ , ♣
    	
    	cout << plain_txt << endl;//output: ♣
    	
    	return 0;
    }
    plain_txt[0] = 'A' = 65 (base-10 ASCII) = 01000001
    plain_txt[1] = 'B' = 66 = 01000010
    key[0] = 'D' = 68 = 01000100
    key[1] = 'E' = 01000101
    and key[2] = 'G', key[3] = 'H' are not used based on the for loop

    so original plain_txt[0] becomes:
    01000001 ('A')
    XOR
    01000100 ('D')
    =========
    00000101 or 5 (base-10)

    and original plain_txt[1] becomes:
    01000010 ('B')
    XOR
    01000101 ('E')
    =========
    00000111 or 7 (base-10)

    so I'd thought if I output: plain_txt I'd get the ASCII sum of: 5 + 10 which is unprintable(form feed). But why does it give me: ♣?

    And also is character arrays special so compilers know to print as a string I mean which I noticed when I
    Code:
    cout << plain_txt << endl;
    Code:
    char plain_txt[] = "AB"
    cout << plain_txt << endl;//output:AB
    //versus
    string s[] = "FED";
    cout << s << endl;//0xAFFEE33 so mem address or: &s[0]

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    What shows up in the command prompt can depend on the active code page, the fonts used on the screen, in the document, and probably how you started cmd.exe.

    for example:
    Code:
    C:\Users\Josh2>chcp
    Active code page: 437
    
    C:\Users\Josh2>echo ♣•
    ♣
    C:\Users\Josh2>
    This code page: OEM 437

    Many fonts repurpose the old nonprintable characters from ASCII too.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    But in terms of logic, is what I'm doing correct, so key[2] and key[3] are not used in the encryption since I have: 'A' XOR 'D' (which is plain_txt[0] XOR key[0]) and 'B' XOR 'E' (which is plain_txt[1] XOR key[1]).

    And how about the other part in question(it's related since I did cout << plain_txt << ) and AB was shown but not if I had a string array.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you're asking if the XOR operator works, the answer is yes. Since the characters generated in the cipher text will be non-printable you can output them as integers instead to confirm. Use casting.

    I'm not sure what you're asking in the other question, and I think other people can answer it better than me. Different types, different operators. Nothing "special".

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    Actually, I'm asking if how I applied XOR using my example in OP is correct. And for the other question, I just didn't know when yo output array name, it outputs each char whereas the string array, if I output the array name, it gives address for 0th element. But former question more important than latter.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Concerning your second issue, I think what you want is string s = "foo" and not string s[] = "foo", that's why you got an address printed instead of the actual string, because you declare an array of string instead of a simple string. A std::string is conceptually equivalent to an array of characters (char[]), but it adds an object wrapping.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your logic is correct and your program is working perfectly.

    The encoded string is a char(5) and a char(7). char(5) is displayed as a club character (in the windows console) and char(7) is "displayed" as a beep. That's what I get, a club character and a beep. So if you're getting two clubs in a row then something has actually gone wrong, although I don't know what since I ran your code withot any changes.

    Anyway, do the encoding loop again and you'll see that you get your original string back. That's the magic of XOR encryption.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    I'm going to have to look into what whiteflags says about chcp/active code page output stuff and as for my OP, I looked @ ASCII table and ASCII 5 is ENQ (enquiry) so when I outputted plaint_txt[0] (which was 'A' XOR 'D' I got 5 (base-10). Here is the ASCII table: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Hi oogabooga, I copied the code in OP, but I'm still getting two clubs trying to print each element in plain_txt. Could it be different compilers, I'm using MinGW that came w/ Code::Blocks IDE.
    Last edited by monkey_c_monkey; 08-11-2012 at 04:00 PM.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The low ascii codes are ancient and not many are in general use anymore. Only the ones that the C language has escape-codes for are in general use, I guess. Anyway, 5 (or ENQ) is not used so it has been delegated the club character on that code page.

    I was mistaken to say I hadn't modified your code. I had commented out the last cout. So now with the original code I get a club, a comma, a BEEP sound, and then a club on the next line and another BEEP (not two clubs on the same line).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some clarification please.
    By ShadowBoss in forum C Programming
    Replies: 3
    Last Post: 11-19-2011, 02:23 PM
  2. Need a clarification here
    By bithub in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 12-27-2004, 01:06 AM
  3. need clarification on something...
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-08-2004, 09:45 AM
  4. FAQ Clarification
    By Ness in forum C Programming
    Replies: 13
    Last Post: 06-10-2003, 04:37 PM