Advertisement
RMarK0

ciphers

Dec 14th, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # Шифрование Цезаря
  2. def cipher_encrypt(plain_text, key):
  3.     encrypted = ""
  4.     for symbol in plain_text:
  5.         if symbol.isupper():
  6.             symbol_index = ord(symbol) - ord('A')
  7.             symbol_shifted = (symbol_index + key) % 26 + ord('A')
  8.             symbol_new = chr(symbol_shifted)
  9.             encrypted += symbol_new
  10.         elif symbol.islower():
  11.             symbol_index = ord(symbol) - ord('a')
  12.             symbol_shifted = (symbol_index + key) % 26 + ord('a')
  13.             symbol_new = chr(symbol_shifted)
  14.             encrypted += symbol_new
  15.         elif symbol.isdigit():
  16.             symbol_new = (int(symbol) + key) % 10
  17.             encrypted += str(symbol_new)
  18.         else:
  19.             encrypted += symbol
  20.     return encrypted
  21.  
  22. # Дешифрование Цезаря
  23. def cipher_decrypt(ciphertext, key):
  24.     decrypted = ""
  25.     for symbol in ciphertext:
  26.         if symbol.isupper():
  27.             symbol_index = ord(symbol) - ord('A')
  28.             symbol_og_pos = (symbol_index - key) % 26 + ord('A')
  29.             symbol_og = chr(symbol_og_pos)
  30.             decrypted += symbol_og
  31.         elif symbol.islower():
  32.             symbol_index = ord(symbol) - ord('a')
  33.             symbol_og_pos = (symbol_index - key) % 26 + ord('a')
  34.             symbol_og = chr(symbol_og_pos)
  35.             decrypted += symbol_og
  36.  
  37.         elif symbol.isdigit():
  38.             symbol_og = (int(symbol) - key) % 10
  39.             decrypted += str(symbol_og)
  40.         else:
  41.             decrypted += symbol
  42.  
  43.     return decrypted
  44.  
  45. #XOR
  46. def xor_cipher( str, key ):
  47.     encripted_str = ""
  48.     for letter in str:
  49.         encripted_str += chr( ord(letter) ^ key )
  50.     return  encripted_str    
  51.  
  52.  
  53. key  = 10
  54.  
  55. text = input("Сообщение для шифровки: ")
  56. offset = int(input('Шаг шифровки: '))
  57.  
  58. cipher_text = cipher_encrypt(text, offset)
  59. print("Cipher encrypted:\t", cipher_text)
  60.  
  61. encrypted_xor = xor_cipher(cipher_text, key )
  62. print( "XOR encript:\t", encrypted_xor )
  63.  
  64. decripted_xor = xor_cipher(encrypted_xor, key )
  65. print( "XOR decript:\t",  decripted_xor)
  66.  
  67. decrypted_cipher = cipher_decrypt(decripted_xor, offset)
  68. print("Caesar decript:\t", decrypted_cipher)
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement