最近想把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
注意,我这里限定了加密6到16位密码,下面说说我的思路:
首先,输入6到16位密码,然后判断这些密码符不符合规定的字符,当然我认为这里我的判定还不够完善,ord函数获取每一个字符对应的十进制ASCII码。大家可以翻看ASCII码表,ASCII码十进制33到126,包含了大小写字母,数字和