Beispiel #1
0
def encrypt(message, key):
    """ apply the null cipher, where the key tells at what index the letters of the plaintext should be hidden in each
    word* of the encrypted text. the 'words' generated by this function will be 5-letter long strings of randomly-chosen letters

    for instance here, are valid encryptions:
        - for key=0, HIGH could be encrypted as HITOW IPSLO GOPES HYTER
        - for key=2, NOON could be encrypted as TUNDE ODOWT ROOWT PUNDT
    """

    message = pre_treat_message(message) # convert to uppercase and remove punctuation

    word_substitutes = []  # store the words here

    # assemble the disguised message by going through each letter of the plaintext
    # and replacing it with a random 5-letter string that has that character in the key position

    for c in message:
        if c not in ALPHABET:  # for our implementation, we won't bother trying to encrypt values not in ALPHABET
            continue

        # which index of the 5-letter string should match the plaintext?
        index_of_plaintext = key % 5  # use the modulo operator so that we can "wrap" large keys into an index that exists in the 5-letter string

        # generate a 5-letter string that has random values for all positions except for index_of_plaintext
        word_substitute = ''
        for i in range(0,5):
            if i == index_of_plaintext:
                word_substitute += c
            else:
                # add a randomly-picked letter to word_substitute
                replace with code that randomly picks a letter and appends it to word_substitute

        word_substitutes.append(word_substitute)

    return ' '.join(word_substitutes)  # return the words as a single string, with words separated by a space
Beispiel #2
0
def decrypt(message, key):
    """ apply the null cipher, where the key tells at what index the letters of the plaintext are hidden in each
    5-letter 'word' of the encrypted text
    """
    message = pre_treat_message(message)  # convert to uppercase and remove punctuation

    words_in_message = message.split()

    return ''.join(word[key % 5] for word in words_in_message)
Beispiel #3
0
def decrypt(message, key):
    """ apply the null cipher, where the key tells at what index the letters of the plaintext are hidden in each
    5-letter 'word' of the encrypted text
    """
    message = pre_treat_message(message)  # convert to uppercase and remove punctuation

    words_in_message = message.split()

    decrypted_message = ''

    for word in words_in_message:
        decrypted_message += word[key % 5]  # find the letter whose index is equal to the key

    return decrypted_message
Beispiel #4
0
def decrypt(message, key):
    """ apply the null cipher, where the key tells at what index the letters of the plaintext are hidden in each
    5-letter 'word' of the encrypted text
    """
    message = pre_treat_message(message)  # convert to uppercase and remove punctuation

    words_in_message = message.split()

    decrypted_message = ''

    for word in words_in_message:
        decrypted_message += word[replace with logic that computes the index of the plaintext letter]  # find the letter whose index is equal to the key

    return decrypted_message
Beispiel #5
0
def decrypt(message):

    message = pre_treat_message(message)

    decrypted_message = ''

    # assemble the plaintext message by decrypting the message one letter at a time
    for c in message:
        if c not in ALPHABET:
            decrypted_message += c
            continue

        # find the corresponding decrypted letter and append it to decrypted_message
        replace with the logic for decrypting the letter    return decrypted_message
def decrypt(message):

    message = pre_treat_message(message)

    decrypted_message = ''

    # assemble the plaintext message by decrypting the message one letter at a time
    for c in message:
        if c not in ALPHABET:
            decrypted_message += c
            continue

        # find the corresponding decrypted letter and append it to decrypted_message
        decrypted_message += ALPHABET[len(ALPHABET)-1-ALPHABET.find(c)]

    return decrypted_message
Beispiel #7
0
def decrypt(message, key):

    message = pre_treat_message(message)

    decrypted_message = ''

    # assemble the plaintext message by decrypting the message one letter at a time
    for c in message:
        if c not in ALPHABET:
            decrypted_message += c
            continue

        # find the corresponding decrypted letter and append it to decrypted_message
        replace with the logic for decrypting the letter

    return decrypted_message
Beispiel #8
0
def encrypt(message, key):

    message = pre_treat_message(message)

    encrypted_message = ''

    # assemble the encrypted message by replacing the letters one at a time
    for c in message:
        if c not in ALPHABET:
            encrypted_message += c
            continue

        index_in_alphabet = ALPHABET.find(c)
        shifted_index = replace with the code that finds the index of the letter to use  # calculate the index of the substitute letter
        encrypted_message += ALPHABET[shifted_index]

    return encrypted_message
def decrypt(message):

    message = pre_treat_message(message)

    decrypted_message = ''

    # assemble the plaintext message by decrypting the message one letter at a time
    for c in message:
        if c not in ALPHABET:
            decrypted_message += c
            continue

        # find the corresponding decrypted letter and append it to decrypted_message
        index_in_alphabet = ALPHABET.find(c)
        shifted_index = 25 - index_in_alphabet
        decrypted_message += ALPHABET[shifted_index]

    return decrypted_message