word_generater.py 946 B

12345678910111213141516171819202122232425262728293031323334
  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 = base64.encodestring(s).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 base64.decodestring(dns)
  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])
  29. #print encodestr('6379')
  30. #print decodestr('dHbR7NjM3OQ')