2023.10.25
密码简介
-
是棋盘密码的变种,它的密码表是关于关键字而变化的
-
密码表的形成是现在5*5的矩阵中先填入关键字(去重),再依次填入a~z不重复的字母,i和j同格
加密过程
设关键字是key
则密码表为:
1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|
1 | k | e | y | a | b |
2 | c | d | f | g | h |
3 | i/j | l | m | n | o |
4 | p | q | r | s | t |
5 | u | v | w | x | z |
在此情况下加密flag
则密文为 23 32 14 24
def nihilist_decode(ciphertext):
ciphertext += ' '
while True:
key = input('请输入关键字:\n')
if key.isalpha() : break
else : print('字符错误,请重输')
alphabet , checkerboard , text , list_1 = 'abcdefghijklmnopqrstuvwxyz' , '' , '' , []
for i in key+alphabet:
if i not in checkerboard:
if i == 'i' and 'j' in checkerboard or i == 'j' and 'i' in checkerboard:continue
else:checkerboard += i
checkerboard = [checkerboard[:5],checkerboard[5:10],checkerboard[10:15],checkerboard[15:20],checkerboard[20:]]
i = 0
while i < len(ciphertext):
if ciphertext[i] in '12345' and ciphertext[i+1] in '12345':
list_1.append(ciphertext[i:i+2])
i += 1
else:list_1.append(ciphertext[i])
i += 1
for i in list_1:
if len(i) == 2:text += checkerboard[int(i[0])-1][int(i[1])-1]
else:text += i
return text