Play Nice
This commit is contained in:
1
play_nice/alphabet
Normal file
1
play_nice/alphabet
Normal file
@@ -0,0 +1 @@
|
||||
lsi28c14ot0vbf7nagh3mpjuxy5kwz6edqr9
|
||||
1
play_nice/enc
Normal file
1
play_nice/enc
Normal file
@@ -0,0 +1 @@
|
||||
1x5hqlod8x7oa88h0de1b5r6xja5sd
|
||||
1
play_nice/flag_enc
Normal file
1
play_nice/flag_enc
Normal file
@@ -0,0 +1 @@
|
||||
f391b621282ef5063ab2de93ab9e4bff
|
||||
56
play_nice/playfair.py
Executable file
56
play_nice/playfair.py
Executable file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/python3 -u
|
||||
import signal
|
||||
|
||||
SQUARE_SIZE = 6
|
||||
|
||||
def generate_square(alphabet):
|
||||
assert len(alphabet) == pow(SQUARE_SIZE, 2)
|
||||
matrix = []
|
||||
for i, letter in enumerate(alphabet):
|
||||
if i % SQUARE_SIZE == 0:
|
||||
row = []
|
||||
row.append(letter)
|
||||
if i % SQUARE_SIZE == (SQUARE_SIZE - 1):
|
||||
matrix.append(row)
|
||||
return matrix
|
||||
|
||||
def get_index(letter, matrix):
|
||||
for row in range(SQUARE_SIZE):
|
||||
for col in range(SQUARE_SIZE):
|
||||
if matrix[row][col] == letter:
|
||||
return (row, col)
|
||||
print("letter not found in matrix.")
|
||||
exit()
|
||||
|
||||
def encrypt_pair(pair, matrix):
|
||||
p1 = get_index(pair[0], matrix)
|
||||
p2 = get_index(pair[1], matrix)
|
||||
|
||||
if p1[0] == p2[0]:
|
||||
return matrix[p1[0]][(p1[1] + 1) % SQUARE_SIZE] + matrix[p2[0]][(p2[1] + 1) % SQUARE_SIZE]
|
||||
elif p1[1] == p2[1]:
|
||||
return matrix[(p1[0] + 1) % SQUARE_SIZE][p1[1]] + matrix[(p2[0] + 1) % SQUARE_SIZE][p2[1]]
|
||||
else:
|
||||
return matrix[p1[0]][p2[1]] + matrix[p2[0]][p1[1]]
|
||||
|
||||
def encrypt_string(s, matrix):
|
||||
result = ""
|
||||
if len(s) % 2 == 0:
|
||||
plain = s
|
||||
else:
|
||||
plain = s + "lsi28c14ot0vbf7nagh3mpjuxy5kwz6edqr9"[0]
|
||||
for i in range(0, len(plain), 2):
|
||||
result += encrypt_pair(plain[i:i + 2], matrix)
|
||||
return result
|
||||
|
||||
alphabet = open("key").read().rstrip()
|
||||
m = generate_square(alphabet)
|
||||
msg = open("msg").read().rstrip()
|
||||
enc_msg = encrypt_string(msg, m)
|
||||
print("Here is the alphabet: {}\nHere is the encrypted message: {}".format(alphabet, enc_msg))
|
||||
signal.alarm(18)
|
||||
resp = input("What is the plaintext message? ").rstrip()
|
||||
if resp and resp == msg:
|
||||
print("Congratulations! Here's the flag: {}".format(open("flag").read()))
|
||||
|
||||
# https://en.wikipedia.org/wiki/Playfair_cipher
|
||||
46
play_nice/sol.py
Executable file
46
play_nice/sol.py
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/home/maxime/.pyvenv/bin/python3
|
||||
|
||||
SQUARE_SIZE = 6
|
||||
|
||||
def generate_square(alphabet):
|
||||
assert len(alphabet) == pow(SQUARE_SIZE, 2)
|
||||
m = []
|
||||
for i, letter in enumerate(alphabet):
|
||||
if i % SQUARE_SIZE == 0:
|
||||
row = []
|
||||
row.append(letter)
|
||||
if i % SQUARE_SIZE == (SQUARE_SIZE - 1):
|
||||
m.append(row)
|
||||
return m
|
||||
|
||||
def get_index(letter, m):
|
||||
for row in range(SQUARE_SIZE):
|
||||
for col in range(SQUARE_SIZE):
|
||||
if m[row][col] == letter:
|
||||
return (row, col)
|
||||
print("letter not found in m.")
|
||||
exit()
|
||||
|
||||
def decode_pair(m, pair):
|
||||
p1 = get_index(pair[0], m)
|
||||
p2 = get_index(pair[1], m)
|
||||
|
||||
if p1[0] == p2[0]:
|
||||
return m[p1[0]][(p1[1] - 1) % SQUARE_SIZE] + m[p2[0]][(p2[1] - 1) % SQUARE_SIZE]
|
||||
elif p1[1] == p2[1]:
|
||||
return m[(p1[0] - 1) % SQUARE_SIZE][p1[1]] + m[(p2[0] - 1) % SQUARE_SIZE][p2[1]]
|
||||
else:
|
||||
return m[p1[0]][p2[1]] + m[p2[0]][p1[1]]
|
||||
|
||||
|
||||
with open("alphabet", 'r') as a, open("flag_enc", 'r') as e:
|
||||
m = generate_square(a.read().strip())
|
||||
enc = e.read().strip()
|
||||
assert len(enc) % 2 == 0
|
||||
|
||||
msg = ""
|
||||
for i in range(0, len(enc), 2):
|
||||
msg += decode_pair(m, enc[i:i+2])
|
||||
|
||||
print(msg)
|
||||
|
||||
Reference in New Issue
Block a user