def cryptanalysis_block_rotate(ciphertext, arguments=[0, 0, 0]):

    dict_list = utilities.load_dictionary(DICT_FILE)

    if (arguments[0] > 0
            and arguments[1] > 0) and (arguments[0]
                                       == arguments[1]) and arguments[2] == 0:
        for i in range(arguments[1]):

            text = d_block_rotate(ciphertext, (arguments[1], i))

            if utilities.is_plaintext(text, dict_list) == True:
                return (arguments[1], i), text

    if (arguments[0] == 0 and arguments[1] == 0) and (arguments[2] > 0):

        for i in range(arguments[0], arguments[1]):

            text = d_block_rotate(ciphertext, (i, arguments[2]))
            if utilities.is_plaintext(text, dict_list) == True:
                return (i, arguments[2]), text

    if (arguments[0] > 0 or arguments[1] > 0) and arguments[2] == 0:

        if arguments[1] == 0:
            arguments[1] = BLOCK_MAX_SIZE
        if arguments[0] == 0:
            arguments[0] = 1

        for i in range(arguments[0], arguments[1]):

            for j in range(arguments[1]):

                text = d_block_rotate(ciphertext, (i, j))
                if utilities.is_plaintext(text, dict_list) == True:

                    return (i, j), text

    if (arguments[0] == 0 and arguments[1] == 0 and arguments[2] == 0):
        if arguments[1] == 0:
            arguments[1] = BLOCK_MAX_SIZE
        if arguments[0] == 0:
            arguments[0] = 1

        for i in range(arguments[0], arguments[1]):

            for j in range(arguments[1]):

                text = d_block_rotate(ciphertext, (i, j))
                if utilities.is_plaintext(text, dict_list) == True:
                    return (i, j), text

    return '', ''
Esempio n. 2
0
def cryptanalysis_xshift(ciphertext):
    #your code here
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    atbash = alphabet[::-1]

    listOfData = []
    combinations = [(alphabet + alphabet.upper()), (atbash + atbash.upper()),
                    (alphabet + atbash.upper()), (atbash + alphabet.upper())]

    for combo in combinations:
        i = 0
        while (i < 26):
            plain_test = d_xshift(ciphertext, tuple((combo, i)))
            chi_value = utilities.get_chiSquared(plain_test)
            listOfData.append(tuple((plain_test, chi_value, i, combo)))
            i += 1
    listOfData = sorted(listOfData, key=lambda tup: tup[1])
    for value in listOfData:
        if (utilities.is_plaintext(value[0], 'engmix.txt', 0.9)):
            key = tuple((value[3], value[2]))
            plaintext = value[0]
            #print(plaintext)
            return key, plaintext

    return key, plaintext
Esempio n. 3
0
def cryptanalysis_xshift(ciphertext):
    #your code here
    key = 0
    plaintext = ''
    regLower = 'abcdefghijklmnopqrstuvwxyz'
    regUpper = regLower.upper()
    atbashLower = regLower[::-1]
    atbashUpper = atbashLower.upper()
    possibilities = [regLower+regUpper, regLower+atbashUpper, atbashLower+regUpper, atbashLower+atbashUpper]
    listTuples = []
    for possible in possibilities:
        for i in range(26):
            decr = d_xshift(ciphertext, tuple((str(possible), i)))
            chi = utilities.get_chiSquared(decr)
            listTuples.append(tuple((decr, chi, i, possible)))
    
    listTuple = sorted(listTuples, key=lambda tup: tup[1])

    for item in listTuple:
        if(utilities.is_plaintext(item[0], 'engmix.txt', 0.8)):
            key = tuple((item[3], item[2])) 
            plaintext = item[0]
            return key, plaintext

    return key, plaintext
Esempio n. 4
0
def cryptanalysis_xshift(ciphertext):
    # your code here
    wordList = utilities.text_to_words(ciphertext)
    single = list()

    for word in wordList:
        if len(word) == 1 and word not in single:
            single.append(word)

    shiftStringList = utilities.get_shiftStringList()

    for s in single:
        for shiftString in shiftStringList:
            now_c = shiftString.index(s)
            ori_c = shiftString.index('I')
            dis = now_c - ori_c if now_c > ori_c else now_c - ori_c + 52
            plaintext = d_xshift(ciphertext, (shiftString, dis))

            if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90):
                utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q2.txt')
                return (shiftString, dis), plaintext

    key = ('', 0)
    plaintext = ''

    return key, plaintext
Esempio n. 5
0
def cryptanalysis_xcrypt(ciphertext):
    # your code here
    plaintext = ''
    i = 0
    for i in range(1,501):
        plain = d_xcrypt(ciphertext, i)
        is_plain = utilities.is_plaintext(plain, "engmix.txt", 0.8)
        if(is_plain == True):
            return i, plain
    return i, plaintext
