Implement Monoalphabetic Cipher Encryption And Decryption In Python
Ek Hazaron Mein Meri Behna Hai Online Serial more. I have to make a Substitution Cipher Program, where I first create a randomized secret-key and then use this key to decrypt/ encrypt some user input (plaintext). The constraints for the problem as follows: • encryptMsg(plaintext,key,alphabet) Takes a plaintext string, an alphabet string and a secret key string as arguments and returns an encrypted cipher string.
Note, within this function, you must first convert the plaintext string to all lower case and remove any punctuation/characters that do not appear in the alphabet string! • decryptMsg(ciphertext,key,alphabet) Will take a ciphertext string, an alphabet string and a secret key string and return the plaintext string. • makeKey(alphabet) Generate and return a secret-key string by randomly shuffling the characters in the alphabet string argument. Hint: this involves turning the string into a list, using the random.shuffle() method, then turning the list back into a string Here's what I have: import random alphabet = 'abcdefghijklmnopqrstuvwxyz.,!'
' def makeKey(alphabet): alphabet= list(alphabet) key= random.shuffle(alphabet) alphabet= str(alphabet) return key def encrypt(plaintext, key, alphabet): ''Encrypt the string and return the ciphertext'' return '.join(key[l] for l in plaintext) print (alphabet) I know I'm doing something wrong with the makeKey function because it doesn't work. I need some help on how to start the other functions. We cannot use dictionaries, only list methods.
Any help or just advice on jumpstarting me in my assignment will be highly appreciated. UPDATE: A sample output is as follows: Alphabet: 'abcdefghijklmnopqrstuvwxyz.,! ' Key: 'nu.t!iyvxqfl,bcjrodhkaew spzgm' Input plaintext: Hey, this is really fun! Cipher text: 'v!
Zmhvxdmxdmo!nll mikbg' Decrypted text: 'hey, this is really fun!' Original answer below: Please show us some sample input and output for an example. Based on your code, I can come up with the following - random.shuffle shuffles everything in place and returns None, change your makeKey to: def makeKey(alphabet): alphabet = list(alphabet) random.shuffle(alphabet) return '.join(alphabet) EDIT 2: For an approach without using dicts in encryption/decryption, see below: import random alphabet = 'abcdefghijklmnopqrstuvwxyz.,! ' # Note the space at the end, which I kept missing. # You could generate the key below using makeKey (i.e.
Key=makeKey(alphabet)) key = 'nu.t!iyvxqfl,bcjrodhkaew spzgm' plaintext = 'Hey, this is really fun!' Zmhvxdmxdmo!nll mikbg def makeKey(alphabet): alphabet = list(alphabet) random.shuffle(alphabet) return '.join(alphabet) def encrypt(plaintext, key, alphabet): keyIndices = [alphabet.index(k.lower()) for k in plaintext] return '.join(key[keyIndex] for keyIndex in keyIndices) def decrypt(cipher, key, alphabet): keyIndices = [key.index(k) for k in cipher] return '.join(alphabet[keyIndex] for keyIndex in keyIndices) cipher = encrypt(plaintext, key, alphabet) print(plaintext) print(cipher) print(decrypt(cipher, key, alphabet)) Prints: Hey, this is really fun! Zmhvxdmxdmo!nll mikbg hey, this is really fun! EDIT: After some spacing issues and experimentation, I came up with this rather simple solution: import random alphabet = 'abcdefghijklmnopqrstuvwxyz.,! ' key = 'nu.t!iyvxqfl,bcjrodhkaew spzgm' plaintext = 'Hey, this is really fun!' Def makeKey(alphabet): alphabet = list(alphabet) random.shuffle(alphabet) return '.join(alphabet) def encrypt(plaintext, key, alphabet): keyMap = dict(zip(alphabet, key)) return '.join(keyMap.get(c.lower(), c) for c in plaintext) def decrypt(cipher, key, alphabet): keyMap = dict(zip(key, alphabet)) return '.join(keyMap.get(c.lower(), c) for c in cipher) cipher = encrypt(plaintext, key, alphabet) print(plaintext) print(cipher) print(decrypt(cipher, key, alphabet)) This prints: Hey, this is really fun! Zmhvxdmxdmo!nll mikbg hey, this is really fun!