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]