Reputation: 111
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. Thanks guys!
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!'
Upvotes: 0
Views: 31560
Reputation: 4862
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 dict
s 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!"
# v! 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!
v! 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!
v! zmhvxdmxdmo!nll mikbg
hey, this is really fun!
Upvotes: 5