run.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import base64
  4. import re
  5. import os
  6. from Crypto.Cipher import AES
  7. import binascii
  8. password = '!caicai20180315!' # 16,24,32位长的密码(密钥)
  9. BASEPATH = "./MSYConfig/source/base/MSYConfigString.h"
  10. TOPATH = "./MSYConfig/source/encrypt/MSYConfigString.h"
  11. def add_to_16(text):
  12. while len(text) % 16 != 0:
  13. text += '\0'
  14. return text
  15. def encrypt(data, password, isHex=0):
  16. if isinstance(password, str):
  17. password = password.encode('utf8')
  18. bs = AES.block_size
  19. pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
  20. cipher = AES.new(password, AES.MODE_ECB)
  21. data = cipher.encrypt(pad(data).encode('utf8'))
  22. if 1 == isHex:
  23. encrypt_data = binascii.b2a_hex(data) # 输出hex
  24. else:
  25. encrypt_data = base64.b64encode(data) # 取消注释,输出Base64格式
  26. return encrypt_data.decode('utf8')
  27. password = add_to_16(password)
  28. def encryptdo():
  29. os.remove(TOPATH)
  30. f=open(BASEPATH,'r')
  31. alllines=f.readlines()
  32. f.close()
  33. f=open(TOPATH,'w' ,encoding='utf8')
  34. for eachline in alllines:
  35. m = re.search( r'@"(.+)"', eachline, re.M|re.I)
  36. if m:
  37. old = m.group(1)
  38. val = str(base64.b64encode(old.encode("utf-8")), "utf-8")
  39. new = encrypt(val, password)
  40. writestr = eachline.replace(old,new,1)
  41. f.writelines(writestr)
  42. else:
  43. f.writelines(eachline)
  44. f.close()
  45. encryptdo()