2017-02-17 2 views
0

Essayer de résoudre les Cryptopals Challenge 10 où CBC doit déchiffrer un fichier texte contre "YELLOW SUBMARINE" avec un IV de tout ASCII 0 (\ x00 \ x00 \ x00 & c). Lien vers le fichier texte est le suivant:Décryptage CBC en utilisant la fonction ECB

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

J'ai suivi l'algorithme CBC utilise en prenant un texte chiffré, le décryptage (en utilisant le déchiffrement BCE) et de prendre XOR avec vecteur d'initialisation pour le premier bloc et cryptogramme (i -1) pour les blocs suivants. Cependant, pour une raison non compréhensible, je ne reçois pas un décryptage lisible. Je vois juste des caractères étranges quand j'imprime après le déchiffrement:

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 

Quel pourrait être le problème ici?

+0

Le caractère '0' ASCII est hexadécimal 30 pas hexadécimal 00. – rossum

+0

@rossum l'a essayé aussi. donne les mêmes résultats. Aussi les questions dites: IV de tout ASCII 0 (\ x00 \ x00 \ x00 & c) – Manahil

Répondre

1

Les iv a besoin d'être 16 zéro octets (la question n'est pas clairement formulée ici quand il dit « ASCII 0 »):

iv = "\x00" * 16 

Vous devez base64 décoder le fichier avant décryptant:

from base64 import b64decode 
#... 

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

Enfin, votre taille de bloc doit être en octets pour que ce code fonctionne, pas de bits:

block = 16