[C++] XOR Encryption

Heading

Hello 0x00’ers. I thought it would be cool to make a post specific to XOR encryption.

At the moment my main focus is getting the basics of python down but, that doesn’t mean I don’t have the time for a little fun with c++ :stuck_out_tongue: I’m not the most knowledgeable person so I apologize if I make a mistake or leave something out.

So xor(Exclusive Or) is a logic gate that says “The output can only be True if one input is True and one input False.”

Examples
1 0 = 1
True True = false
0 0 = 0
False True = True

Now the cool thing is, this can be used for encryption. You simply input a string and xor it by a single char(using a for loop) or by a string of the same size as the one being encrypted.

Example
[‘A’, ‘R’, ‘P’, ‘A’, ‘n’, ‘e’, ‘t’, ‘\0’]

we have a string of ASCII characters(‘ARPAnet’) that we then convert to binary

01000001 01010010 01010000 01000001 01101110 01100101 01110100

now lets xor that string by another 7 character string such as racecar
using the logic we talked about above
[‘r’, ‘a’, ‘c’, ‘e’, ‘c’, ‘a’, ‘r’, ‘\0’]

01110010 01100001 01100011 01100101 01100011 01100001 01110010

ARPAnet
01000001 01010010 01010000 01000001 01101110 01100101 01110100
racecar xor
01110010 01100001 01100011 01100101 01100011 01100001 01110010


and we get

00110011 00110011 00110011 00100100 00001101 00000100 00000110

which can then be translated back into it’s ASCII text form to get “333$”.

Notice how there are not 7 characters like there were in ARPAnet and racecar. This is because some bytes do not “translate” into ASCII characters after they got xor’d. This makes decryption hard if you just have the encrypted string instead of the actual encrypted bytes.

To decrypt you would xor the flipped bits using the same key.

encrypted binary
00110011 00110011 00110011 00100100 00001101 00000100 00000110
racecar xor
01110010 01100001 01100011 01100101 01100011 01100001 01110010


and you would get your old message back

01000001 01010010 01010000 01000001 01101110 01100101 01110100

I made a little script to automate this. Unfortunately it doesn’t like spaces so you will need to use another character such as a dash or underscore.

the script will be linked here

Have fun and stay frosty :night_with_stars:

10 Likes

Great first article! I think XOR-encryption should be fundamental knowledge, so this post helps beginners a lot ;). I only recommend you to leave out using namespace std next time, because it’s bad practice :slight_smile:. See explanation here.
Anyway, it looks like you’ll post good stuff. What are your next ideas you may write about?

5 Likes

As stated @TheDoctor, XOR-encryption is a fundamental knowledge to whom pretends to be a computer security specialist ! Often use to encrypt malware, via a fixed or sliding key, … XOR is a basic component of encryption and can be found in plenty of algorithms. However, I think that it is important to note, even if it seems obvious, that XOR should not be used,for security concern, in standalone.

Anyway, nice article, summarizing efficiently the XOR concept.

Best,
Nitrax

4 Likes

Hi there,

Maybe I get you wrong, but shouldn’t this

1 0 = 1
True True = false
0 0 = 0
False True = True

be this?

1 0 = 1
True False = True
0 0 = 0
False False = False

I think this table would fit in better here:

Input	        Output
A       B
0	0	0
0	1	1
1	0	1
1	1	0

Best regards, SmartOne

4 Likes

Thank you…I’m just going to write about everything that comes to mind :stuck_out_tongue: I might make a c++ basics when I learn c++. I think trying to teach something you’re learning makes you understand it better.

1 Like

Hi, I’t could be that but i’ts basically saying the same thing, just in a different order :smile_cat:

Great article mate! It’s nice that you decided to jump from Python to C++. Many say it’ll make many other languages easier to learn (perhaps as it’s a bit hard to learn).

1 Like

Ah and I recall a pretty good C/C++ tut on NB by @anon79434934, if you’d want to go check that out.

1 Like

Thank you, and yup. I think it really helps in the long run if you know at least 2 different languages

I think one of us is missing something :grin: In the first example, in my opinion the text doesn’t match the boolean values:

1 0 = 1
True True = false

Here, you say the first bool is true, which I agree with. The second Boolean, 0, is false but in the text you say it’s true. Also, the result of that operation should be true, shouldn’t it? :slight_smile:

Please tell me if I get something totally wrong here!
Best, SmartOne

1 Like

Hi, it’s just a logic gate. It doesn’t really matter what the values are. It’s only true if one input is on and the other is off, true false, 1 0, etc.

Thank you,
-Fust3rCluck

2 Likes

Nice, I remember when I first learned about XOR. Seems like magic right?

This blew my mind back in the day: https://sinister.ly/Thread-How-To-Write-Nonalphanumeric-PHP-Backdoors

2 Likes

I made an asm version.

2 Likes

Nicely written! Thanks for sharing

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.