XOR? XOR!!!! Beginner (Python)

I finally have the luxury of time to learn new things, in which I decided to beef up some of my cryptography knowledge. A basic cryptography category in which certain CTFs present is a classic XOR challenge.

Being a person with ZERO knowledge in cryptography, some research were needed. So in a nutshell, XOR is the operation of taking 2 bits, putting in through the XOR operation or also known as ^.

Some XOR rules:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

Since XOR works at the bit level, in order to encryt a message like ATTACK AT DAWN, the message needs to be in bit representation before taking each for a spin in the XOR operator.

As this was just a simple practice, I decided to expeirment using a single key. Take the example of the following.

Message is ATTACK AT DAWN
Key chosen is H

So to encrypt that message using XOR, which individual character have to be XOR-ed by the key, therefore a conersion was needed. Thankfully, converting from string to bits was easy, by using the following bin(ord('a')), then putting it into a list (Using python over here).

Since some of the XOR cipher text is not readable as text, I encoded the cipher text in base64 to make it "transportable". In which, a decode is needed before passing the message through the decryption process.

Here is the code sample code for encryption and decryption:

import base64
#Encryption

cipher_bits = []

input_string = 'ATTACK AT DAWN'
key = 'H'
print "XOR Key: " + key
key_bin = (bin(ord(key)))[2:]
key_left_zeros = '0' * (8-len(key_bin))
new_key_bin = key_left_zeros + key_bin
# print new_key_bin

str_bits = []
for ch in input_string:
	tmp_bit = (bin(ord(ch)))[2:]
	bit_left_zeros = '0' * (8 - len(tmp_bit))
	new_bit = bit_left_zeros + tmp_bit
	str_bits.append(list(new_bit))


# print str_bits

temp_bits = ''
for i in range(len(str_bits)):
	for j in range(len(str_bits[i])):
		temp_bits += str(int(str_bits[i][j]) ^ int(new_key_bin[j]))
	cipher_bits.append(list(temp_bits))
	temp_bits = ''

# print cipher_bits

tmp_bits_holder = []
for i in range(len(cipher_bits)):
	tmp_bits_holder.append(''.join(cipher_bits[i]))

# print tmp_bits_holder

tmp_cipher_text = ''
tmp_ch_holder = ''
for i in range(len(tmp_bits_holder)):
	tmp_ch_holder = chr(int(tmp_bits_holder[i],2))
	tmp_cipher_text += tmp_ch_holder
	tmp_ch_holder = ''
new_cipher_text = base64.b64encode(tmp_cipher_text)
print "XOR RAW Encrypted" + tmp_cipher_text
print "XOR RAW Encrypted Encoded: " + new_cipher_text




#Decryption
cipher_text = base64.b64decode(new_cipher_text)
cipher_str_bits = []
for ch in cipher_text:
	c_tmp_bit = (bin(ord(ch)))[2:]
	c_bit_left_zeros = '0' * (8 - len(c_tmp_bit))
	c_new_bit = c_bit_left_zeros + c_tmp_bit
	cipher_str_bits.append(list(c_new_bit))

# print cipher_str_bits

c_temp_bits = ''
message_bits = []
for i in range(len(cipher_str_bits)):
	for j in range(len(cipher_str_bits[i])):
		c_temp_bits += str(int(cipher_str_bits[i][j]) ^ int(new_key_bin[j]))
	message_bits.append(list(c_temp_bits))
	c_temp_bits = ''
# print message_bits
tmp_message_bolder = []
for bits in message_bits:
	tmp_message_bolder.append(''.join(bits))

decrypted_message = ''
for i in tmp_message_bolder:
	decrypted_message += chr(int(i,2))

print "XOR Decrypted: " + decrypted_message