最近想把word密码文件的服务器密码信息归档到mysql数据库,心想着如果直接在里面写明文密码会不会不安全,如果用sha这些不可逆的算法又没法还原回来,所以自己就想着用Python写一个小代码,先把明文密码加密之后再存mysql表中。下面贴出我的Python代码:
首先是加密encript.py
# coding:utf-8 def encrpt(s): length = len(s) j = "" for i in s: if ord(i) <= 49: i = chr(ord(i) + length) j = j + i elif ord(i) > 81 and ord(i) <= 126: i = chr(ord(i) - length) j = j + i else: j = j + chr(32) + i return j s = raw_input("请输入6到16位的密码:") enscript_s = "" if len(s) < 6 or len(s)>16: print ("密码长度不符合") if len(s) == 0: print ("密码不能为空") for i in s: if ord(i) < 33 and ord(i) > 126: print ("非法字符") if len(s) >= 6 and len(s) <= 16: enscript_s = encrpt(s) print enscript_s