python通過(guò)cython加密代碼
#coding=utf-8 import getopt import os, sys import zipfile from Crypto.Cipher import AES import random, struct #加密python3的代碼 def transfer3(dir_pref): os.system('cython -2 %s.py;' 'gcc -c -fPIC -I/usr/include/python3.5/ %s.c -o %s.o' % (dir_pref, dir_pref, dir_pref)) os.system('gcc -shared %s.o -o %s.so' % (dir_pref, dir_pref)) if clear: os.system('rm -f %s.c %s.o %s.py' % (dir_pref, dir_pref, dir_pref)) else: os.system('rm -f %s.c %s.o' % (dir_pref, dir_pref)) #加密python2的代碼 def transfer2(dir_pref): os.system('cython -2 %s.py;' 'gcc -c -fPIC -I/usr/include/python2.7/ %s.c -o %s.o' % (dir_pref, dir_pref, dir_pref)) os.system('gcc -shared %s.o -o %s.so' % (dir_pref, dir_pref)) if clear: os.system('rm -f %s.c %s.o %s.py' % (dir_pref, dir_pref, dir_pref)) else: os.system('rm -f %s.c %s.o' % (dir_pref, dir_pref)) #加密AI模型 def encrypt_file(in_filename, out_filename=None, chunksize=64*1024): """ 使用AES(CBC模式)加密文件給定的密鑰。 :param key: 加密密鑰-必須是16、24或32字節(jié)長(zhǎng)。長(zhǎng)按鍵更安全。 :param in_filename: 輸入的文件的名稱 :param out_filename: 如果為None,將使用“<in_filename>.enc”。 :param chunksize: 設(shè)置函數(shù)用于讀取和加密文件。大塊一些文件和機(jī)器的大小可能更快。塊大小必須可被16整除。 :return: None """ if not out_filename: out_filename = in_filename + '.enc' salt = '' # 鹽值 key = "{: <32}".format(salt).encode("utf-8") #iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16)) #encryptor = AES.new(key, AES.MODE_CBC, iv) iv = b'0000000000000000' encryptor = AES.new(key, AES.MODE_CBC, iv) filesize = os.path.getsize(in_filename) with open(in_filename, 'rb') as infile: with open(out_filename, 'wb') as outfile: outfile.write(struct.pack('<Q', filesize)) outfile.write(iv) while True: chunk = infile.read(chunksize) if len(chunk) == 0: break elif len(chunk) % 16 != 0: chunk += (' ' * (16 - len(chunk) % 16)).encode("utf-8") outfile.write(encryptor.encrypt(chunk)) def zip_dir(dir_path,out_path): """ 壓縮指定文件夾 :param dir_path: 目標(biāo)文件夾路徑 :param out_path: 壓縮文件保存路徑+xxxx.zip :return: 無(wú) """ zip = zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) for path, dirnames, filenames in os.walk(dir_path): # 去掉目標(biāo)跟路徑,只對(duì)目標(biāo)文件夾下邊的文件及文件夾進(jìn)行壓縮 fpath = path.replace(dir_path, '') for filename in filenames: zip.write(os.path.join(path, filename), os.path.join(fpath, filename)) zip.close() if __name__ == '__main__': help_show = ''' python version: python3 該代碼用于加密python3編寫(xiě)的代碼,將.py文件轉(zhuǎn)換成.so文件,達(dá)到加密的效果 python2 該代碼用于加密python2編寫(xiě)的代碼,將.py文件轉(zhuǎn)換成.so文件,達(dá)到加密的效果 Options: -h, --help 顯示幫助 -d, --directory 你需要加密的文件夾 -o, --operation 你所需要執(zhí)行的操作,python3 or python2 or model -f, --file 加密單個(gè)py文件 -c, --clear 刪除原始的py文件 -m, --maintain 列出你不需要加密的文件和文件夾,如果是文件夾的話需要加[] 例子: -m __init__.py,setup.py,[poc,resource,venv,interface] -z, --zip 加密之后壓縮文件 Example: python setup.py -f test_file.py -o python2 加密單個(gè)文件 python setup.py -d test_dir -o python2 -m __init__.py,setup.py,[poc/,resource/,venv/,interface/] -c 加密文件夾 python3 setup.py -f test_file.py -o python3 加密單個(gè)文件 python3 setup.py -d test_dir -o python3 -m __init__.py,setup.py,[poc/,resource/,venv/,interface/] -c 加密文件夾 ''' clear = 0 is_zip = False root_name = '' operation = '' file_name = '' m_list = '' try: options,args = getopt.getopt(sys.argv[1:],"vh:d:f:cm:o:z:",["version","help","directory=","file=","operation=","zip","clear","maintain="]) except getopt.GetoptError: print(help_show) sys.exit(1) for key, value in options: if key in ['-h', '--help']: print(help_show) elif key in ['-c', '--clear']: clear = 1 elif key in ['-d', '--directory']: root_name = value elif key in ['-f', '--file']: file_name = value elif key in ['-o', '--operation']: operation = value elif key in ['-z','--zip']: is_zip = True elif key in ['-m', '--maintain']: m_list = value file_flag = 0 dir_flag = 0 if m_list.find(',[') != -1: tmp = m_list.split(',[') file_list = tmp[0] dir_list = tmp[1:-1] file_flag = 1 dir_flag = 1 elif m_list.find('[') != -1: dir_list = m_list[1:-1] dir_flag = 1 else: file_list = m_list.split(',') file_flag = 1 if dir_flag == 1: dir_tmp = dir_list.split(',') dir_list=[] for d in dir_tmp: if d.startswith('./'): dir_list.append(d[2:]) else: dir_list.append(d) if root_name != '': if not os.path.exists(root_name): print('No such Directory, please check or use the Absolute Path') sys.exit(1) if os.path.exists('%s_old' % root_name): os.system('rm -rf %s_old' % root_name) #os.system('cp -r %s %s_old' % (root_name, root_name)) #備份源文件 try: for root, dirs, files in os.walk(root_name): for file in files: if m_list != '': skip_flag = 0 if dir_flag == 1: for dir in dir_list: if (root+'/').startswith(root_name + '/' + dir): skip_flag = 1 break if skip_flag: continue if file_flag == 1: if file in file_list: continue pref = file.split('.')[0] dir_pref = root + '/' + pref if file.endswith('.pyc'): os.system('rm -f %s' % dir_pref+'.pyc') elif file.endswith('.so'): pass elif file.endswith('.py'): if operation == 'python3': transfer3(dir_pref) elif operation == 'python2': transfer2(dir_pref) else: pass except Exception as e: print(e) if file_name != '': try: dir_pref = file_name.split('.')[0] if operation == 'python3': transfer3(dir_pref) elif operation == 'python2': transfer2(dir_pref) else: pass except Exception as e: print(e) if is_zip: zip_dir(root_name,root_name+'.zip') if operation == 'model': encrypt_file(root_name)
以上就是python通過(guò)cython加密代碼的詳細(xì)內(nèi)容,更多關(guān)于python cpython加密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python語(yǔ)音合成之第三方庫(kù)gTTs/pyttsx3/speech橫評(píng)(內(nèi)附使用方法)
Python是一種非常強(qiáng)大的腳本語(yǔ)言,可以用來(lái)實(shí)現(xiàn)各種復(fù)雜的應(yīng)用,其中之一就是文本轉(zhuǎn)語(yǔ)音,即把文字轉(zhuǎn)換成聲音來(lái)發(fā)出,下面這篇文章主要給大家介紹了關(guān)于Python語(yǔ)音合成之第三方庫(kù)gTTs/pyttsx3/speech橫評(píng)的相關(guān)資料,文中還介紹了詳細(xì)的使用方法,需要的朋友可以參考下2023-05-05基于Python實(shí)現(xiàn)智能停車場(chǎng)車牌識(shí)別計(jì)費(fèi)系統(tǒng)
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)一個(gè)智能停車場(chǎng)車牌識(shí)別計(jì)費(fèi)系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-04-04python 實(shí)現(xiàn)存儲(chǔ)數(shù)據(jù)到txt和pdf文檔及亂碼問(wèn)題的解決
這篇文章主要介紹了python 實(shí)現(xiàn)存儲(chǔ)數(shù)據(jù)到txt和pdf文檔及亂碼問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Python Pandas常用函數(shù)方法總結(jié)
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著Pandas常用函數(shù)方法展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06簡(jiǎn)單了解Django ORM常用字段類型及參數(shù)配置
這篇文章主要介紹了簡(jiǎn)單了解Django ORM常用字段類型及參數(shù)配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Python中的優(yōu)先隊(duì)列(priority?queue)和堆(heap)
這篇文章主要介紹了Python中的優(yōu)先隊(duì)列(priority?queue)和堆(heap),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例
今天小編就為大家分享一篇python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11