python 用Python实现的Vigenere密码。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用Python实现的Vigenere密码。相关的知识,希望对你有一定的参考价值。

import argparse

ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


def encrypt(message, key):
    message = message.replace(':', '')
    message = message.replace(';', '')
    message = message.replace(',', '')
    message = message.replace('.', '')
    message = message.replace('?', '')
    message = message.replace('!', '')

    message = message.upper()

    encrypted_message = ''

    for i in range(len(message)):
        message_letter_index = ALPHABET.index(message[i])
        key_letter_index = ALPHABET.index(key[(i % len(key))].upper())
        index = (message_letter_index + key_letter_index) % len(ALPHABET)
        encrypted_message += ALPHABET[index]

    return encrypted_message


def decrypt(message, key):
    message = message.replace(':', '')
    message = message.replace(';', '')
    message = message.replace(',', '')
    message = message.replace('.', '')
    message = message.replace('?', '')
    message = message.replace('!', '')

    message = message.upper()

    decrypted_message = ''

    for i in range(len(message)):
        message_letter_index = ALPHABET.index(message[i])
        key_letter_index = ALPHABET.index(key[(i % len(key))].upper())
        index = (message_letter_index - key_letter_index) % len(ALPHABET)
        decrypted_message += ALPHABET[index]

    return decrypted_message


def main():
    usage = "python vigenere_cipher.py -e -m \"secret\"\nor\n \ " \
            "python vigenere_cipher.py -d -m \"CIABIR\""
    description = "Command line program for demonstrating the Vigenere cipher."
    parser = argparse.ArgumentParser(usage=usage, description=description)

    parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0')

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("-e", "--encrypt", action="store_true", dest="encrypt", help="Encrypts the plain text message.")
    group.add_argument("-d", "--decrypt", action="store_true", dest="decrypt", help="Decrypts the encrypted message.")

    parser.add_argument("-m", "--message", action="store", required=True,
                        dest="message", help="You need to specify the message. Like this: -m \"secret\"")

    parser.add_argument("-k", "--key", action="store", required=True, dest="key", help="You need to specify the key "
                                                                                       "as a positive number.")

    options = parser.parse_args()

    if options.encrypt:
        print("The encrypted message is: " + encrypt(options.message, options.key))
    elif options.decrypt:
        print("The decrypted message is: " + decrypt(options.message, options.key))


if __name__ == '__main__':
    main()

以上是关于python 用Python实现的Vigenere密码。的主要内容,如果未能解决你的问题,请参考以下文章

Python 中的 Vigenere 密码

Python:解码使用仿射密码编码的 Vigenere 密码

python中的Vigenere密码[关闭]

Python中的Vigenere密码不适用于大写/小写字母转换

Vigenere Cipher 没有错误消息 Python

python 简单的加密/解密算法(Vigenere密码)