[C++] XOR Encryption

encryption
programming
c++

#1

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:


Crypto Algs (Part 3.0): XOR
(The C# Dude) #2

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?


#3

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


(Not a N00b, but still learning) #4

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


#5

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.


#6

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


#7

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).


#8

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


#9

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


(Not a N00b, but still learning) #10

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


#11

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


(Awn) #12

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


(N3k00n3) #13

I made an asm version.


(John Demco) #14

Nicely written! Thanks for sharing


(system) #15

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