maroph icon

break_caesar.py

maroph | PRO | 12/02/19 03:54:00 PM UTC | 0 ⭐ | 405 👁️ | Never ⏰ | []
Python |

680 B

|

None

|

0 👍

/

0 👎

# decrypt the Caesar cipher by brute force
 
ciphertext = "hpwwozypjzfnclnvpoespnzopjzflcpyzhlyzqqtntlwnzopmcplvpcszhhzfwojzftxaczgpespnlpdlcntaspcezxlvpteslcopcezmcplvcplozyezespypiedepaezqtyozfe"
 
# Code #2 at https://www.geeksforgeeks.org/python-split-string-into-list-of-characters/
def split(word):
    return list(word)
 
 
def decrypt(char, offset):
    result = ''
    if (char.isupper()):
        result += chr((ord(char) - offset - 65) % 26 + 65)
    else:
        result += chr((ord(char) - offset - 97) % 26 + 97)
    return result
 
 
for i in range(1, 26):
    print('offset :', i)
    for c in split(ciphertext):
        print(decrypt(c,i),end='')
    print()

Comments