python Atbash密码在Python中实现。

Posted

tags:

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

import argparse

ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


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

    message = message.upper()

    encrypted_message = ''

    for c in message:
        if c not in ALPHABET:
            encrypted_message += c
            continue

        index_in_alphabet = ALPHABET.find(c)
        shifted_index = len(ALPHABET) - 1 - index_in_alphabet
        encrypted_message += ALPHABET[shifted_index]

    return encrypted_message


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

    message = message.upper()

    decrypted_message = ''

    for c in message:
        if c not in ALPHABET:
            decrypted_message += c
            continue

        index_in_alphabet = ALPHABET.find(c)
        shifted_index = len(ALPHABET) - 1 - index_in_alphabet
        decrypted_message += ALPHABET[shifted_index]

    return decrypted_message


def main():
    usage = "python atbash_cipher.py -e -m \"secret\"\nor\n \ " \
            "python atbash_cipher.py -d -m \"HVXIVG\""
    description = "Command line program for demonstrating the Atbash 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\"")

    options = parser.parse_args()

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


if __name__ == '__main__':
    main()

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

我可以在没有 POST 的情况下在 python 中实现 Web 用户身份验证系统吗?

python 在python中实现图形边缘/

python 在python中实现倒数第一个堆栈

在 Python 中实现装饰器模式

在python中实现类接口的正确方法是啥

python 在Python中实现经典的快速排序算法