76 lines
1.7 KiB
Python
76 lines
1.7 KiB
Python
|
|
import sys
|
||
|
|
import os
|
||
|
|
import shutil
|
||
|
|
|
||
|
|
encryptTypes = [ ".json", ".txt", ".text", ".xml", ".js", ".jsc", ".png", ".jpg", ".jpeg", ".tga", ".bmp" ]
|
||
|
|
|
||
|
|
class ffEncrypt(object):
|
||
|
|
|
||
|
|
def __init__(self, key ):
|
||
|
|
self.mKey = key
|
||
|
|
|
||
|
|
def build( self, res, dest ):
|
||
|
|
|
||
|
|
keyLength = len(self.mKey)
|
||
|
|
|
||
|
|
if None == self.mKey or 0 == keyLength:
|
||
|
|
return False
|
||
|
|
|
||
|
|
if not os.path.exists(res):
|
||
|
|
return False
|
||
|
|
|
||
|
|
s = res.lower().replace('\\', '/')
|
||
|
|
lastSplit = s.rfind('/')
|
||
|
|
if -1 != lastSplit:
|
||
|
|
s = s[lastSplit + 1:]
|
||
|
|
|
||
|
|
# Fmt Check
|
||
|
|
NeedEncrypt = False
|
||
|
|
for suffix in encryptTypes:
|
||
|
|
if s.endswith( suffix ):
|
||
|
|
NeedEncrypt = True
|
||
|
|
break
|
||
|
|
|
||
|
|
if not NeedEncrypt:
|
||
|
|
shutil.copy( res, dest )
|
||
|
|
return True
|
||
|
|
|
||
|
|
with open(res, 'rb') as f:
|
||
|
|
resBytes = f.read()
|
||
|
|
|
||
|
|
resBytesLen = len(resBytes)
|
||
|
|
if 0 == resBytesLen:
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
hashVal = ffEncrypt.get_hash(s)
|
||
|
|
hashAry = [
|
||
|
|
hashVal & 0xff,
|
||
|
|
(hashVal >> 8) & 0xff,
|
||
|
|
(hashVal >> 16) & 0xff,
|
||
|
|
(hashVal >> 24) & 0xff
|
||
|
|
]
|
||
|
|
|
||
|
|
realKey = bytearray()
|
||
|
|
for k in range(0,keyLength):
|
||
|
|
realKey.append( ord(self.mKey[k] ) ^ hashAry[ k % 4 ] )
|
||
|
|
|
||
|
|
BA = bytearray(resBytes)
|
||
|
|
|
||
|
|
for i in range(0,resBytesLen):
|
||
|
|
BA[i] = BA[i] ^ realKey[ i % keyLength]
|
||
|
|
|
||
|
|
with open(dest, 'wb') as f:
|
||
|
|
f.write(BA)
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_hash(src):
|
||
|
|
result = 0
|
||
|
|
srcLength = len(src)
|
||
|
|
for i in range(0,srcLength):
|
||
|
|
result = int(((result << 5) + result) + ord(src[i])) & 0xffffffff
|
||
|
|
return result
|
||
|
|
|