python——凯撒、栅栏、摩斯

凯撒

# 开发时间:2023/3/7 15:25
import pycipher
c=pycipher.Caesar(5).encipher('hello word')
print(c)
p=pycipher.Caesar(5).decipher(c)
print(p)
txt=input("cipher is:").strip()
for key in range(1,26):
    plain=""
    for i in txt:
        if i.isupper():
            plain+=chr(65+(ord(i)-65-key)%26)
        elif i.islower():
            plain+=chr(97+(ord(i)-97-key)%26)
        else:
            plain+=i
print(plain)
# 开发时间:2023/3/14 18:43
panduan = input('请输入一个数字1|2|3(1.解密|2.加密|3.退出):')
if panduan=='1':
    cipher = input('请输入你要解密的密文:').strip()
    for key in range(0,26):
        plain = ''
        for j in cipher:
            if j.isupper():
                plain+=chr(65+(ord(j)-65-key)%26)
            elif j.islower():
                plain+=chr(97+(ord(j)-97-key)%26)
            else:
                plain+=j
        # if 'flag' in plain:
        #     print(plain)
        print(plain)
elif panduan == '2':
    cipher=''
    plain = input('请输入你要加密的明文:').strip()
    key=int(input('请输入秘钥:'))
    for j in plain:
        if j.isupper():
            cipher += chr(65 + (ord(j) - 65 + key) % 26)
        elif j.islower():
            cipher += chr(97+(ord(j)-97+key)%26)
        else:
            cipher+=j
    print(cipher)
elif panduan == '3':
    exit('谢谢惠顾!')
else:
    print('请按要求输入!')

摩斯密码

# 开发时间:2023/3/7 14:29
txt="""0010 0100 01 110 1111011 11 11111 010 000 0 001101 1010 111 100 0 001101 01111 000 001101 00 10 1 0 010 0 000 1 01111 10 11110 101011 1111101
"""
if "." in txt or "-" in txt:
    table=str.maketrans("01",".-")#设置替换要求:零等于点,1等于-
    txt=txt.translate(table).split()#进行替换
else:
    txt=txt.split()
key={'01':'A','1000':'B','1010':'C','100':'D','0':'E','0010':'F','110':'G',
     '0000':'H','00':'I','0111':'J','101':'K','0100':'L','11':'M','10':'N',
     '111':'O','0110':'P','1101':'Q','010':'R','000':'S','1':'T','001':'U',
     '0001':'V','011':'W','1001':'X','1011':'Y','1100':'Z','01111':'1',
     '00111':'2','00011':'3','00001':'4','00000':'5','10000':'6','11000':'7',
     '11100':'8','11110':'9','11111':'0','001100':'?','10010':'/','101101':'()',
     '100001':'-','010101':'.','110011':',','011010':'@','111000':':',
     '101010':':','10001':'=','011110':"'",'101011':'!','001101':'_',
     '010010':'"','10110':'(','1111011':'{','1111101':'}'}
for i in txt:
    print(key.get(i),end="")
# 开发时间:2023/3/14 18:44
panduan=input('请输入一个数字1|2|3(1.解密|2.加密|3.退出):')
if panduan=='1':
    cipher = input('请输入你要解密的密文(可以是01表示的密文)):').strip()
    zi = {'01': 'A', '1000': 'B', '1010': 'C', '100': 'D', '0': 'E', '0010': 'F',
           '110': 'G', '0000': 'H', '00': 'I', '0111': 'J', '101': 'K', '0100': 'L',
           '11': 'M', '10': 'N', '111': 'O', '0110': 'P', '1101': 'Q', '010': 'R',
           '000': 'S', '1': 'T', '001': 'U', '0001': 'V', '011': 'W', '1001': 'X',
           '1011': 'Y', '1100': 'Z', '01111': '1', '00111': '2', '00011': '3',
           '00001': '4', '00000': '5', '10000': '6', '11000': '7', '11100': '8',
           '11110': '9', '11111': '0', '001100': '?', '10010': '/', '101101': '()',
           '100001': '-', '010101': '.', '110011': ',', '011010': '@', '111000': ':',
           '101010': ':', '10001': '=', '011110': "'", '101011': '!', '001101': '_',
           '010010': '"', '10110': '(', '1111011': '{', '1111101': '}'}
    if "." in cipher or '-' in cipher:
        txt=str.maketrans('.-','01')
        cipher=cipher.translate(txt).split()
    else:
        cipher=cipher.split()
    for i in cipher:
        print(zi[i],end='')
elif panduan=='2':
    plain=input('请输入你要加密的明文:').strip()
    codes = {'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': '----.',
             '.': '.-.-.-', ':': '---...', ',': '--..--', ';': '-.-.-.',
             '?': '..--..', '=': '-...-', '\'': '.----.', '/': '-..-.',
             '!': '-.-.--', '-': '-....-', '_': '..--.-', '"': '.-..-.',
             '(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '....',
             '@': '.--.-.', '+': '.-.-.'}
    for i in plain:
        code=codes.get(i.upper(),i)
        print(code,end='')
elif panduan=='3':
    exit('谢谢惠顾!')
else:
    print('请按要求输入!')

栅栏密码

def zhalan(txt,key):
    flag=""
    for i in range(key):
        for j in range(i,len(txt),key):
            flag+=txt[j]
    return flag
if __name__=='__main__':
    txt=input("请输入明文或者密文:".strip())
    for m in range(2,len(txt)):
        if len(txt)%m==0:
            flag = zhalan(txt, m)
            print(f'{m}栏:{flag}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值