word_generater.py 942 B

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