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))
Rook
  • 5,734
  • 3
  • 34
  • 43
  • Possible duplicate of [How would I make a simple encryption/decryption program?](http://stackoverflow.com/questions/33221559/how-would-i-make-a-simple-encryption-decryption-program) – Artjom B. Feb 28 '16 at 16:50
  • in python 3 see https://docs.python.org/3/library/stdtypes.html#str.maketrans and https://docs.python.org/3/library/stdtypes.html#str.translate – Paweł Kordowski Feb 28 '16 at 16:59

1 Answers1

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'
Paweł Kordowski
  • 2,688
  • 1
  • 14
  • 21