作者:郭彦伶克瑞雅书 | 来源:互联网 | 2021-08-31 00:12
#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('
以上就是python通过cython加密代码的详细内容,更多关于python cpython加密的资料请关注其它相关文章!