maroph

break_caesar.py

Dec 2nd, 2019
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. # decrypt the Caesar cipher by brute force
  2.  
  3. ciphertext = "hpwwozypjzfnclnvpoespnzopjzflcpyzhlyzqqtntlwnzopmcplvpcszhhzfwojzftxaczgpespnlpdlcntaspcezxlvpteslcopcezmcplvcplozyezespypiedepaezqtyozfe"
  4.  
  5. # Code #2 at https://www.geeksforgeeks.org/python-split-string-into-list-of-characters/
  6. def split(word):
  7.     return list(word)
  8.  
  9.  
  10. def decrypt(char, offset):
  11.     result = ''
  12.     if (char.isupper()):
  13.         result += chr((ord(char) - offset - 65) % 26 + 65)
  14.     else:
  15.         result += chr((ord(char) - offset - 97) % 26 + 97)
  16.     return result
  17.  
  18.  
  19. for i in range(1, 26):
  20.     print('offset :', i)
  21.     for c in split(ciphertext):
  22.         print(decrypt(c,i),end='')
  23.     print()
Advertisement
Add Comment
Please, Sign In to add comment