3

I need to encrypt a message using a secret key and return the message. I tried this and I got the correct output.

def my_encryption(some_string):
    character_set= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
    secret_key="    Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"
    m=some_string
    k=m.translate({ord(x): y for (x, y) in zip(character_set,secret_key )})
    return m

print(my_encryption("Lets meet at the usual place at 9 am"))

The output I got is

oABjMWAABMDBMB2AMvjvDPMYPD1AMDBMGMDWB

and this is correct. I would like to know, will there be any other way to do this with out using translate?. I am curious to know the alternate ways. I will be glad to know. Thank you.

3 Answers 3

2

you can use a simple dictionary

def my_encryption(some_string):
    character_set= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
    secret_key=    "Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"
    table = {x: y for (x, y) in zip(character_set,secret_key )}
    return "".join( map(lambda x:table.get(x,x),some_string) )

the get method can receive 2 arguments, the first is the key to search and the second is a value to return in case that the key is not present, in this case assign that as x to leave that unchanged

here a test

>>> my_encryption("Lets meet at the usual place at 9 am")
'oABjMWAABMDBMB2AMvjvDPMYPD1AMDBMGMDW'
>>> 

this is usually the first thing that come to my mind when I want to do this simple substitution cipher.

and the inverse is as simple as invert key-value

def my_decription(some_string):
    character_set= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
    secret_key=    "Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"
    table = {x: y for (x, y) in zip(character_set,secret_key )}
    return "".join( map(lambda x:table.get(x,x),some_string) )

>>> my_decription('oABjMWAABMDBMB2AMvjvDPMYPD1AMDBMGMDW')
'Lets meet at the usual place at 9 am'
>>> 
Sign up to request clarification or add additional context in comments.

Comments

1

A simple solution I use when making things less plain text is base64 module. This is not encryption by any means. Just makes the text a little harder to read:

>>> import base64
>>> base64.b64encode(b'This is a secret.')
b'VGhpcyBpcyBhIHNlY3JldC4='
>>> base64.b64decode(b'VGhpcyBpcyBhIHNlY3JldC4=').decode('utf-8')
'This is a secret.'

3 Comments

no, if you observe the character set and secret key, a is mapped to D and b is mapped to d and so on.. with that character set only i would like to perform operations. i want to know the various ways other than usage of translate.
it is encrypting using the secret key above not just encrypting randomly.
Ah I see. Maybe this answer would be of some use? Simple way to encode a string according to a password?
0
def my_encryption(some_string):
     output_string=""
     index=0
     character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
     secret_key    = "Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"
     for character in some_string:
        index=character_set.find(character)
        output_string=output_string+secret_key[index]
      return output_string

1 Comment

While this code may solve the question, including an explanation really helps to improve the quality of your post.

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.