Esempio n. 6
0
def cryptanalysis_vigenere(ciphertext):
    # yoru code here
    FriedmanL = utilities.getKeyL_friedman(ciphertext)
    ShiftL = utilities.getKeyL_shift(ciphertext)
    non_alpha = utilities.get_nonalpha(ciphertext)

    if ShiftL != 1:
        # try every possible size
        start = min(FriedmanL, ShiftL)
        if start >= 3:
            start -= 1
        end = max(FriedmanL, ShiftL) + 2
        modified = utilities.remove_nonalpha(ciphertext)
        for size in range(start, end):
            plaintext = ''
            shiftList = []
            blocks = utilities.text_to_blocks(modified, size)
            baskets = utilities.blocks_to_baskets(blocks)

            for element in baskets:
                # get the list of chi_squared for every shift
                chiList = [round(utilities.get_chiSquared(utilities.d_shift(element, (i, 'l'))), 4)
                           for i in range(26)]

                shiftList.append(chiList.index(min(chiList)))

            for i in range(len(baskets)):
                baskets[i] = utilities.d_shift(baskets[i], (shiftList[i], 'l'))

            result = utilities.blocks_to_baskets(baskets)
            for e in result:
                plaintext += e

            plaintext = utilities.insert_nonalpha(plaintext, non_alpha)

            if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90):
                plain = utilities.remove_nonalpha(plaintext)[:size]
                key = ''
                v_square = utilities.get_vigenereSquare()
                counter = 0
                for pChar in plain:
                    pIndex = v_square[0].index(pChar)
                    kIndex = v_square[pIndex].index(blocks[0][counter])
                    counter += 1
                    key += v_square[0][kIndex]

                utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q4.txt')
                return key, plaintext

    key = 0
    plaintext = ''

    return key, plaintext
def _restore_word_playfair(word, dict_list):

    new_word = word
    x_count = 0
    i = 0
    x_char = 'x'
    if new_word.isupper() == True:
        x_char = 'X'
    if utilities.is_plaintext(new_word, dict_list, 1) == True:
        return new_word
    else:
        while i < len(new_word):
            if new_word[i] == x_char and i != 0 and x_count < 2:
                # print('test1')
                new_word = new_word[:i] + new_word[i - 1] + new_word[i + 1:]
                if utilities.is_plaintext(word, dict_list, 1) == True:
                    return new_word
                x_count += 1
            i += 1

    if utilities.is_plaintext(new_word, dict_list, 1) == False:
        j = 0
        x_count = 0
        x_skip = 1
        new_word = word
        while j < len(new_word):
            if new_word[j] == x_char and j != 0 and x_count < 2:
                # print('test2')
                if x_skip == 0:
                    x_skip -= 1
                    new_word = new_word[:j] + new_word[j - 1] + new_word[j +
                                                                         1:]
                    if utilities.is_plaintext(word, dict_list, 1) == True:
                        return new_word
                else:
                    x_skip -= 1
                x_count += 1
            j += 1

    if utilities.is_plaintext(new_word, dict_list, 1) == False:
        k = 0
        x_count = 0
        x_skip = 1
        new_word = word
        while k < len(new_word):
            if new_word[k] == x_char and k != 0 and x_count < 2:
                # print('test3')
                if x_skip == 1:
                    x_skip -= 1
                    new_word = new_word[:k] + new_word[k - 1] + new_word[k +
                                                                         1:]
                    if utilities.is_plaintext(word, dict_list, 1) == True:
                        return new_word
                else:
                    x_skip -= 1
                x_count += 1
            k += 1

    return new_word
Esempio n. 8
0
def cryptanalysis_xcrypt(ciphertext):
    for i in range(1, 501):
        #print("i = " + str(i))
        plaintext = d_xcrypt(ciphertext, i)
        #I = utilities.get_indexOfCoin(ciphertext)
        #print(plaintext)
        is_plain = utilities.is_plaintext(plaintext, "engmix.txt", 0.8)
        #print(is_plain)
        if (is_plain == True):
            print(plaintext)
            return i, plaintext
    # your code here
    #print(plaintext)
    return i, plaintext
Esempio n. 9
0
def cryptanalysis1_myszkowski(ciphertext):
    # your code here
    possible = ['aab', 'aba', 'baa', 'bba', 'bab', 'abb']
    dictList = utilities.load_dictionary('engmix.txt')

    attempts = 0
    for pos in possible:
        plaintext = d_myszkowski(ciphertext, pos)
        attempts += 1

        if utilities.is_plaintext(plaintext, dictList, 0.90):
            print('key found after ' + str(attempts) + ' attempts')
            return plaintext, pos

    return '', ''
