0
votes

Trying to solve Cryptopals Challenge 10 where have to CBC decrypt a text file against "YELLOW SUBMARINE" with an IV of all ASCII 0 (\x00\x00\x00 &c). Link to the text file is following:

http://cryptopals.com/static/challenge-data/10.txt

I have followed the algorithm CBC uses by taking cipher text, decrypting(using ECB decryption) and then taking xor with Initialization Vector for first block and ciphertext(i-1) for subsequent blocks. However for some not-understandable reason I am not getting a readable decryption. I just see some weird characters when I print after decryption:

from Crypto.Cipher import AES
key ='YELLOW SUBMARINE'
iv = "%00%00%00"*32
iv = iv.replace('%',r'\x')

#XOR-ing function
def xor_strings(a, b):
    return "".join(chr(ord(a1) ^ ord(b1)) for a1, b1 in zip(a, b))

#Taking input file and converting it into a single string 
file = open('10.txt','r')
data = file.read()
block = 128

obj = AES.new(key, AES.MODE_ECB)

def split_len(string, size):
    return [string[i:i+size] for i in range(0, len(string), size)]

mylist = split_len(data,block)


decrypted = ""
for i in range (0,len(mylist)):
     mystr = obj.decrypt(mylist[i])
     if (i==0):
          decrypted = decrypted + xor_strings(mystr,iv)
     else:
          decrypted = decrypted + xor_strings(mystr, mylist[i-1])
 print decrypted

What might be the problem here ?

1
The ASCII '0' character is hex 30 not hex 00.rossum
@rossum tried that too. gives same results. Also the questions said: IV of all ASCII 0 (\x00\x00\x00 &c)Manahil

1 Answers

1
votes

The iv needs to be 16 zero bytes (the question is not clearly worded here when it says “ASCII 0”):

iv = "\x00" * 16

You need to base64 decode the file before decrypting it:

from base64 import b64decode
#...

file = open('10.txt','r')
data = file.read()
data = b64decode(data)

Finally your block size needs to be in bytes for this code to work, not bits:

block = 16