Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Шифрование Цезаря
- def cipher_encrypt(plain_text, key):
- encrypted = ""
- for symbol in plain_text:
- if symbol.isupper():
- symbol_index = ord(symbol) - ord('A')
- symbol_shifted = (symbol_index + key) % 26 + ord('A')
- symbol_new = chr(symbol_shifted)
- encrypted += symbol_new
- elif symbol.islower():
- symbol_index = ord(symbol) - ord('a')
- symbol_shifted = (symbol_index + key) % 26 + ord('a')
- symbol_new = chr(symbol_shifted)
- encrypted += symbol_new
- elif symbol.isdigit():
- symbol_new = (int(symbol) + key) % 10
- encrypted += str(symbol_new)
- else:
- encrypted += symbol
- return encrypted
- # Дешифрование Цезаря
- def cipher_decrypt(ciphertext, key):
- decrypted = ""
- for symbol in ciphertext:
- if symbol.isupper():
- symbol_index = ord(symbol) - ord('A')
- symbol_og_pos = (symbol_index - key) % 26 + ord('A')
- symbol_og = chr(symbol_og_pos)
- decrypted += symbol_og
- elif symbol.islower():
- symbol_index = ord(symbol) - ord('a')
- symbol_og_pos = (symbol_index - key) % 26 + ord('a')
- symbol_og = chr(symbol_og_pos)
- decrypted += symbol_og
- elif symbol.isdigit():
- symbol_og = (int(symbol) - key) % 10
- decrypted += str(symbol_og)
- else:
- decrypted += symbol
- return decrypted
- #XOR
- def xor_cipher( str, key ):
- encripted_str = ""
- for letter in str:
- encripted_str += chr( ord(letter) ^ key )
- return encripted_str
- key = 10
- text = input("Сообщение для шифровки: ")
- offset = int(input('Шаг шифровки: '))
- cipher_text = cipher_encrypt(text, offset)
- print("Cipher encrypted:\t", cipher_text)
- encrypted_xor = xor_cipher(cipher_text, key )
- print( "XOR encript:\t", encrypted_xor )
- decripted_xor = xor_cipher(encrypted_xor, key )
- print( "XOR decript:\t", decripted_xor)
- decrypted_cipher = cipher_decrypt(decripted_xor, offset)
- print("Caesar decript:\t", decrypted_cipher)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement