123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import base64
- import re
- import os
- from Crypto.Cipher import AES
- import binascii
- password = '!caicai20180315!' # 16,24,32位长的密码(密钥)
- BASEPATH = "./MSYConfig/source/base/MSYConfigString.h"
- TOPATH = "./MSYConfig/source/encrypt/MSYConfigString.h"
- def add_to_16(text):
- while len(text) % 16 != 0:
- text += '\0'
- return text
- def encrypt(data, password, isHex=0):
- if isinstance(password, str):
- password = password.encode('utf8')
- bs = AES.block_size
- pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
- cipher = AES.new(password, AES.MODE_ECB)
- data = cipher.encrypt(pad(data).encode('utf8'))
- if 1 == isHex:
- encrypt_data = binascii.b2a_hex(data) # 输出hex
- else:
- encrypt_data = base64.b64encode(data) # 取消注释,输出Base64格式
- return encrypt_data.decode('utf8')
- password = add_to_16(password)
- def encryptdo():
- os.remove(TOPATH)
- f=open(BASEPATH,'r')
- alllines=f.readlines()
- f.close()
- f=open(TOPATH,'w' ,encoding='utf8')
- for eachline in alllines:
-
- m = re.search( r'@"(.+)"', eachline, re.M|re.I)
- if m:
- old = m.group(1)
- val = str(base64.b64encode(old.encode("utf-8")), "utf-8")
- new = encrypt(val, password)
- writestr = eachline.replace(old,new,1)
-
- f.writelines(writestr)
- else:
- f.writelines(eachline)
- f.close()
- encryptdo()
-
|