the_baahubali

Untitled

Mar 26th, 2022
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import enchant
  2.  
  3. print("\n\n");
  4. en = enchant.Dict("en_US")
  5.  
  6.  
  7. def caesar_multiple(plain_text, rot):
  8.     lower_text=plain_text.lower()
  9.     en_text=""
  10.     for char in lower_text:
  11.         new_ascii_char = ord(char)-rot    #subtracting the key from the ascii value #ord converts charachter to ascii value
  12.         if char == " ":
  13.             encrypt_char = " "
  14.         elif new_ascii_char>=97:
  15.             encrypt_char= chr(new_ascii_char)
  16.         else:
  17.             new_ascii_value= ord('z')-(ord('a')-new_ascii_char)+1
  18.             encrypt_char = chr(new_ascii_value)
  19.         en_text = en_text+encrypt_char        # encrypted text
  20.     return en_text
  21.  
  22.  
  23. for filenum in range(1,6):
  24.     filename = str(filenum) + ".txt"
  25.     file = open(filename)    #open file
  26.     text = file.read()      #read file
  27.     for rot in range(1,27):
  28.         decryption_found = True
  29.         decrypted = caesar_multiple(text, rot)
  30.        
  31.         for word in decrypted.split():
  32.             is_english_word = en.check(word)
  33.             if not is_english_word:
  34.                 decryption_found = False
  35.                 break
  36.                
  37.         if decryption_found:
  38.            
  39.             print("File: " + filename + " | Encrypted text: " + text + " | Decrypted text: " + decrypted + " | Key: " + str(rot))
  40.            
  41.             print("\n")
  42.             break
  43.        
  44.  
  45.        
Advertisement
Add Comment
Please, Sign In to add comment