mrAnderson33

Шифр сдвига

Jun 18th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. #Реализация шифрования / дешифрование простейшим шифром сдвига
  2. #Взлом шифра статистическим криптоанализом(только на русском)
  3.  
  4. #!/usr/bin/python3
  5.  
  6. import collections
  7.  
  8. def enCrypt(filename,key=3):
  9.     encrypte = ""
  10.     f = open(filename)
  11.     for char in f.read():
  12.         encrypte+=chr(ord(char) + key)
  13.     return encrypte
  14.  
  15. def deCrypt(encrypted,key =3):
  16.     decrypted = ""
  17.     for char in encrypted:
  18.         decrypted+=chr(ord(char)- key)
  19.     return decrypted
  20.  
  21. def mostFrequent (encrypted) :
  22.     charCount = {}
  23.     for char in encrypted:
  24.         if char != '#':
  25.             if char not in charCount:
  26.                 charCount[char] = 1
  27.             else:
  28.                 charCount[char] += 1
  29.     char_counter = collections.Counter(charCount).most_common(4)
  30.     return  char_counter
  31.  
  32. def tryGetKeys(mostReq):
  33.     keys =  []
  34.     for char in mostReq:
  35.         if ord(char[0]) > 0 : keys.append(ord(char[0]) - ord('о'))
  36.     return keys
  37.  
  38. encrypted = enCrypt("input")
  39. decrypted = deCrypt(encrypted)
  40. most_Frequent = mostFrequent(encrypted)
  41. keys = tryGetKeys(most_Frequent)
  42. for key in keys:
  43.     print(deCrypt(encrypted,key))
Advertisement
Add Comment
Please, Sign In to add comment