Commit d55ed342 authored by Ivan Maslov's avatar Ivan Maslov

# Created + tested SafeSource!!! You can Create and run crypted .py code

parent 4e5dc748
......@@ -66,17 +66,14 @@ def decrypt_file_bytes(key, in_filename, chunksize=24*1024):
заданным ключом.
"""
lResult = b''
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, iv)
with open(out_filename, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
lResult = lResult+decryptor.decrypt(chunk)
lResult=lResult[0:origsize-1]
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
lResult = lResult+decryptor.decrypt(chunk)
lResult=lResult[0:origsize]
return lResult
\ No newline at end of file
......@@ -43,11 +43,13 @@ def Run():
lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat the key to protect source code').encode("utf-8")).digest()
if lKeyHashStr_1 == lKeyHashStr_2:
for lFileItem in lPyFileList:
#Create right \\ splashes
lFileItem = os.path.abspath(lFileItem)
Crypter.encrypt_file(lKeyHashStr_1, lFileItem, f"{lFileItem[0:-2]}{gOutCryptedExtension}")
#Remove old file
os.remove(lFileItem)
#Hotfix - dont encrypt __main__.py to __main__.cry - python dont see it
if True:#"__main__.py" not in lFileItem:
#Create right \\ splashes
lFileItem = os.path.abspath(lFileItem)
Crypter.encrypt_file(lKeyHashStr_1, lFileItem, f"{lFileItem[0:-2]}{gOutCryptedExtension}")
#Remove old file
os.remove(lFileItem)
else:
raise Exception("User set different secret key 1 and key 2")
############ Step 6 - Final stage
......
......@@ -5,6 +5,9 @@ import sys
import imp
import base64
from . import Crypter # Crypto functions
import datetime #Datetime
import hashlib #Get hash of the word
import pyautogui
EXT = '.cry'
# How to run
......@@ -87,7 +90,9 @@ class Base64Importer(object):
src = ''
return src
# for __main__
def get_code(self, inModName):
return self.get_source(inModName)
#---------------------------------------------------------------------------
def __collect_modules_info(self, root_package_path):
"""Собирает информацию о модулях из указанного пакета
......@@ -125,14 +130,31 @@ class Base64Importer(object):
gInUncryptedExtension = "py" # cry for filename.cry
gOutCryptedExtension = "cry" # cry for filename.cry
gFileMaskToDelete = "pyc" #Remove all .pyc files
#Settings to run crypted module
gSettings = {
#runpy
"run_module": {
"mod_name": "TestPackage", #"TestPackage",
"init_globals": None,
"run_name": None,
"alter_sys": True
}
}
# Create process run
def Run():
global gSettings
print(f"{str(datetime.datetime.now())}: Run decryptography")
############# Step 5 - Ask and confirm the secret word
lKeyHashStr_1 = hashlib.sha256(pyautogui.password('Please enter the key to protect source code').encode("utf-8")).digest()
lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat the key to protect source code').encode("utf-8")).digest()
if lKeyHashStr_1 == lKeyHashStr_2:
CryptographerInit("", inKey = lKeyHashStr_1)
sys.meta_path.append(Base64Importer("TestPackage",inKey = lKeyHashStr_1))
#CryptographerInit("", inKey = lKeyHashStr_1)
#Init the functions
import runpy
runpy.run_module(**gSettings["run_module"])
else:
raise Exception("User set different secret key 1 and key 2")
############ Step 6 - Final stage
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment