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):
[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 textBut 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.