我必须制作一个替换密码程序,在那里我首先创建一个随机密钥,然后使用这个密钥来解密/加密一些用户输入(明文)。问题的约束如下:
encryptMsg(plaintext,key,alphabet) 以明文字符串、字母字符串和密钥字符串为参数,返回加密后的密码字符串。请注意,在此函数中,您必须首先将纯文本字符串转换为全部小写,并删除所有未出现在字母字符串中的标点符号/字符!
解密Msg(ciphertext,key,alphabet) 将取密文字符串、字母字符串和密钥字符串并返回明文字符串。
makeKey(alphabet) 通过随机打乱字母字符串参数中的字符来生成并返回一个密钥字符串。提示:这涉及将字符串转换为列表,使用 random.shuffle() 方法,然后将列表转换回字符串
这是我所拥有的:
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)
Run Code Online (Sandbox Code Playgroud)
我知道我在 makeKey 函数上做错了,因为它不起作用。我需要一些关于如何启动其他功能的帮助。我们不能使用字典,只能使用列表方法。任何关于在我的任务中快速启动我的帮助或建议将不胜感激。谢谢你们!
更新:示例输出如下:
字母:'abcdefghijklmnopqrstuvwxyz.,!'
键:'nu.t!iyvxqfl,bcjrodhkaew spzgm'
输入明文:嘿嘿,真好玩!
密文:'v! zmhvxdmxdmo!nll mikbg'
解密文本:'嘿,这真的很有趣!
原答案如下:
请向我们展示一些示例输入和输出作为示例。根据您的代码,我可以想出以下内容 -random.shuffle将所有内容洗牌并返回None,将您的 makeKey 更改为:
def makeKey(alphabet):
alphabet = list(alphabet)
random.shuffle(alphabet)
return ''.join(alphabet)
Run Code Online (Sandbox Code Playgroud)
编辑 2:
对于dict在加密/解密中不使用s的方法,请参见下文:
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))
Run Code Online (Sandbox Code Playgroud)
打印:
Hey, this is really fun!
v! zmhvxdmxdmo!nll mikbg
hey, this is really fun!
Run Code Online (Sandbox Code Playgroud)
编辑:
经过一些间距问题和实验,我想出了这个相当简单的解决方案:
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))
Run Code Online (Sandbox Code Playgroud)
这打印:
Hey, this is really fun!
v! zmhvxdmxdmo!nll mikbg
hey, this is really fun!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20527 次 |
| 最近记录: |