替换Cipher Python

时间:2016-03-23 20:36:43

标签: python

我必须制作一个替代密码程序,我首先创建一个随机密钥,然后使用此密钥解密/加密某些用户输入(明文)。问题的限制如下:

  1. encryptMsg(明文,键,字母表) 将明文字符串,字母字符串和密钥字符串作为参数并返回 加密的密码字符串。注意,在此函数中,您必须首先将明文字符串转换为all 小写并删除任何未出现在字母表字符串中的标点/字符!

  2. decryptMsg(密文,密钥,字母) 将获取密文字符串,字母字符串和密钥字符串并返回明文 字符串。

  3. makeKey(字母) 通过随机填充字母表字符串中的字符来生成并返回一个密钥字符串 论点。提示:这涉及使用random.shuffle()将字符串转换为列表 方法,然后将列表转回字符串

  4. 以下是我所拥有的:

    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)
    

    我知道我对makeKey函数做错了,因为它不起作用。我需要一些关于如何启动其他功能的帮助。我们不能使用字典,只能列出方法。任何帮助或只是建议我在我的任务中启动我将非常感谢。谢谢你们!

    更新:示例输出如下:

    字母:' abcdefghijklmnopqrstuvwxyz。,! '

    Key:' nu.t!iyvxqfl,bcjrodhkaew spzgm'

    输入明文:嘿,这真的很有趣!

    密文:' v! zmhvxdmxdmo!nll mikbg'

    解密文字:'嘿,这真的很有趣!'

1 个答案:

答案 0 :(得分:4)

以下原始答案

请向我们展示一些示例输入和输出示例。根据您的代码,我可以提出以下结果 - random.shuffle将所有内容随机播放并返回None,将您的makeKey更改为:

def makeKey(alphabet):
   alphabet = list(alphabet)
   random.shuffle(alphabet)
   return ''.join(alphabet)

编辑2

对于在加密/解密中未使用dict的方法,请参见下文:

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))

打印

Hey, this is really fun!
v! zmhvxdmxdmo!nll mikbg
hey, this is really fun!

修改

经过一些间距问题和实验后,我想出了这个相当简单的解决方案:

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))

打印

Hey, this is really fun!
v! zmhvxdmxdmo!nll mikbg
hey, this is really fun!