# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import base64
import sys
AES_LENGTH = 16
class prpcrypt():
def __init__(self, key):
self.key = key
self.mode = AES.MODE_ECB
self.cryptor = AES.new(self.pad_key(self.key).encode(), self.mode)
# 加密函数,如果text不是16的倍数【加密文本text必须为16的倍数!】,那就补足为16的倍数
# 加密内容需要长达16位字符,所以进行空格拼接
def pad(self,text):
while len(text) % AES_LENGTH != 0:
text += ' '
return text
# 加密密钥需要长达16位字符,所以进行空格拼接
def pad_key(self,key):
while len(key) % AES_LENGTH != 0:
key += ' '
return key
def encrypt(self, text):
# 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
# 加密的字符需要转换为bytes
# print(self.pad(text))
self.ciphertext = self.cryptor.encrypt(self.pad(text).encode())
# 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
# 所以这里统一把加密后的字符串转化为16进制字符串
return b2a_hex(self.ciphertext)
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
plain_text = self.cryptor.decrypt(a2b_hex(text)).decode()
return plain_text.rstrip(' ')
def encrypt_py(self, from_file, to_file):
with open(to_file, 'w') as f:
f.write(self.encrypt(base64.b64encode(open(from_file, 'r', encoding='utf8').read().encode()).decode()).decode())
def exec(self, file):
return base64.b64decode(self.decrypt(open(file, 'r', encoding='utf8').read().encode()))
if __name__ == '__main__':
argvs = {
'key': sys.argv[1],
}
pc = prpcrypt(argvs['key']) # 初始化密钥
pc.encrypt_py('main.py', 'main.encrypt.py')
exec(pc.exec('main.encrypt.py'))
墨之科技,版权所有 © Copyright 2017-2027
湘ICP备14012786号 邮箱:ai@inksci.com