Exemple #1
0
def cryptanalysis_affine(ciphertext):
    # your code here
    baseString = utilities_A4.get_baseString()
    length = len(baseString)
    dictList = utilities_A4.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_i_table = mod.mul_inv_table(len(n_s))

        for mi in m_i_table[0]:
            if m_i_table[1][mi] != 'NA':
                for beta in range(len(n_s)):
                    k = [mi, beta]
                    key = (n_s, k)
                    plaintext = d_affine(ciphertext, key)
                    attempts += 1

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

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

    return '', ''
def cryptanalysis_decimation(ciphertext):
    plaintext = ''
    baseString = utilities_A4.get_baseString()
    dictList = utilities_A4.load_dictionary('engmix.txt')
    count = 0

    for i in range(0, len(baseString) - 26 + 1):
        base = baseString[:26 + i]
        table = mod.mul_inv_table(26 + i)

        for x in table[1]:
            if x != 'NA':
                key = (base, table[0][x])
                plaintext = d_decimation(ciphertext, key)
                count += 1

                if utilities_A4.is_plaintext(plaintext.lower(), dictList, 0.9):
                    print("Key found after:", count, "attemps")
                    return plaintext, key[1]

    return '', ''
Exemple #3
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 '', ''
Exemple #4
0
def analyze_mathCipher(baseString):
    # your code here
    total = 0
    illegal = 0
    noCipher = 0
    decimation = 0
    valid = 0

    # total
    m = len(baseString)
    total = m**3

    # illegal
    legal = 0
    m_i_table = mod.mul_inv_table(m)
    for mi in m_i_table[1]:
        if mi != 'NA':
            legal += 1
    legal *= legal
    legal *= m
    illegal = total - legal

    # noCipher
    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:
                            if mod.residue(mi1 * mi2, m) == 1:
                                noCipher += 1
                            else:
                                decimation += 1
    valid = legal - decimation - noCipher

    return [total, illegal, noCipher, decimation, valid]
