Esempio n. 1
0
def block_hamming_distance(x, y):

    x_bin = rep.DataRep()
    y_bin = rep.DataRep()

    x_bin.setFromIntList(x)
    y_bin.setFromIntList(y)

    x_binstr = x_bin.toBinString()
    y_binstr = y_bin.toBinString()
    diff = 0

    for (x_i, y_i) in zip(x_binstr, y_binstr):

        if x_i != y_i:

            diff += 1

    return diff
Esempio n. 2
0
def decode(ciphertext):

    s = rep.DataRep()
    s.setFromHexString(ciphertext)
    record = dict()

    for ch in range(256):

        decrypt = s.singleByteXOR(ch)
        record[ch] = score_freqs(decrypt)

    return max(record.keys(), key=lambda x: record[x])
Esempio n. 3
0
def decode(text):

    s = rep.DataRep()
    s.setFromHexString(text)
    winner = 0
    plaintext = None

    for ch in range(256):

        decrypt = s.singleByteXOR(ch)
        score = score_freqs(decrypt)
        if score > winner:

            winner = score
            plaintext = decrypt

    return plaintext, winner
Esempio n. 4
0
def XORdecode(text):

    s = rep.DataRep()
    s.setFromIntList(text)
    winner = 0
    record = dict()

    for ch in range(256):

        decrypt = s.singleByteXOR(ch)
        score = score_freqs(decrypt)
        record[ch] = score
        if score > winner:

            winner = score
            xor_key = ch

    return xor_key
Esempio n. 5
0
 def __init__(self):
     
     self.data = rep.DataRep()
     self.key = rep.DataRep()
     self.round_keys = []
Esempio n. 6
0
import byte_rep as rep

plaintext = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
k = [ord(x) for x in "ICE"]
s = rep.DataRep()
s.setFromString(plaintext)
s.setFromIntList(s.multiByteXOR(k))
print(s.toHexString())
Esempio n. 7
0
def entropy_vec(v):

    return sum([entropy(x) for x in v])


def to_prob_dist(v):

    dist = []
    for elem in set(v):

        dist.append(v.count(elem) / len(v))

    return dist


#read file and decode
with open('c8.txt') as _file:

    record = dict()
    for file_data in _file.readlines():

        data = rep.DataRep()
        data.setFromHexString(file_data.rstrip())

        # turn into probability distribution and record entropy
        record[file_data] = entropy_vec(to_prob_dist(data.toIntList()))

    # print line with lowest entropy
    print(min(record.keys(), key=lambda x: record[x]))