This commit is contained in:
yumoqing 2019-08-12 11:55:18 +08:00
parent 60476e2c1f
commit 87ad573a80
2 changed files with 16 additions and 22 deletions

View File

@ -24,7 +24,6 @@ class MyTemplateEngine:
'type':type,
'isNone':isNone,
'len':len,
'recordFind':recordFind,
'render':self.render,
'renders':self.renders,
'ArgsConvert':ArgsConvert,
@ -36,13 +35,12 @@ class MyTemplateEngine:
}
self.env.globals.update(denv)
def setGlobal(self,dic):
self.env.globals.update(dic)
def set(self,k,v):
self.env.globals.update({k:v})
def _render(self,template,data):
self._setEnv()
uRet = template.render(**data)
return uRet.encode(self.out_coding)
return uRet
def renders(self,tmplstring,data):
def getGlobal():
@ -59,7 +57,6 @@ class MyTemplateEngine:
return self._render(template,data)
def renderJsonFile(self,tmplfile,jsonfile):
f = codecs.open(jsonfile,"r",self.file_coding)
data = json.load(f)
f.close()
return self.render(tmplfile,data)
with codecs.open(jsonfile,"r",self.file_coding) as f:
data = json.load(f)
return self.render(tmplfile,data)

View File

@ -3,10 +3,9 @@ import random, base64
from hashlib import sha1
class RC4:
def __init__(self,key='1234567890',data_coding='utf8'):
def __init__(self,data_coding='utf8'):
self.bcoding = 'iso-8859-1'
self.dcoding = data_coding
self.key = key.encode(self.bcoding)
self.salt = b'AFUqx9WZuI32lnHk'
def _crypt(self,data,key):
@ -28,13 +27,10 @@ class RC4:
return ''.join(out).encode(self.bcoding)
def encode(self,data, encode=base64.b64encode, salt_length=16):
def encode(self,data, key,encode=base64.b64encode, salt_length=16):
"""RC4 encryption with random salt and final encoding"""
#salt = ''
#for n in range(salt_length):
# salt += chr(random.randrange(256))
#salt = salt.encode(self.bcoding)
a = sha1(self.key + self.salt)
akey = key.encode(self.bcoding)
a = sha1(akey + self.salt)
k = a.digest()
data = self.salt + self._crypt(data, k)
@ -42,12 +38,13 @@ class RC4:
data = encode(data)
return data.decode(self.dcoding)
def decode(self,data, decode=base64.b64decode, salt_length=16):
def decode(self,data, key,decode=base64.b64decode, salt_length=16):
"""RC4 decryption of encoded data"""
akey = key.encode(self.bcoding)
if decode:
data = decode(data)
salt = data[:salt_length]
a = sha1(self.key + self.salt)
a = sha1(akey + self.salt)
k = a.digest() #.decode('iso-8859-1')
r = self._crypt(data[salt_length:], k)
return r.decode(self.dcoding)
@ -58,11 +55,11 @@ if __name__=='__main__':
# 密钥
data="231r3 feregrenerjk gkht324g8924gnfw k;ejkvwkjerv"
key = '123456'
rc4 = RC4(key)
rc4 = RC4()
print(data)
# 加码
encoded_data = rc4.encode(data)
encoded_data = rc4.encode(data,key)
print(encoded_data,len(encoded_data) )
# 解码
decoded_data = rc4.decode(encoded_data)
decoded_data = rc4.decode(encoded_data,key)
print(decoded_data)