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 31 32 33 34
| def create_polybius_square(board): polybius_square = {} for i in range(len(board)): for j in range(len(board[i])): polybius_square[board[i][j]] = (i + 1, j + 1) return polybius_square
def decrypt_polybius(polybius_square, ciphertext): plaintext = "" ciphertext = ciphertext.replace(",", "") for i in range(0, len(ciphertext), 2): row = int(ciphertext[i]) col = int(ciphertext[i + 1]) for letter, position in polybius_square.items(): if position == (row, col): plaintext += letter break return plaintext
if __name__ == "__main__": chessboard = [['U', 'W', 'T', 'X', 'M'], ['A', 'S', 'Y', 'E', 'D'], ['O', 'R', 'F', 'N', 'L'], ['V', 'I', 'H', 'Z', 'Q'], ['P', 'B', 'K', 'G', 'C']] ciphertext = "13,43,42,22,33,35,21,54,21,21,21,21,21,21"
polybius_square = create_polybius_square(chessboard)
plaintext = decrypt_polybius(polybius_square, ciphertext) print("明文:", plaintext)
|