How To Xor Bytes In Python
Class 3: Binary, XOR, and the One-time Pad
Overview
The post-obit exercises will help you undersand the binary backside all of the characters we apply. You will also learn about a very important binary operation, XOR. We'll also look at an encryption scheme that uses XOR, the Onetime Pad, and we'll perform some attacks against some poorly encrypted strings.
Binary
Everything represented on your computer is but stored equally $.25, 1s and 0s. Let'southward have a wait at some binary numbers:
These are what the numbers actually are; everything else is simply a lie. Even letters are just binary:
And to convert back to ASCII:
XOR
With binary operators nosotros tin can simply deal with the decimal representations, but it's important to empathize what'southward happening under the hood.
Hither'south a truth table for the binary XOR (sectional-or) operation:
| A | B | A XOR B |
|---|---|---|
| 1 | 1 | 0 |
| ane | 0 | one |
| 0 | one | ane |
| 0 | 0 | 0 |
And here'southward how to XOR 2 numbers in Python ('^' is the binary XOR operator in Python). Pay attenion to what is happening at the binary level:
Without coding it, what is 1100 ^ 0101? What well-nigh 8 ^ eleven?
Now, how exercise we XOR ii strings? Well, we merely take to convert them to numbers first! We saw in the previous exercise how to practice this, but there is an easier mode using the pycrypto Python module. We tin also catechumen from the number back to the bytes:
Convert "UDCTF" to an integer and XOR information technology with the number 30249328405. What is the event in ASCII?
Now, you lot accept everything you demand to write your own XOR function that takes in two strings and returns the XOR of them. If you want to attempt this, it would be a great coding practice, just I'm going to show you a quick and easy-to-use XOR function from pwntools:
The pwntools xor function will bicycle the shorter cord over the longer one.
Transformations
Then far, we've seen a few means to correspond strings:
- ASCII
- Integer
- Hex Digest
And we'll add one more:
- Byte Array
A byte assortment of a string is just an assortment of the ordinal values of the characters. For case, they byte array of "abc" is [ord('a'), ord('b'), ord('c')].
It's very of import to be able to convert between these formats:
# These are the 12 one-line transformations that are most useful to crypto CTF problems #THIS IS THE PYTHON 2 VERSION #in every CTF or crypto session this line is my offset include from Crypto.Util.number import * #Our four formats are plaintext (ASCII), hex digest, integer, and byte array pt = "this is plaintext" #1) ASCII to HEX hexdigest = pt.encode('hex') #two) HEX to ASCII hexdigest.decode('hex') #iii) ASCII to Integer int_pt = bytes_to_long(pt) #4) Integer to ASCII long_to_bytes(int_pt) #5) Integer to hex format(int_pt, 'ten') #vi) hex to Integer int(hexdigest, 16) #7) ASCII to byte array pt_as_bytes = map(ord, pt) #8) byte assortment to ASCII "".join(map(chr, pt_as_bytes)) #nine) byte array to hex "".join(map(lambda x: hex(ten)[2:], pt_as_bytes)) #10) hex to byte assortment [int(hexdigest[i:i + 2], 16) for i in range(0, len(hexdigest), 2)] #xi) byte array to int bytes_to_long("".bring together(map(chr, pt_as_bytes))) #12) int to byte array map(ord, long_to_bytes(int_pt)) Challenge: Numbers two
Complete the Numbers 2 challenge using everything y'all've learned and then far this class.
One-Time Pad
Why XOR is Awesome
XOR is slap-up for cryptography for 2 reasons:
1. XOR preserves randomness
If I XOR a patterned cord of bits 1111000011110000 with a random cord of bits 1001110101000110, I become something that looks random: 110110110110110. Why is this?
For each fleck in my random cord, if there is a l/l chance of it being 0 or 1, and then the output also has a 50/50 chance of being 0 or 1. If 1 is my input so one ^ i is 0 and 1 ^ 0 is 1. If my input is a 0 then 0 ^ 1 is one and 0 ^ 0 is 0. Either mode I've got the aforementioned random distribution equally what I XOR with.
2. A XOR A is 0
This may not seem that heady at commencement, but this basically allows us to opposite an XOR functioning.
(A XOR B) XOR A is B
If we XOR A with B, and and so XOR the result of that with A once again, the A's "cancel out" (since A XOR A is 0), and we're left with B.
Verify this for yourself. Accept the XOR("hello", "world"). Now take the effect of that and XOR it with "globe" once more.
Encryption Scheme
Encryption schemes are oftentimes described by iii algorithms, GEN the cardinal generation algorithm, ENC the encryption algorithm which consumes a central and a message and returns a ciphertext, and DEC a decryption algorithm which consumes a ciphertext and a key and returns the plaintext bulletin.
GEN: choose a random key uniformly from {0,1}ℓ (the prepare of binary strings of length ℓ)
ENC: given m∈{0,ane}ℓ and k∈{0,1}ℓ then output is c:=k XOR yard
DEC: given one thousand∈{0,1}ℓ and c∈{0,i}ℓ, the output message is m:=yard XOR c
In words: take a random sequence of 0s and 1s as the undercover cardinal. XOR that fundamental with your bulletin to get the ciphertext. To recover the message, XOR the ciphertext and the key.
This is the perfect encryption scheme...theoretically. If used incorrectly, say if the central is not random, or the length of the key is less than the length of the message, or the cardinal is used multiple times, everything breaks.
Approaches
Inquire yourself the post-obit questions while solving the problems:
- What exercise I have? Practise I have the plaintext, the ciphertext, or the primal?
- What do I demand? How can I get from what I have to what I need? What operations can I perform?
- Is in that location annihilation that stands out or seems "wrong"?
- Is at that place annihilation I can infer (similar, does the plaintext incorporate UDCTF)? i.east. GUESSING
- Is the search space small enough to brute force?
Challenges
- The Key - Exercise you know how XOR works?
- The Lock - What if the primal is used more than one time?
- All The Single Ladies
- XOR ROX
- XORientation
- haXOR
Source: https://github.com/zelinsky/CTF-Course/blob/master/Classes/3.md

0 Response to "How To Xor Bytes In Python"
Post a Comment