Exemple #1
0
def d_blockRotate(ciphertext, key):
    # your code here
    plaintext = ''
    nonAlpha = get_nonalpha(ciphertext)
    ciphertext = "".join([char for char in ciphertext if char.isalpha()])

    blocks = text_to_blocks(ciphertext, key[0])

    for i in range(len(blocks)):
        blocks[i] = utilities_A2.shift_string(blocks[i], key[1], 'r')
        #print(blocks[i])

    temp = ''
    for letter in blocks[len(blocks) - 1]:
        if (letter != 'q'):
            temp += letter
    blocks[len(blocks) - 1] = temp

    for block in blocks:
        for j in range(len(block)):
            plaintext += block[j]

    plaintext = insert_nonalpha(plaintext, nonAlpha)

    return plaintext
def e_blockRotate(plaintext, key):
    # your code here
    nonAlpha = get_nonalpha(plaintext)
    ciphertext = ''
    plaintext = "".join([char for char in plaintext if char.isalpha()])
    blocks = text_to_blocks(plaintext, key[0])
    for _ in range(len(blocks[len(blocks) - 1]), len(blocks[0]), 1):
        blocks[len(blocks) - 1] += 'q'
    for block in blocks:
        ciphertext += utilities_A2.shift_string(block, key[1], 'l')
    ciphertext = insert_nonalpha(ciphertext, nonAlpha)
    return ciphertext
Exemple #3
0
def getKeyL_shift(ciphertext):
    # your code here
    counts = [0] * 21
    for i in range(1, 21):
        s = utilities_A2.shift_string(ciphertext, i, 'r')
        matches = 0
        for j in range(i, len(s)):
            if ciphertext[j] == s[j]:
                matches += 1
                counts[i] = matches
    maxN = counts[0]
    for num in counts:
        if num > maxN:
            maxN = num
    k = counts.index(maxN)
    return k
Exemple #4
0
def d_blockRotate(ciphertext, key):
    nonAlpha = get_nonalpha(ciphertext)
    ciphertext = "".join([char for char in ciphertext if char.isalpha()])
    blocks = text_to_blocks(ciphertext, key[0])
    plaintext = ''

    for block in blocks:
        plaintext += utilities_A2.shift_string(block, key[1], 'r')

    plaintext = insert_nonalpha(plaintext, nonAlpha)
    index = 0
    for i in range(len(plaintext) - 1, -1, -1):
        if plaintext[i] != 'q':
            index = i
            break

    plaintext = plaintext[0:index + 1]

    return plaintext