Guest User

Untitled

a guest
Dec 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. """
  2. * Author: Abszoluto
  3. * Code made with Python 3.7.0
  4. * What does that do ?
  5. This is a code that will open a file and cryptograph its content
  6. The cryptograph method is Vigenere Cypher
  7. * Version 2.0
  8. Added more safe inverting the array wich contains the characters cryptographed (after using Vigenere Cypher)
  9. """
  10.  
  11. import io
  12.  
  13. key = "0DB20660AFDE446C5722CCF2F5256999" # I am using a hash to use as the key for Vigenere Cypher
  14. inputfile = "content.txt" # Change here the name of the file that you want to cryptograph
  15. outputfile = "cryptcontent.txt" # Change here the name of the file that you want to decypher
  16.  
  17. def code(filename):
  18.  
  19. """
  20. * This method will open a file, cryptograph its content and save it in a file 'cryptcontent.txt'
  21. """
  22.  
  23. characterAtKey = 0
  24. content = []
  25. file = open(inputfile,"r")
  26. with io.open(outputfile, "w", encoding="utf-8") as file2:
  27. while True:
  28. result = file.read(1) # If there isnt something to read, result will be 0
  29. if (not result):
  30. content = content[::-1] # Inverting the array with the cryptographed content
  31. for character in content:
  32. file2.write(chr(character)) # Write every single character at the file "filename"
  33. print("File coded sucessfully as 'cryptcontent.txt'.. \n")
  34. file.close()
  35. file2.close()
  36. break;
  37. content.append(ord(result)+ord(key[characterAtKey])) # adding the cryptographed character(value of (character) + value of (key[position]) ) at the array content
  38. characterAtKey = (characterAtKey + 1)%len(key) # next character at the key, the mod will go back at the first character when there isnt more characters to use
  39.  
  40.  
  41. def decode():
  42.  
  43. """
  44. * This method will open a file cryptographed, decipher its content and print it at the console
  45. """
  46.  
  47. decode = ""
  48. characterAtKey = 0
  49. with io.open(outputfile, "r", encoding="utf-8") as file:
  50. print("\nDECODING... \n")
  51. result = file.read() # reading the whole file
  52. result = result[::-1] # reversing the file content
  53. for charactere in result:
  54. decode = decode + (chr((ord(charactere)-ord(key[characterAtKey])))) # Concatenating the string decode with the character decypher
  55. characterAtKey = (characterAtKey + 1)%len(key) # next character at the key, the mod will go back at the first character when there isnt more characters to use
  56. file.close()
  57. print("FILE SUCESSFULLY DECODED: \n")
  58. print(decode)
  59.  
  60.  
  61. while True:
  62.  
  63. """
  64. * Main Menu
  65. * Type Code for code a file with the name content.txt
  66. * Type decode to decode a file with the name of "cryptcontent.txt"
  67. * Type anything else to leave
  68. """
  69.  
  70. option = input("Do you wanna 'code', 'decode' or 'leave' ? ")
  71. if option.lower() == "code":
  72. code(inputfile)
  73. elif option.lower() == "decode":
  74. decode()
  75. else:
  76. break
  77. input("Press any button to continue...")
Add Comment
Please, Sign In to add comment