单个字母的间距viginere ciph

2024-06-11 04:39:12 发布

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

试图在python中解释viginere密码中的空格/空白。这是单字母加密。你知道吗

请注意,我对python非常陌生!你知道吗

def encrypt(message, key):
    '''Vigenere encryption of message using key.'''

    # Converted to uppercase.
    # Non-alpha characters stripped out.
    message = filter(str.isalpha, message.upper())

    def enc(c, k):
        '''Single letter encryption.'''

        return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A'))

    return ''.join(starmap(enc, zip(message, cycle(key))))


def decrypt(message, key):
    '''Vigenere decryption of message using key.'''

    def dec(c, k):
        '''Single letter decryption.'''

        return chr(((ord(c) - ord(k) - 2 * ord('A')) % 26) + ord('A'))

    return ''.join(starmap(dec, zip(message, cycle(key))))

我已经试过了,但不起作用

def enc(c, k):
        '''Single letter encryption.'''
        if ord(c) == 32:
            return chr(ord(c))
        else:
            return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A'))

我现在的代码是这样的:

"Hello World" --> "JKJLJYUPLY" key = "wasup"

我想要:

"Hello world" --> "JKJLJ YUPLY" key "wasup"

Tags: ofkeymessagereturndefencryptionusingjoin
1条回答
网友
1楼 · 发布于 2024-06-11 04:39:12

我并没有真正记录自己的加密方法本身,但我测试它逐字逐句,它工作得很好:

to_encrypt = 'Hello World'
my_key = 'wasup'

encrypted = []

for word in to_encrypt.split():
    encrypted.append(encrypt(word, my_key))

print(' '.join(encrypted))

打印JKJLJ YUPLY

如果viginere密码应该是逐字应用的,那么这就是使用它的方法。你知道吗

相关问题 更多 >