MorseCode摩斯密码编/解码脚本(python)

这是一个使用Python编写的摩斯密码编码解码工具,支持通过命令行直接运行。工具定义了摩斯密码字典,并提供了编码和解码函数。用户可以选择编码或解码模式,输入相应文本,程序会根据输入进行操作。此外,工具还具备一定的健壮性测试,确保输入的字符在支持范围内。
摘要由CSDN通过智能技术生成

一次小练手,做了一定的健壮性测试,暂未发现有较大的问题(除了代码逻辑又臭又长之外...)

使用argparse库实现脚本在cmd中直接使用,无需pycharm打开。

没有写从文件读取的功能,可以看我另一篇的base解码脚本中的文件IO实现。

 

import argparse
CODE = {
        ".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G",
        "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..": "L", "--": "M", "-.": "N",
        "---": "O", ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T",
        "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z",

        "-----": "0", ".----": "1", "..---": "2", "...--": "3", "....-": "4",
        ".....": "5", "-....": "6", "--...": "7", "---..": "8", "----.": "9",

        ".-.-.-": ".", "---...": ":", "--..--": ",", "-.-.-.": ";", "..--..": "?",
        "-...-": "=", ".----.": "'", "-..-.": "/", "-.-.--": "!", "-....-": "-",
        "..--.-": "_", ".-..-.": '"', "-.--.": "(", "-.--.-": ")", "...-..-": "$",
        ".-...": "&", ".--.-.": "@", ".-.-.": "+",
    }   #摩斯密码字典

#编码模块
def morse_encode(n):
    #n = input("[+]要编码的明文为: ").upper()
    inverted_code = {value:key for key,value in CODE.items()}
    codelist = []
    keylist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;?='/!-_\"()$&@+"
    #keylist = []
    #for i in inverted_code.keys():
    #    keylist.append(i)
    for i in n:
        codelist.append(str(inverted_code.get(i)) + " ")
    for i in codelist:
        if i == 'None ':
            print("Error!仅支持如下字符:\n" + ''.join(keylist))
            exit()
        else:
            continue
    print("[=]编码结果为:\n" + ''.join(codelist))

#解码模块
def morse_decode(n):
    #n = input("[+]要解码的密文为: ")
    codelist = []
    spl = input(r"[+]分隔符" + '("/"或空格)' + "为:")
    if len(spl) == 1:
        if ord(spl) == 32 or ord(spl) == 47:
            if spl not in n:
                print("[x]请检查分隔符是否正确!")
                print("[!]如果只翻译一个字符请在最后加空格,并将分隔符设为空格!")
                exit()
            else:
                s = str(n).split(spl)
            for i in s:
                codelist.append(str(CODE.get(i)))
            if codelist[0] == str(None):
                print("[x]请检查分隔符或密文是否正确")
                exit()
            else:
                if ord(n[-1]) == 32:
                    codelist.pop()
                print("[=]解码结果为:" + ''.join(codelist))
        else:
            print("[x]请输入"' / '"或 空格!")
            exit()
    elif len(spl) == 0:
        print("[x]请输入分隔符!")
        print("[!]如果只翻译一个字符请在最后加空格,并将分隔符设为空格!")
        exit()
    elif len(spl) > 1:
        print("[x]请输入一位分隔符!")
        exit()

#cmd调用函数
def morsecode(encode, decode, text):
    if encode:
        if text:
            n = text.upper()
            print("[+]要编码的明文为:" + n)
            morse_encode(n)
        else:
            morse_encode(input("[+]要编码的明文为: ").upper())
    elif decode:
        if text:
            n = text
            print("[+]要解码的密文为:" + n)
            morse_decode(n)
        else:
            morse_decode(input("[+]要解码的密文为:"))

#运用argparse方法实现cmd调用
paser = argparse.ArgumentParser(description='morsecode e&d')
paser.add_argument('--encode', '-e',nargs='?', const=1, help='调用encode模块')
paser.add_argument('--decode', '-d',nargs='?', const=1, help='调用decode模块')
paser.add_argument('--text', '-t',nargs='?', help='传入明文或密文,请用双引号框选字符串')
args = paser.parse_args()

morsecode(args.encode, args.decode, args.text)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值