Exemple #5
0
def test_q1():
    print("-------------------------------------------")
    print("Testing Q1: Modular Arithmetic Library")
    filename = 'q1_solution.txt'
    outFile = open(filename, 'w')
    print()

    outFile.write('1- Testing residue_set:\n')
    outFile.write('residue_set({}) =  {}\n'.format(10, mod.residue_set(10)))
    outFile.write('residue_set({}) =   {}\n'.format(1, mod.residue_set(1)))
    outFile.write('residue_set({}) =  '.format(-5))
    outFile.write('{}\n'.format(mod.residue_set(-5)))
    outFile.write('residue_set({}) = '.format([5]))
    outFile.write('{}\n'.format(mod.residue_set([5])))
    outFile.write('\n')

    outFile.write('2- Testing residue:\n')
    outFile.write('residue({},{}) =  {}\n'.format(17, 5, mod.residue(17, 5)))
    outFile.write('residue({},{}) = '.format(3.4, 5))
    outFile.write('{}\n'.format(mod.residue(3.4, 5)))
    outFile.write('residue({},{}) = '.format(13, -5))
    outFile.write('{}\n'.format(mod.residue(13, -5)))
    outFile.write('\n')

    outFile.write('3- Testing is_congruent:\n')
    outFile.write('is_congruent({},{},{})= {}\n'.format(
        22, 33, 11, mod.is_congruent(22, 33, 11)))
    outFile.write('is_congruent({},{},{}) =   {}\n'.format(
        7, 9, 3, mod.is_congruent(7, 9, 3)))
    outFile.write('is_congruent({},{},{})=  '.format(3.4, 5, 9))
    outFile.write('{}\n'.format(mod.is_congruent(3.4, 5, 9)))
    outFile.write('is_congruent({},{},{}) =  '.format(3, 5, -9))
    outFile.write('{}\n'.format(mod.is_congruent(3, 5, -9)))
    outFile.write('\n')

    outFile.write('4- Testing add:\n')
    outFile.write('add({},{},{}) =  {}\n'.format(17, 23, 7, mod.add(17, 23,
                                                                    7)))
    outFile.write('add({},{},{}) = {}\n'.format(-17, 23, 7,
                                                mod.add(-17, 23, 7)))
    outFile.write('add({},{},{}) = {}\n'.format(17, -23, 7,
                                                mod.add(17, -23, 7)))
    outFile.write('add({},{},{}) =   '.format(9, 17, 0))
    outFile.write('{}\n'.format(mod.add(9, 17, 0)))
    outFile.write('add({},{},{}) = '.format([9], 17, 7))
    outFile.write('{}\n'.format(mod.add([9], 17, 7)))
    outFile.write('add({},{},{}) = '.format(9, 17.1, 8))
    outFile.write('{}\n'.format(mod.add(9, 17.1, 8)))
    outFile.write('\n')

    outFile.write('5- Testing sub:\n')
    outFile.write('sub({},{},{}) =  {}\n'.format(17, 23, 7, mod.sub(17, 23,
                                                                    7)))
    outFile.write('sub({},{},{}) = {}\n'.format(-17, 23, 7,
                                                mod.sub(-17, 23, 7)))
    outFile.write('sub({},{},{}) = {}\n'.format(17, -23, 7,
                                                mod.sub(17, -23, 7)))
    outFile.write('sub({},{},{}) =   '.format(9, 17, 0))
    outFile.write('{}\n'.format(mod.sub(9, 17, 0)))
    outFile.write('sub({},{},{}) = '.format([9], 17, 7))
    outFile.write('{}\n'.format(mod.sub([9], 17, 7)))
    outFile.write('sub({},{},{}) = '.format(9, 17.1, 8))
    outFile.write('{}\n'.format(mod.sub(9, 17.1, 8)))
    outFile.write('\n')

    outFile.write('6- Testing additive inverse:\n')
    outFile.write('add_inv({},{}) =   {}\n'.format(3, 5, mod.add_inv(3, 5)))
    outFile.write('add_inv({},{}) =   {}\n'.format(6, 1, mod.add_inv(6, 1)))
    outFile.write('add_inv({},{})=  {}\n'.format(22, 10, mod.add_inv(22, 10)))
    outFile.write('add_inv({},{}) =  '.format(6, -1))
    outFile.write('{}\n'.format(mod.add_inv(6, -1)))
    outFile.write('add_inv({},{}) = '.format(6.2, 6))
    outFile.write('{}\n'.format(mod.add_inv(6.2, 6)))
    a = 4
    b = 2
    m = 5
    result = mod.sub(a, b, m) == mod.add(a, mod.add_inv(b, m), m)
    outFile.write(
        'sub({0},{1},{2}) == add({0},add_inv({1},{2}),{2})? = {3}\n'.format(
            a, b, m, result))
    outFile.write('\n')

    outFile.write('7- Testing Addition Table:\n')
    outFile.write('Addition Table for mode {} =\n'.format(5))
    addTab = mod.add_table(5)
    for i in range(len(addTab)):
        outFile.write(str(addTab[i]))
        outFile.write('\n')
    outFile.write('Addition Table for mode {} =\n'.format(8))
    addTab = mod.add_table(8)
    for i in range(len(addTab)):
        outFile.write(str(addTab[i]))
        outFile.write('\n')
    outFile.write('Addition Table for mode {} =\n'.format(0))
    outFile.write(mod.add_table(0))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('8- Testing Subtraction Table:\n')
    outFile.write('Subtraction Table for mode {} =\n'.format(5))
    subTab = mod.sub_table(5)
    for i in range(len(subTab)):
        outFile.write(str(subTab[i]))
        outFile.write('\n')
    outFile.write('Subtraction Table for mode {} =\n'.format(8))
    subTab = mod.sub_table(8)
    for i in range(len(subTab)):
        outFile.write(str(subTab[i]))
        outFile.write('\n')
    outFile.write('Subtraction Table for mode {} =\n'.format([5]))
    outFile.write(mod.sub_table([5]))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('9- Testing Addition Inverse Table:\n')
    outFile.write('Addition Inverse Table for mode {} =\n'.format(5))
    addInvTab = mod.add_inv_table(5)
    outFile.write(str(addInvTab[0]))
    outFile.write('\n')
    outFile.write(str(addInvTab[1]))
    outFile.write('\n')
    outFile.write('Addition Inverse Table for mode {} =\n'.format(26))
    addInvTab = mod.add_inv_table(26)
    outFile.write(str(addInvTab[0]))
    outFile.write('\n')
    outFile.write(str(addInvTab[1]))
    outFile.write('\n')
    outFile.write('Addition Inverse Table for mode {} =\n'.format(-2))
    outFile.write(mod.add_inv_table(-2))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('10- Testing mul:\n')
    outFile.write('mul({},{},{}) =    {}\n'.format(3, 5, 5, mod.mul(3, 5, 5)))
    outFile.write('mul({},{},{}) =    {}\n'.format(8, 3, 7, mod.mul(8, 3, 7)))
    outFile.write('mul({},{},{})=   {}\n'.format(17, -3, 7, mod.mul(17, -3,
                                                                    7)))
    outFile.write('mul({},{},{}) =   '.format(9, 17, 0))
    outFile.write('{}\n'.format(mod.mul(9, 17, 0)))
    outFile.write('mul({},{},{}) = '.format([9], 17, 7))
    outFile.write('{}\n'.format(mod.mul([9], 17, 7)))
    outFile.write('mul({},{},{}) = '.format(9, 17.1, 8))
    outFile.write('{}\n'.format(mod.mul(9, 17.1, 8)))
    outFile.write('\n')

    outFile.write('11- Testing Multiplication Table:\n')
    outFile.write('Multiplication Table for mode {} =\n'.format(4))
    mulTab = mod.mul_table(4)
    for i in range(len(mulTab)):
        outFile.write(str(mulTab[i]))
        outFile.write('\n')
    outFile.write('Multiplication Table for mode {} =\n'.format(5))
    mulTab = mod.mul_table(5)
    for i in range(len(mulTab)):
        outFile.write(str(mulTab[i]))
        outFile.write('\n')
    outFile.write('Multiplication Table for mode {} =\n'.format(-5))
    outFile.write(mod.mul_table(-5))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('12- Testing is_prime:\n')
    outFile.write('is_prime({}) =  {}\n'.format(97, mod.is_prime(97)))
    outFile.write('is_prime({}) = {}\n'.format(479, mod.is_prime(479)))
    outFile.write('is_prime({})= {}\n'.format(1044, mod.is_prime(1044)))
    outFile.write('is_prime({}) =   {}\n'.format(0, mod.is_prime(0)))
    outFile.write('is_prime({}) = {}\n'.format(-17, mod.is_prime(-17)))
    outFile.write('\n')

    outFile.write('13- Testing gcd:\n')
    outFile.write('gcd({},{}) =  {}\n'.format(629, 357, mod.gcd(629, 357)))
    outFile.write('gcd({},{}) =  {}\n'.format(440, 700, mod.gcd(440, 700)))
    outFile.write('gcd({},{}) =  {}\n'.format(-30, 700, mod.gcd(-30, 700)))
    outFile.write('gcd({},{}) = {}\n'.format(540, -539, mod.gcd(540, -539)))
    outFile.write('gcd({},{})   = '.format(711, 0))
    outFile.write(mod.gcd(711, 0))
    outFile.write('\n')
    outFile.write('gcd({},{})   = '.format(0, 311))
    outFile.write(mod.gcd(0, 311))
    outFile.write('\n')
    outFile.write('gcd({},{})  = '.format([9], 27))
    outFile.write(mod.gcd([9], 27))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('14- Testing is_relatively_prime:\n')
    outFile.write('is_relatively_prime({},{}) =     {}\n'.format(
        4, 5, mod.is_relatively_prime(4, 5)))
    outFile.write('is_relatively_prime({},{})=  {}\n'.format(
        540, 539, mod.is_relatively_prime(540, 539)))
    outFile.write('is_relatively_prime({},{}) =   {}\n'.format(
        18, 26, mod.is_relatively_prime(18, 26)))
    outFile.write('is_relatively_prime({},{}) =    {}\n'.format(
        0, 26, mod.is_relatively_prime(0, 26)))
    outFile.write('is_relatively_prime({},{}) =  '.format([1], 26))
    outFile.write(mod.is_relatively_prime([1], 26))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('15- Testing has_mul_inv:\n')
    outFile.write('has_mul_inv({},{}) =     {}\n'.format(
        4, 5, mod.has_mul_inv(4, 5)))
    outFile.write('has_mul_inv({},{}) =   {}\n'.format(17, 26,
                                                       mod.has_mul_inv(17,
                                                                       26)))
    outFile.write('has_mul_inv({},{}) =   {}\n'.format(18, 26,
                                                       mod.has_mul_inv(18,
                                                                       26)))
    outFile.write('has_mul_inv({},{}) =    {}\n'.format(
        0, 26, mod.has_mul_inv(0, 26)))
    outFile.write('has_mul_inv({},{}) =  '.format([1], 26))
    outFile.write(mod.has_mul_inv([1], 26))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('16- Testing EEA:\n')
    outFile.write('eea({},{}) =   {}\n'.format(700, 440, mod.eea(700, 440)))
    outFile.write('eea({},{}) =     {}\n'.format(88, 35, mod.eea(88, 35)))
    outFile.write('eea({},{}) =     {}\n'.format(35, 88, mod.eea(35, 88)))
    outFile.write('eea({},{}) =    {}\n'.format(-88, 35, mod.eea(-88, 35)))
    outFile.write('eea({},{}) =    {}\n'.format(88, -35, mod.eea(88, -35)))
    outFile.write('eea({},{}) =     '.format(0, 777))
    outFile.write(mod.eea(0, 777))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('17- Testing mul_inv:\n')
    outFile.write('mul_inv({},{}) =   {}\n'.format(23, 26, mod.mul_inv(23,
                                                                       26)))
    outFile.write('mul_inv({},{}) =     {}\n'.format(5, 6, mod.mul_inv(5, 6)))
    outFile.write('mul_inv({},{}) =   {}\n'.format(24, 26, mod.mul_inv(24,
                                                                       26)))
    outFile.write('mul_inv({},{}) = {}\n'.format(700, 440,
                                                 mod.mul_inv(700, 440)))
    outFile.write('mul_inv({},{}) =   {}\n'.format(0, 777,
                                                   mod.mul_inv(700, 440)))
    outFile.write('mul_inv({},{}) =  '.format(1, [99]))
    outFile.write(mod.mul_inv(1, [99]))
    outFile.write('\n')
    outFile.write('mul_inv({},{}) =  '.format([1], 99))
    outFile.write(mod.mul_inv([1], 99))
    outFile.write('\n')
    outFile.write('\n')

    outFile.write('18- Testing Multiplicative Inverse Table:\n')
    outFile.write('Multiplicative Inverse Table for mode {} =\n'.format(5))
    mulInvTab = mod.mul_inv_table(5)
    outFile.write(str(mulInvTab[0]))
    outFile.write('\n')
    outFile.write(str(mulInvTab[1]))
    outFile.write('\n')
    outFile.write('Multiplicative Inverse Table for mode {} =\n'.format(26))
    mulInvTab = mod.mul_inv_table(26)
    outFile.write(str(mulInvTab[0]))
    outFile.write('\n')
    outFile.write(str(mulInvTab[1]))
    outFile.write('\n')
    outFile.write('Multiplicative Inverse Table for mode {} =\n'.format(-2))
    outFile.write(mod.mul_inv_table(-2))
    outFile.write('\n')
    outFile.write('\n')

    outFile.close()
    print('Comparing q1_solution with q1_sample:')
    print(utilities_A4.compare_files('q1_solution.txt', 'q1_sample.txt'))
    print()
    print("-------------------------------------------")

    return