Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import random
  2. import hashlib
  3. import os
  4. from datetime import datetime
  5. import getpass
  6.  
  7. def pretty_time(t):
  8.     return str(t.date()) + " " + str(t.hour) + "-" + str(t.minute) + "-" + str(t.second)
  9.  
  10. if not os.path.isfile("input.txt"):
  11.     open("input.txt", "w").write("Write your input here")
  12.     print("Please open the input.txt file")
  13.     exit()
  14.  
  15. input_text = open("input.txt", "r").read()
  16.  
  17. username = input("Username : ")
  18. password = getpass.getpass("Password : ")
  19.  
  20. key = hashlib.sha256( (username + "." + password).encode("utf-8") ).hexdigest()
  21.  
  22. print("\nInput : ")
  23. print(input_text, "\n")
  24.  
  25. print("Decode / Encode?")
  26. action = ""
  27. while action != "decode" and action != "encode":
  28.     action = input("Action : ").lower()
  29.     if action != "decode" and action != "encode":
  30.         print("Invalid action")
  31.  
  32. output = ""
  33. for i in input_text:
  34.     if ord(i) > 31 and ord(i) < 127:
  35.         value = ord(i) - 32
  36.         if action == "encode":
  37.             output += chr((value + int(key, 16)) % 95 + 32)
  38.         elif action == "decode":
  39.             output += chr( (value - int(key, 16)) % 95 + 32 )
  40.         key = hashlib.sha256(key.encode("utf-8")).hexdigest()
  41.  
  42. save_file = ""
  43. while save_file != "y" and save_file != "n":
  44.     save_file = input("Save output as a file? (Y/N) : ").lower()
  45.     if save_file != "y" and save_file != "n":
  46.         print("Invalid input")
  47.  
  48. if save_file == "y":
  49.         output_dir = pretty_time(datetime.now()) + ".txt"
  50.         output_file = open(output_dir, "w")
  51.         output_file.write(output)
  52.         print("Output saved at : " + output_dir)
  53. print(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement