SHARE
TWEET

Untitled

a guest Mar 20th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import choice
  2. from copy import copy
  3. import os
  4.  
  5. letters = ['а','б','в','г','д','е','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я',' ']
  6.  
  7. def generate_key():
  8.     file = open("key.txt","a+")
  9.     dup = copy(letters)
  10.     for i in range(len(letters)):
  11.         random_letter = choice(dup)
  12.         file.write(letters[i] + random_letter + '\n')
  13.         dup.remove(random_letter)
  14.     file.close()
  15.     return
  16.  
  17. def read_key():
  18.     file = open("key.txt", "r")
  19.     keys = file.read().split('\n')[:-1]
  20.     return dict((key[0], key[1]) for key in keys)
  21.  
  22. def encryption():
  23.     filename = input()
  24.     keys = read_key()
  25.     text = open(filename, "r").read()
  26.     r = []
  27.     for i in range(len(text.strip())):
  28.         if text[i].lower() in letters:
  29.             r.append(keys[text[i].lower()])
  30.         else:
  31.             r.append(text[i])
  32.            
  33.     text = "".join(r)
  34.     file = open("encrypted.txt","w+")
  35.     file.write(text)
  36.     file.close()
  37.  
  38. def get_key(val, my_dict):
  39.     for key, value in my_dict.items():
  40.          if val == value:
  41.              return key
  42.          
  43.  
  44.     return val
  45.  
  46. def decrypt():
  47.     filename = input()
  48.     if os.path.exists(filename):
  49.         text = open(filename).read()
  50.         keys = read_key()
  51.         r = []
  52.         for i in range(len(text.strip())):
  53.             r.append(get_key(text[i], keys))
  54.         text = "".join(r)
  55.         print(text)
  56.         file = open("decrypted.txt","w+")
  57.         file.write(text)
  58.         file.close()
  59.     else:
  60.         print("No encrypt file")
  61.  
  62. def main():
  63.     command = input()
  64.     if command == 'encrypt':
  65.         generate_key()
  66.         encryption()
  67.     elif command == 'decrypt':
  68.         decrypt()
  69.     else:
  70.         print('Wrong command name')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top