Reputation: 17
I'm supposed to do Permutation encryption where I'm provided with a text and a key. Lets say the
text = "abbdcada"
key = "dcab"
so I have to map something like this (a,b,c,d) -> (d,c,a,b) so the output of above input should be
output = dccbadbd
I can easily do this if I have text and key of same length but I'm unable to make a logic for text longer than key length.
Can anyone help me plz...
Upvotes: 0
Views: 81
Reputation: 6112
You can use a dictionary for the mapping.
def encrypt(text):
mapping = {'a': 'd', 'b': 'c', 'c': 'a', 'd': 'b'}
return ''.join(mapping[i] for i in text)
>>> encrypt("abbdcada")
'dccbadbd'
Upvotes: 1