word_generater_py3.py 944 B

1234567891011121314151617181920212223242526272829303132
  1. # conding: utf-8
  2. import string
  3. import base64
  4. import random
  5. import sys
  6. def encodestr(s):
  7. salt1 = ''.join(random.sample(string.ascii_letters + string.digits, 5))
  8. salt2 = ''.join(random.sample(string.ascii_letters + string.digits, 8))
  9. ens = ''
  10. sens = str(base64.b64encode(s.encode('utf-8')), 'utf-8').strip()
  11. sens = sens.strip('=')
  12. for num, word in enumerate(sens):
  13. if num == 0:
  14. ens += salt1
  15. if num == len(sens) - 4 and len(sens) > 10:
  16. ens += salt2
  17. ens += word
  18. return ens
  19. def decodestr(s):
  20. dns = s[5:].replace(s[-12:-4],'') + '=='
  21. return str(base64.b64decode(dns.encode('utf-8')),'utf-8')
  22. if __name__ == "__main__":
  23. if len(sys.argv) != 3:
  24. print("usage: word_generater.py [decodestr]/[encodestr] [string]")
  25. if sys.argv[1] == "decode" :
  26. print(decodestr(sys.argv[2]))
  27. if sys.argv[1] == "encode" :
  28. print(encodestr(sys.argv[2]))