introduced digraph reversal in case of same row/column

This commit is contained in:
Luxferre
2025-02-24 11:29:07 +02:00
parent 5eb44b10d6
commit 2fd14fc10c
2 changed files with 27 additions and 17 deletions
+9 -8
View File
@@ -83,8 +83,9 @@ def ipfencrypt(msg:str, key:str):
elif y1 == y2: # same row: get the coords to the right
x1 = (x1 + 1) % pfsize
x2 = (x2 + 1) % pfsize
enc.append(getgridchar(kg, x1, y1))
# swap the ciphertext character
enc.append(getgridchar(kg, x2, y2))
enc.append(getgridchar(kg, x1, y1))
# generate a random prefix
preflen = (len(key) % 10) + 1
prefix = ''
@@ -112,14 +113,14 @@ def ipfdecrypt(enc:str, key:str):
msg.append(getgridchar(kg, x2, y1))
else:
if x1 == x2: # same column: get the coords above
y1 -= 1
if y1 < 0:
y1 += pfsize
y2 -= 1
if y2 < 0:
y2 += pfsize
elif y1 == y2: # same row: get the coords to the left
x1 -= 1
if x1 < 0:
x1 += pfsize
msg.append(getgridchar(kg, x1, y1))
x2 -= 1
if x2 < 0:
x2 += pfsize
msg.append(getgridchar(kg, x2, y2))
# finalize the decrypted message
return ''.join(msg).strip()