如何计算python vigenere芯片中的空间

2024-06-11 05:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在为python中的一个类编写一个vigenere密码程序,它必须能够处理大写、小写和其他随机字符,但是其他随机字符将被忽略,下面的示例不会加密

python vigenere.py -e "This is my key"

hello world

altdw oapvh

This is my message that I am sending you!

Moqk qk yw wiqlhow bzmr S ek llvvqfs wyy!

我目前的问题是加密和解密都是我不知道如何让密码移位,所以下一个密钥在传递消息时,加密函数的代码是

#encrypt a plantext string
def encrypt(message, key):                                                                                   
# single letter encrpytion.                                                                
def enc(c,k):
    #checks to see if the character is a letter or another character
    if(not(c.isalpha())):
        return c
    else:
        #the actual encryption algorithm see https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher algebraeic description
        return chr((((ord(k) - 65) + (ord(c) - case(c))) % 26) + case(c))                              

#add single letters back together
return "".join(starmap(enc, zip(message, cycle(key))))         

如果有人有一个简单的方法做到这一点,并保持所有的空间和!如果不改变芯片的工作原理,请让我知道!在


Tags: key密码messagereturnismydefthis