Umar Gulzar
Umar Gulzar

Reputation: 17

Replace a text(of any length) with a key(shuffled characters) provided using Python(Permutation encryption)

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

Answers (1)

alec
alec

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

Related Questions