cython加密代码python_python通过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字节长。长按键更安全。
:param in_filename: 输入的文件的名称
:param out_filename: 如果为none,将使用“.enc”。
:param chunksize: 设置函数用于读取和加密文件。大块一些文件和机器的大小可能更快。块大小必须可被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('
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: 目标文件夹路径
:param out_path: 压缩文件保存路径+xxxx.zip
:return: 无
"""
zip = zipfile.zipfile(out_path, "w", zipfile.zip_deflated)
for path, dirnames, filenames in os.walk(dir_path):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
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编写的代码,将.py文件转换成.so文件,达到加密的效果
python2 该代码用于加密python2编写的代码,将.py文件转换成.so文件,达到加密的效果
options:
-h, --help 显示帮助
-d, --directory 你需要加密的文件夹
-o, --operation 你所需要执行的操作,python3 or python2 or model
-f, --file 加密单个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 加密单个文件
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 加密单个文件
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)
cython加密代码python_python通过cython加密代码相关推荐
- cython加密代码python_利用Cython对python代码进行加密
利用Cython对python代码进行加密 Cython是属于PYTHON的超集,他首先会将PYTHON代码转化成C语言代码,然后通过c编译器生成可执行文件.优势:资源丰富,适合快速开发.翻译成C后速 ...
- cython编译python_Cython编译python为so 代码加密示例
1. 编译出来的so比网上流传的其他方法小很多. 2. language_level 是python的主版本号,如果python版本是2.x,目前的版本Cython需要人工指定language_le ...
- Java三行代码搞定MD5加密
Java三行代码搞定MD5加密 https://www.dexcoder.com/selfly/article/4026 public class MD5Test {public static voi ...
- cython python3_30倍!使用Cython加速Python代码
原标题:30倍!使用Cython加速Python代码 作者:George Seif.Thomas Wolf.Lukas Frei 编译:1+1=6 | 公众号海外部 前言 你可能经常会一次又一次地听到 ...
- java数据加密解密代码_java使用RSA加密方式实现数据加密解密的代码
RSA的应用 RSA是一种非对称加密算法.现在,很多登陆表单的密码的都采用RSA加密,例如京东中的登陆使用公钥对密码进行加密 java使用RSA加密方式实现数据加密解密,需要首先产生私钥和公钥 测试代 ...
- python加密敏感信息_仅需10行代码,使用python加密用户敏感数据
原标题:仅需10行代码,使用python加密用户敏感数据 数据分析师必须要遵守的一个规则就是数据保密,但在跨部门沟通的时候,难免会有数据泄露的情况,所以,对于用户的姓名.手机号.地址等敏感信息,一般需 ...
- 几维安全Javascript代码混淆(js加密)在线使用说明
2019独角兽企业重金招聘Python工程师标准>>> 几维安全Javascript代码混淆是一项在线加密服务,用户只需将JS文件打包成zip包,提交到加密平台,即可完成代码混淆.字 ...
- php自写代码加密,加密解密:教你加密自己写的VBS代码
今天在删自己的黑历史时,突然发现了几个VBS文件..发现了以前写的vbs代码加密器. [mb_view]手机上就别玩了 至于为什么你懂的[/mb_view] 首先,我们都知道,VBS代码可以直接被看到 ...
- 【小迪安全】Day04基础入门-30 余种加密编码进制Web数据库系统代码参数值
一.知识点 1.1存储密码加密-Web&数据库&系统 1.2传输数据编码-各类组合传输参数值 1.3代码特性加密-JS&PHP&NET&JAVA 1.4数据显示 ...
最新文章
- 2020秋季人工神经网络作业登记与批改
- 0x13链表与邻接表之邻值查找
- 关于jffs2文件系统如何掉电保护
- StringBuffer和StringBuild
- 这里有10个优质Python开源项目,来帮你学好Python
- spark代码连接hive_spark连接Hive
- 我的编程能力是从什么时候开始突飞猛进的?
- 中国开源燃烧!Zilliz 获全球开源基础软件最大单笔融资 4300 万美元
- gtool:操作genotype data的利器
- 高效能人士的七个习惯读后感与总结概括-(第五章)
- oracle18c安装教程6,Oracle 18c 安装详细过程(最全面)
- 三台路由器两台计算机华为,两台华为路由器怎样设置桥接 两台华为路由器设置桥接的方法...
- Exchange的邮箱创建与使用
- 信用卡还款怎么分期,还款还是要技巧的
- Android Notification取消声音 取消弹出
- 【ultraedit编辑器中怎么取消文件自动备份】
- 电子计算机奏出美妙的交响改为把字句,北师大版三年级下册语文的期中试卷
- 《夏目友人帐:结缘空蝉》-二丫影院在线观看
- 【牛客 错题集】Linux系统方面错题合集
- 五大常用算法——分支限界算法详解及经典例题