Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Реализация шифрования / дешифрование простейшим шифром сдвига
- #Взлом шифра статистическим криптоанализом(только на русском)
- #!/usr/bin/python3
- import collections
- def enCrypt(filename,key=3):
- encrypte = ""
- f = open(filename)
- for char in f.read():
- encrypte+=chr(ord(char) + key)
- return encrypte
- def deCrypt(encrypted,key =3):
- decrypted = ""
- for char in encrypted:
- decrypted+=chr(ord(char)- key)
- return decrypted
- def mostFrequent (encrypted) :
- charCount = {}
- for char in encrypted:
- if char != '#':
- if char not in charCount:
- charCount[char] = 1
- else:
- charCount[char] += 1
- char_counter = collections.Counter(charCount).most_common(4)
- return char_counter
- def tryGetKeys(mostReq):
- keys = []
- for char in mostReq:
- if ord(char[0]) > 0 : keys.append(ord(char[0]) - ord('о'))
- return keys
- encrypted = enCrypt("input")
- decrypted = deCrypt(encrypted)
- most_Frequent = mostFrequent(encrypted)
- keys = tryGetKeys(most_Frequent)
- for key in keys:
- print(deCrypt(encrypted,key))
Advertisement
Add Comment
Please, Sign In to add comment