python Python中的空密码实现。

Posted

tags:

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

import argparse
import random

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

    word_substitutes = []

    for c in message:
        if c not in ALPHABET:
            continue

        index_of_plaintext = key % 5

        word_substitute = ""
        for i in range(0, 5):
            if i == index_of_plaintext:
                word_substitute += c
            else:
                random_letter_index = random.randint(0, len(ALPHABET) - 1)
                word_substitute += ALPHABET[random_letter_index]

        word_substitutes.append(word_substitute)

    return ' '.join(word_substitutes)


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

    words_in_message = message.split()

    decrypted_message = ""

    for word in words_in_message:
        decrypted_message += word[key % 5]

    return decrypted_message


def main():
    usage = "python null_cipher.py -e -m \"secret\"\nor\n \ " \
            "python null_cipher.py -d -m \"WESYL ZREFM VSCNJ HIRLG CQEKK CQTXX\""
    description = "Command line program for demonstrating the Null 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", type=int,
                        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中的空密码实现。的主要内容,如果未能解决你的问题,请参考以下文章

python - 如何使用列表中的键和Python中的空值初始化dict?

python 中的空值说明

附加到python中的空列表时出错[重复]

python删除list中的空list

python 去除HTML中的空标签对

Python,如何使用字典填充数据框中的空值