Training: Crypto - Caesar I
提示是凯撒密码,写了个凯撒密码加解密的Python程序,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| def encode(): message = input("输入明文:") n = int(input("输入密钥数字:")) translated = '' for i in message: if i == ' ': translated += ' ' else: translated += chr((ord(i)-65+n)%26+65) print(translated) def decode(): message = input("输入密文:") for key in range(26): translated = '' for i in message: if i == " ": translated += " " else: translated += chr((ord(i)-65-key)%26+65) print("密钥为%s,明文为:%s" % (key,translated)) mode = input("需要加密输入1,需要解密输入2:") if mode == "1": encode() elif mode == "2": decode() else: print("错误!")
|
输入CQN ZDRLT KAXFW OXG SDVYB XENA CQN UJIH MXP XO LJNBJA JWM HXDA DWRZDN BXUDCRXW RB LXMXXQRPRJUX后运行结果:
Training: Crypto - Transposition I
置换加密,根据前面的oWdnreuf.l推测这个单词为Wonderful.,由此推测出密钥为1032547698(解密)
写了一个简单的小程序进行解密:
1 2 3 4 5 6 7
| a = "oWdnreuf.lY uoc nar ae dht eemssga eaw yebttrew eh nht eelttre sra enic roertco drre . Ihtni koy uowlu dilekt oes eoyrup sawsro don:wd rodeapmomb.l" key = "1032547698" b = [] for i in range(len(a)): b.append(int(key[i%len(key)])+len(key)*(int(i/len(key)))) c = list(map(lambda x:a[x],b)) print(''.join(c))
|
运行结果为:
Training: Crypto - Caesar II
凯撒密码,密钥数量从26扩大到128
1 2 3 4 5 6 7 8 9 10 11 12
| def decode(message): for key in range(128): isRight = True translated = '' for i in message: translated += chr((key+i)%128) print("密钥为%s,明文为:%s" % (key,repr(translated))) message = "5A 02 02 77 20 7D 02 75 3F 20 0C 02 08 20 06 02 7F 09 78 77 20 02 01 78 20 00 02 05 78 20 76 7B 74 7F 7F 78 01 7A 78 20 7C 01 20 0C 02 08 05 20 7D 02 08 05 01 78 0C 41 20 67 7B 7C 06 20 02 01 78 20 0A 74 06 20 79 74 7C 05 7F 0C 20 78 74 06 0C 20 07 02 20 76 05 74 76 7E 41 20 6A 74 06 01 3A 07 20 7C 07 52 20 44 45 4B 20 7E 78 0C 06 20 7C 06 20 74 20 04 08 7C 07 78 20 06 00 74 7F 7F 20 7E 78 0C 06 03 74 76 78 3F 20 06 02 20 7C 07 20 06 7B 02 08 7F 77 01 3A 07 20 7B 74 09 78 20 07 74 7E 78 01 20 0C 02 08 20 07 02 02 20 7F 02 01 7A 20 07 02 20 77 78 76 05 0C 03 07 20 07 7B 7C 06 20 00 78 06 06 74 7A 78 41 20 6A 78 7F 7F 20 77 02 01 78 3F 20 0C 02 08 05 20 06 02 7F 08 07 7C 02 01 20 7C 06 20 78 7F 05 77 03 7C 7B 7B 74 01 74 7A 41" message = message.split(" ") a = [int(i,16) for i in message] decode(a)
|
为了在结果中直接输出\r而不转义,print时用了repr( )
repr(): 产生一个解释器易读的表达形式。
Training: Crypto - Substitution I
用https://quipqiup.com/