Esempio n. 10
0
def cryptanalysis_xcrypt(ciphertext):
    # your code here
    if ciphertext[-1] == 'q':
        lastIndex = ciphertext.rfind('q')
        Index = ciphertext.rfind('q', 0, lastIndex)

        possibleKey = len(ciphertext) / (lastIndex - Index)

        plaintext = d_xcrypt(ciphertext, possibleKey)
        if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90):
            utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q1.txt')
            return int(possibleKey), plaintext

    found = False
    for i in range(1, 501):
        plaintext = d_xcrypt(ciphertext, i)
        if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90):
            found = True
            break

    if found is False:
        return 0, ''
    utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q1.txt')
    return i, plaintext
Esempio n. 11
0
def verify_RSA(message):
    # your code here
    file = open('public_keys.txt', 'r')
    content = file.read()
    info = content.split('\n')

    public_keys = []
    for i in info:
        temp = i.split(' ')
        public_keys.append(temp)

    dictList = utilities.load_dictionary('engmix.txt')

    for key in public_keys:
        plaintext = d_RSA(message, (int(key[1]), int(key[2])))

        if utilities.is_plaintext(plaintext, dictList, 0.90):
            return key[0], plaintext

    return 'Unknown', ''
Esempio n. 12
0
def cryptanalysis3_myszkowski(ciphertext):
    # your code here
    init_list = [[0, 0, 0, 1, 2], [1, 1, 1, 0, 2], [2, 2, 2, 0, 1]]
    lower = utilities.get_lower()
    dictList = utilities.load_dictionary('engmix.txt')

    attempts = 0
    for init in init_list:
        combinations = SDES.combinations(init)
        for com in combinations:
            key = ''
            for num in com:
                key += lower[num]

            plaintext = d_myszkowski(ciphertext, key)
            attempts += 1

            if utilities.is_plaintext(plaintext, dictList, 0.80):
                print('key found after ' + str(attempts) + ' attempts')
                return plaintext, key

    return '', ''
Esempio n. 13
0
def cryptanalysis2_myszkowski(ciphertext, length):
    # your code here
    dictList = utilities.load_dictionary('engmix.txt')

    attempts = 0
    for l in dictList:
        for word in l:
            if len(word) == length:
                remove_repetition = ''
                for c in word.lower():
                    if c not in remove_repetition:
                        remove_repetition += c

                if len(remove_repetition) != length and len(
                        remove_repetition) != 1:
                    plaintext = d_myszkowski(ciphertext, word)
                    attempts += 1

                    if utilities.is_plaintext(plaintext, dictList, 0.90):
                        print('key found after ' + str(attempts) + ' attempts')
                        return plaintext, word

    return '', ''
Esempio n. 14
0
def cryptanalysis_mathCipher(ciphertext):
    # your code here
    baseString = utilities.get_baseString()
    length = len(baseString)
    dictList = utilities.load_dictionary('engmix.txt')

    sub_baseString = []
    for j in range(25, length):
        sub_baseString.append(baseString[:j + 1])

    attempts = 0
    for n_s in sub_baseString:
        m = len(n_s)
        m_i_table = mod.mul_inv_table(m)
        for mi1 in m_i_table[0]:
            if m_i_table[1][mi1] != 'NA':
                for mi2 in m_i_table[0]:
                    if m_i_table[1][mi2] != 'NA':
                        for c in range(m):
                            if mod.residue(mi2**2 - c, m) != 0:
                                k = [mi1, mi2, c]
                                key = (n_s, k)
                                plaintext = d_mathCipher(ciphertext, key)
                                attempts += 1

                                if len(utilities.remove_nonalpha(
                                        plaintext)) < len(plaintext) / 2:
                                    continue

                                if utilities.is_plaintext(
                                        plaintext, dictList, 0.90):
                                    print('key found after ' + str(attempts) +
                                          ' attempts')
                                    return plaintext, key

    return '', ''
Esempio n. 15
0
def d_type3(ciphertext):
    alpha = utilities.get_lower()
    dictList = utilities.load_dictionary('engmix.txt')

    for a in range(0, 5):
        for b in range(0, 5):
            for c in range(0, 5):
                for d in range(0, 5):
                    key_matrix = matrix.new_matrix(2, 2, 0)
                    key_matrix[0][0] = a
                    key_matrix[0][1] = b
                    key_matrix[1][0] = c
                    key_matrix[1][1] = d
                    if mod.has_mul_inv(a * d - b * c, 26):
                        key = ''
                        for i in range(2):
                            for j in range(2):
                                key += alpha[key_matrix[i][j]]
                        first = d_hill(ciphertext, key)

                        k, plain = cryptanalysis_shift(first)

                        if utilities.is_plaintext(plain, dictList, 0.70):
                            return plain, (k, key)