Python Forum
Monoalphabetic cipher
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Monoalphabetic cipher
#1
Hello!
I wrote this code:
keys={'a':'z','b':'y','c':'x','d':'w','e':'v','f':'u','g':'t','h':'s','i':'r','j':'q','k':'p','l':'o','m':'n'}
reverse_keys={}
for key,value in keys.items():
    reverse_keys[value]=key
def encrypt(text):
    text=str(text)
    encrypting=[]
    for l in text:
        encrypting.append(keys.get(l,l))
    print(''.join(encrypting))
def decipher(text):
    text=str(text)
    decrypted=[]
    for l in text:
        decrypted.append(reverse_keys.get(l,l))
    print(''.join(decrypted))
print(encrypt("hej"))
print(decipher("svq"))
But now I want to create a function encrypt(text,keys) for example encrypt(hej,"zyxwvutsrqponmlkjihgfedcba"). I want the user to define the key himself.
Reply
#2
I would make keys a parameter to decipher, with a default (as a string like in your desired example) equivalent to what the global variable is now. Convert that to a dictionary like keys is now, and use that for encryption. Of course, first you need to fix the fact that your keys only cover half the dictionary, so only half the letters get encrypted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cipher Caesar Azilkhan 1 2,145 Nov-21-2019, 03:40 PM
Last Post: ichabod801
  Caesar cipher Drone4four 19 10,758 Nov-11-2018, 04:07 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020