Пример #1
0
def d_hill(ciphertext, key):
    # your code here
    if len(ciphertext) == 0:
        print('Error(d_hill): invalid ciphertext')
        return ''

    new_key = ''
    if len(key) > 4:
        new_key += key[:4]
    elif len(key) == 4:
        new_key += key
    else:
        new_key += key
        counter = 0
        while len(new_key) < 4:
            new_key += key[counter]
            counter += 1

    baseString = utilities_A4.get_lower()

    key_matrix = matrix.new_matrix(2, 2, 0)
    count = 0
    for i in range(2):
        for j in range(2):
            key_matrix[i][j] = baseString.index(new_key[count].lower())
            count += 1

    if mod.gcd(matrix.det(key_matrix), 26) != 1:
        print('Error(d_hill): key is not invertible')
        return ''

    inverse_key_matrix = matrix.inverse(key_matrix, 26)

    plaintext = ''
    non_alpha = utilities_A4.get_nonalpha(ciphertext)
    blocks = utilities_A4.text_to_blocks(
        utilities_A4.remove_nonalpha(ciphertext), 2)

    for block in blocks:
        block_m = matrix.new_matrix(2, 1, 0)
        block_m[0][0] = baseString.index(block[0].lower())
        block_m[1][0] = baseString.index(block[1].lower())

        result_m = matrix.matrix_mod(matrix.mul(inverse_key_matrix, block_m),
                                     26)

        plaintext += baseString[result_m[0][0]].lower()
        plaintext += baseString[result_m[1][0]].lower()

    plaintext = utilities_A4.insert_nonalpha(plaintext, non_alpha)
    while plaintext[-1] == 'q':
        plaintext = plaintext[:-1]

    return plaintext
Пример #2
0
def test_q3():
    print("-------------------------------------------")
    print("Testing Q3: Affine Cipher")
    print()

    alphabet = utilities_A4.get_lower()

    baseStr = alphabet
    k = [5, 8]
    plaintext = 'AFFINE CIPHER'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = solution_A4.e_affine(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = solution_A4.d_affine(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    baseStr = alphabet + ' ?!'
    k = [45, 3]
    plaintext = 'Is mission accomplished?'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = solution_A4.e_affine(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = solution_A4.d_affine(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    baseStr = alphabet + ' ?!.'
    k = [10, 19]
    plaintext = 'Plan B ... initiated'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext = ', plaintext)
    print('ciphertext=  ', end='')
    solution_A4.e_affine(plaintext, key)
    print('plaintext2=  ', end='')
    solution_A4.d_affine(plaintext, key)
    print()

    print('Testing cryptanalysis:')
    plaintext = 'There are two kinds of cryptography in this world: cryptography that will stop your kid sister '
    plaintext += 'from reading your files, and cryptography that will stop major governments from reading your files.'
    baseString = utilities_A4.get_baseString()[:26]
    key = (baseString, [3, 9])
    ciphertext = solution_A4.e_affine(plaintext, key)
    plaintext, key = solution_A4.cryptanalysis_affine(ciphertext)
    if plaintext != '':
        print('Key found = ', key)
        print('plaintext = ', plaintext)
    print()
    baseString = utilities_A4.get_baseString()[:27]
    key = (baseString, [17, 11])
    ciphertext = solution_A4.e_affine(plaintext, key)
    plaintext, key = solution_A4.cryptanalysis_affine(ciphertext)
    if plaintext != '':
        print('Key found = ', key)
        print('plaintext = ', plaintext)

    print("-------------------------------------------")
    print()
    return
Пример #3
0
def test_q2():
    print("-------------------------------------------")
    print("Testing Q2: Decimation Cipher")
    print()

    alphabet = utilities_A4.get_lower()

    baseStr = alphabet
    k = 5
    plaintext = 'Strike in progress'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = solution_A4.e_decimation(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = solution_A4.d_decimation(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    baseStr = alphabet + ' ?!'
    k = 46
    plaintext = 'Is mission accomplished?'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext =  ', plaintext)
    ciphertext = solution_A4.e_decimation(plaintext, key)
    print('ciphertext=  ', ciphertext)
    plaintext2 = solution_A4.d_decimation(ciphertext, key)
    print('plaintext2=  ', plaintext2)
    print()

    baseStr = alphabet + ' ?!.'
    k = 10
    plaintext = 'Plan B ... initiated'
    key = (baseStr, k)
    print('key = ', key)
    print('plaintext = ', plaintext)
    print('ciphertext=  ', end='')
    solution_A4.e_decimation(plaintext, key)
    print('plaintext2=  ', end='')
    solution_A4.d_decimation(plaintext, key)
    print()

    print('Testing cryptanalysis:')
    ciphertext = 'Mrhoyu on xhsehumm'
    plaintext, key = solution_A4.cryptanalysis_decimation(ciphertext)
    if plaintext != '':
        print('Key found = ', key)
        print('plaintext = ', plaintext)
    print()

    plaintext = 'There are two kinds of cryptography in this world: cryptography that will stop your kid sister '
    plaintext += 'from reading your files, and cryptography that will stop major governments from reading your files.'
    baseString = utilities_A4.get_baseString()[:35]
    key = (baseString, 33)
    ciphertext = solution_A4.e_decimation(plaintext, key)
    plaintext, key = solution_A4.cryptanalysis_decimation(ciphertext)
    if plaintext != '':
        print('Key found = ', key)
        print('plaintext = ', plaintext)
    print("-------------------------------------------")
    print()
    return