Home > Project Euler, python > Project euler #59

Project euler #59

31 Dicembre 2012
Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.

A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function  is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.

For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.

Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.

Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher1.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.

Analisi:
Ricorrendo a Google e più precisamente all’analisi crittografica, all’analisi della frequenza dei caratteri ed all’analisi della frequenza, è risultato che in un testo di medie dimensioni, il carattere più ripetuto è lo spazio.
Il carattere spazio è il doppio più frequente del carattere ‘e’ (più frequente tra le lettere in lingua inglese).
Quindi del testo cifrato, sapendo che la chiave di crittazione è di 3 caratteri, trovando i 3 caratteri cifrati più ricorrenti, si dovrebbero in teoria ottenere i caratteri spazio, sui quali sono state utilizzate ciclicamente le tre lettere della chiave di crittazione.
Sapendo che lo spazio rappresenta il numero 32, tramite XOR risaliamo a quale lettera della chiave di crittazione ci stiamo riferendo.
Ottenute le 3 lettere, si fanno i tentativi con le possibili parole di tre lettere inglesi, finchè non otteniamo un testo decrittato plausibile.

file_in = open(r'/home/banco/Python/cipher1.txt')
seq = [int(n) for n in file_in.readlines()[0].rstrip().split(',')]
file_in.close()
occs = set(seq)

qty = {}
for occ in occs:
    qty[occ] = seq.count(occ)
# ricerco i 3 caratteri più ripetuti
for i in range(3):
    value = max(qty.values())
    key = [k for k in qty.keys() if qty[k]==value][0]
    print 'chr: %s - count: %s - decrypted: %s' %(key, value, chr(key ^ 32))
    qty.pop(key)

otteniamo che i tre caratteri (presumibilmente sempre ‘spazio’) più ricorrenti sono:

>>> 
chr: 79 - count: 86 - decrypted: o
chr: 68 - count: 77 - decrypted: d
chr: 71 - count: 70 - decrypted: g

non ci sono moltepossibilità in inglese: ‘dog’ e ‘god’

God ispira di più… proviamo a decrittare:

# decritto il messaggio
i = 0
enc = ''
while i < len(seq):
    if i % 3 == 0:
        c = chr(seq[i]^ord('g'))
    elif (i + 2) % 3 == 0:
        c = chr(seq[i]^ord('o'))
    else:
        c = chr(seq[i]^ord('d'))
    enc += c
    i += 1
print enc

risultato:

>>>
"(The Gospel of John, chapter 1) 1 In the beginning the Word already existed.
....

sembra tanto lui.
Ora non resta che calcolare la somma dei singoli caratteri:

sum(ord(c) for c in enc)

Nota:
Il mio approccio è stato inizialmente di tentare le parole di tre lettere della lingua inglese, decisamente un approccio di Brute Force, che difficilmente sarebbe rimasto sotto al minuto.
Grazie Google.

Categorie:Project Euler, python Tag:
I commenti sono chiusi.