0

I need to encrypt a string in python according to a cipher.

character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
secret_key    = "Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"

any ideas? my function always returns a long list like:

2
e
4
4

code:

import string

def my_encryption(s):        
    s.translate(str.maketrans(character_set, secret_key))
2

1 Answer 1

0

in python 2

>>> import string
>>> translate_table = string.maketrans(character_set, secret_key)
>>> string.translate("test", translate_table)
'BAjB'

in python 3:

>>> "test".translate(str.maketrans(character_set, secret_key))
'BAjB'

or manually in python 3:

>>> D = dict(zip(character_set, secret_key))
>>> ''.join(map(D.get, "test"))
'BAjB'
Sign up to request clarification or add additional context in comments.

7 Comments

type str has no attr maketrans
it has, docs.python.org/3/library/stdtypes.html#str.maketrans do you use python 3 or python 2?
python 3, but in edX, not a full python lib
not really, and I guess you want to return something from your function, where do you define character_set and secret_key? I would like to see the whole code
character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " secret_key = "Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.