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

dev-linux
Ivan Maslov 5 years ago
parent 4e5dc7485e
commit d55ed34256

@ -66,17 +66,14 @@ def decrypt_file_bytes(key, in_filename, chunksize=24*1024):
заданным ключом. заданным ключом.
""" """
lResult = b'' lResult = b''
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
with open(in_filename, 'rb') as infile: with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0] origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16) iv = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, iv) decryptor = AES.new(key, AES.MODE_CBC, iv)
with open(out_filename, 'wb') as outfile: while True:
while True: chunk = infile.read(chunksize)
chunk = infile.read(chunksize) if len(chunk) == 0:
if len(chunk) == 0: break
break lResult = lResult+decryptor.decrypt(chunk)
lResult = lResult+decryptor.decrypt(chunk) lResult=lResult[0:origsize]
lResult=lResult[0:origsize-1]
return lResult return lResult

@ -43,11 +43,13 @@ def Run():
lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat 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: if lKeyHashStr_1 == lKeyHashStr_2:
for lFileItem in lPyFileList: for lFileItem in lPyFileList:
#Create right \\ splashes #Hotfix - dont encrypt __main__.py to __main__.cry - python dont see it
lFileItem = os.path.abspath(lFileItem) if True:#"__main__.py" not in lFileItem:
Crypter.encrypt_file(lKeyHashStr_1, lFileItem, f"{lFileItem[0:-2]}{gOutCryptedExtension}") #Create right \\ splashes
#Remove old file lFileItem = os.path.abspath(lFileItem)
os.remove(lFileItem) Crypter.encrypt_file(lKeyHashStr_1, lFileItem, f"{lFileItem[0:-2]}{gOutCryptedExtension}")
#Remove old file
os.remove(lFileItem)
else: else:
raise Exception("User set different secret key 1 and key 2") raise Exception("User set different secret key 1 and key 2")
############ Step 6 - Final stage ############ Step 6 - Final stage

@ -5,6 +5,9 @@ import sys
import imp import imp
import base64 import base64
from . import Crypter # Crypto functions from . import Crypter # Crypto functions
import datetime #Datetime
import hashlib #Get hash of the word
import pyautogui
EXT = '.cry' EXT = '.cry'
# How to run # How to run
@ -87,7 +90,9 @@ class Base64Importer(object):
src = '' src = ''
return src return src
# for __main__
def get_code(self, inModName):
return self.get_source(inModName)
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def __collect_modules_info(self, root_package_path): def __collect_modules_info(self, root_package_path):
"""Собирает информацию о модулях из указанного пакета """Собирает информацию о модулях из указанного пакета
@ -125,14 +130,31 @@ class Base64Importer(object):
gInUncryptedExtension = "py" # cry for filename.cry gInUncryptedExtension = "py" # cry for filename.cry
gOutCryptedExtension = "cry" # cry for filename.cry gOutCryptedExtension = "cry" # cry for filename.cry
gFileMaskToDelete = "pyc" #Remove all .pyc files 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 # Create process run
def Run(): def Run():
global gSettings
print(f"{str(datetime.datetime.now())}: Run decryptography") print(f"{str(datetime.datetime.now())}: Run decryptography")
############# Step 5 - Ask and confirm the secret word ############# 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_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() lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat the key to protect source code').encode("utf-8")).digest()
if lKeyHashStr_1 == lKeyHashStr_2: 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: else:
raise Exception("User set different secret key 1 and key 2") raise Exception("User set different secret key 1 and key 2")
############ Step 6 - Final stage ############ Step 6 - Final stage

Loading…
Cancel
Save