Exemple #1
0
def decrypt(cipher, shift=None):
    ciphers = []
    if shift == None:
        for i in range(0, 26):
            ciphers.append(decrypt(cipher, i))
        scores = score_fitness(ciphers)
        return ciphers[scores.index(max(scores))]
    else:
        return ''.join([chr(((ord(x)-65-shift) % 26) + 65) for x in cipher.upper() if x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'])
Exemple #2
0
def decrypt(cipher, shift=None):
    ciphers = []
    if shift == None:
        for i in range(0, 26):
            ciphers.append(decrypt(cipher, i))
        scores = score_fitness(ciphers)
        return ciphers[scores.index(max(scores))]
    else:
        return ''.join([
            chr(((ord(x) - 65 - shift) % 26) + 65) for x in cipher.upper()
            if x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        ])
Exemple #3
0
def decrypt(cipher, a=None, b=None):
    #if unknown a and b, make best guess. More accurate with
    #longer cipher texts
    if a==None or b==None:
        canidates = []
        for i in [1,3,5,7,11,13,17,19,23]:
            for j in range(1,25):
                canidates.append(decrypt(cipher,i,j))
        scores = score_fitness(canidates)
        return canidates[scores.index(max(scores))]
    else:
        a_i = 0
        for i in range(1,25):
            if (a * i) % 26 == 1:
                a_i = i
                break
        return ''.join([chr( ( (a_i * (ord(x) - 65 - b) % 26) + 65) ) for x in cipher.upper() if x.isalpha()])
Exemple #4
0
def decrypt(cipher, a=None, b=None):
    #if unknown a and b, make best guess. More accurate with
    #longer cipher texts
    if a == None or b == None:
        canidates = []
        for i in [1, 3, 5, 7, 11, 13, 17, 19, 23]:
            for j in range(1, 25):
                canidates.append(decrypt(cipher, i, j))
        scores = score_fitness(canidates)
        return canidates[scores.index(max(scores))]
    else:
        a_i = 0
        for i in range(1, 25):
            if (a * i) % 26 == 1:
                a_i = i
                break
        return ''.join([
            chr(((a_i * (ord(x) - 65 - b) % 26) + 65)) for x in cipher.upper()
            if x.isalpha()
        ])
Exemple #5
0
def decrypt(cipher, n=None):
    if n == None:
        ciphers = []
        for i in range(1, len(cipher)):
            ciphers.append(decrypt(cipher, i))
        scores = score_fitness(ciphers)
        return ciphers[scores.index(max(scores))]
    else:
        cipherlen = len(cipher)
        parts = [""] * n
        count = cycle(range(0, n) + range(1, n - 1)[::-1])
        for i in cipher:
            index = count.next()
            parts[index] += "x"
        for i in range(0, n):
            parts[i] = cipher[: len(parts[i])]
            cipher = cipher[len(parts[i]) :]
        count = cycle(range(0, n) + range(1, n - 1)[::-1])
        plaintext = ""
        for i in range(0, cipherlen):
            index = count.next()
            plaintext += parts[index][0]
            parts[index] = parts[index][1:]
        return plaintext
Exemple #6
0
def decrypt(cipher, n=None):
    if n == None:
        ciphers = []
        for i in range(1, len(cipher)):
            ciphers.append(decrypt(cipher, i))
        scores = score_fitness(ciphers)
        return ciphers[scores.index(max(scores))]
    else:
        cipherlen = len(cipher)
        parts = [''] * n
        count = cycle(range(0, n) + range(1, n - 1)[::-1])
        for i in cipher:
            index = count.next()
            parts[index] += 'x'
        for i in range(0, n):
            parts[i] = cipher[:len(parts[i])]
            cipher = cipher[len(parts[i]):]
        count = cycle(range(0, n) + range(1, n - 1)[::-1])
        plaintext = ''
        for i in range(0, cipherlen):
            index = count.next()
            plaintext += parts[index][0]
            parts[index] = parts[index][1:]
        return plaintext