Python Forum
Write function to find decryptionkey - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Write function to find decryptionkey (/thread-22960.html)



Write function to find decryptionkey - kotter - Dec-05-2019

I have the following assignment:

[inline]Say we have a random string containing only small letters. Then we have a key that encrypts the string.
An example of such a key could be for example (but it could be other keys as well):

a becomes q, b becomes w, c becomes e, so the letters ordered like the english alphabet get changed by the key like the order of a qwerty keyboard.

Now I need to write a function such that the key given as argument key in the function gives as output the decryption key. By using the decryption key, the encrypted string changes back into its orginal string.
Two examples of keys with decryption keys:

(1)
key = "badcfehgjilknmporqtsvuxwzy"
decryption key = "badcfehgjilknmporqtsvuxwzy"

(2)
key = qwertyuiopasdfghjklzxcvbnm
decryption key = kxvmcnophqrszyijadlegwbuft
[/inline]

I've tried to come up with a function, but whatever I try I always get the wrong output. This is currently my function (function IsLetter just checks if every character in the key is a letter):

def DecryptionKey(key):
    array1 = list(range(0, 26))
    array2 = []
    for i in key:
        if IsLetter(i):
            number = ord(i) - 97
            array2.append(number)
    array3 = [x - y for x, y in zip(array1, array2)]
    array4 = [-x for x in array3]
    array5 = [x + y for x,y in zip(array1, array4)]
    text = ''
    for i in array5:
        text += key[i]
    return text
            
But my output for the two examples given above is:

Output:
abcdefghijklmnopqrstuvwxyz jvtkznxoghqlryuipasmbecwfd
Edit: The problem is that I don't know how to solve this 'puzzle'. I think I am missing some logical steps. Anyone who has an idea? All help is appreciated.


RE: Write function to find decryptionkey - buran - Dec-05-2019

for start - post the text of your assignment verbatim. From your explanations is not clear


RE: Write function to find decryptionkey - kotter - Dec-05-2019

Assignment: Implement the function DecryptionKey(key). This function returns new_key created from the encryption key stored in key. The new key must be the key used to decrypt a text that was encrypted using key. So, when new_text = ConvertText(text, key) was used to encrypt text, then text = ConverText(new_text, new_key) can be used to decrypt new_test. Test this function.

This is the assignment, I hope things are clearer now, if not, let me know.

Now I have the following function:

def DecryptionKey(key):
    array1 = list(range(0, 26))
    Dekey = ""
    array2 = []
    for i in key:
        number = ord(i) - 97
        lijst2.append(number)
    array3 = list(zip(array1, lijst2))
    
    return array3
With output:

Output:
[(0, 16), (1, 22), (2, 4), (3, 17), (4, 19), (5, 24), (6, 20), (7, 8), (8, 14), (9, 15), (10, 0), (11, 18), (12, 3), (13, 5), (14, 6), (15, 7), (16, 9), (17, 10), (18, 11), (19, 25), (20, 23), (21, 2), (22, 21), (23, 1), (24, 13), (25, 12)]
So we have for example in the current list a tuple (0, 16). But I want a new list that has the same tuples but now (16, 9), since we have tuples (0, 16) and (16, 9) in the current list. Another example: In the current list there is a tuple (10, 0), but in the new list I want a tuple (0, 16), since in the current list we have the tuples (0, 16) and (10, 0). Anyone any idea how I could do this?

If I could create such new array in the function than I think I solved the problem


RE: Write function to find decryptionkey - nilamo - Dec-11-2019

Why are you using ord() to try to find the corresponding character? As far as I know, there's no association on the qwerty keyboard that's like that.

I think it'd be easier (to write, and to understand), to have two strings, one that's a-z, and the other that's qwerty (q-m). Then finding the key is as easy as looking up the index from one string and applying it to the other string.

So, something like:
def build_key(seed):
    alpha = 'abcdefg'
    shifted = 'qwertyu'
    key = []
    for character in seed:
        index = alpha.find(character)
        key.append(shifted[index])
    return "".join(key)

print(build_key("beg")) # wtu