Skip to content

Commit 26d68fa

Browse files
committed
动态生成密钥对,对密码进行加密
1 parent 9a955ce commit 26d68fa

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

commons/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ def login(request):
4343
if request.session.get('username') and request.session.get('lock'):
4444
del request.session['lock']
4545
del request.session['username']
46-
return render(request, 'login.html')
46+
return render(request, 'login.html', {'public_key': crypt.gen_pri_pub_key})
4747
elif request.method == 'POST':
4848
username = request.POST.get('username')
4949
password = request.POST.get('password')
5050
de_password = crypt.de_js_encrypt(password)
51+
5152
login_ip = request.META.get('REMOTE_ADDR')
5253
# code = request.POST.get('code')
5354
remember_me = request.POST.get('remember_me')
@@ -134,7 +135,8 @@ def system_log(request):
134135
# datatables服务端分页
135136
@admin_auth
136137
def get_system_log(request):
137-
mongo = MongoOps(settings.MONGODB_HOST, settings.MONGODB_PORT, settings.RECORD_DB, settings.RECORD_COLL)
138+
mongo = MongoOps(settings.MONGODB_HOST, settings.MONGODB_PORT, settings.RECORD_DB, settings.RECORD_COLL,
139+
settings.MONGODB_USER, settings.MONGODB_PASS)
138140
draw = int(request.GET.get('draw')) # 记录操作次數
139141
start = int(request.GET.get('start')) # 起始位置
140142
length = int(request.GET.get('length')) # 每页长度

templates/login.html

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,11 @@
108108

109109
function encry_pwd(pwd) {
110110
let crypt = new JSEncrypt();
111-
crypt.setKey("-----BEGIN PUBLIC KEY-----\n" +
112-
"MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgGhvDNv1H/3EGeNcS+ju3Ytx7QlW\n" +
113-
"tvUi3KqV5U73md9G4Gyw+izQuvYYnZ/rDZrHocLB2fVcSZKKj8vNxkkpNqv00OJC\n" +
114-
"GbwzGcOtXOpsRpiY2qhd0kvRwglnN84h9kWq8C6oMe/eWwZkpsCQJMmtiHeHdzSU\n" +
115-
"xHkf4mpwxUfVDqF5AgMBAAE=\n" +
116-
"-----END PUBLIC KEY-----");
111+
crypt.setPublicKey(`{{ public_key }}`);
117112

118113
// Encrypt the data with the public key.
119114
return crypt.encrypt(pwd)
120115
}
121116
</script>
122117
</body>
123-
</html>
118+
</html>
-480 Bytes
Binary file not shown.

utils/crypt_pwd.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,31 @@ def decrypt_pwd(self, encrypted_pwd):
6363
decrypt_pwd = str(aes.decrypt(decrypt_base64), encoding='utf-8')
6464
return decrypt_pwd.rstrip()
6565

66+
@property
67+
def gen_pri_pub_key(self):
68+
rsa = RSA.generate(1024)
69+
private_key = rsa.exportKey().decode('utf8')
70+
with open('pri.pem', 'w') as f:
71+
f.write(private_key)
72+
public_key = rsa.publickey().exportKey().decode('utf8')
73+
return public_key
74+
6675
@staticmethod
6776
def de_js_encrypt(en_pwd):
6877
"""
6978
解密通过jsencrypt.js文件加密的密码
7079
:param en_pwd:
7180
:return:
7281
"""
73-
74-
key = """-----BEGIN RSA PRIVATE KEY-----
75-
MIICWwIBAAKBgGhvDNv1H/3EGeNcS+ju3Ytx7QlWtvUi3KqV5U73md9G4Gyw+izQ
76-
uvYYnZ/rDZrHocLB2fVcSZKKj8vNxkkpNqv00OJCGbwzGcOtXOpsRpiY2qhd0kvR
77-
wglnN84h9kWq8C6oMe/eWwZkpsCQJMmtiHeHdzSUxHkf4mpwxUfVDqF5AgMBAAEC
78-
gYBKjjUs9qN/JDejJCohQh4xxgSGLTzyZpAIzHhnVsaoKs5faj1AL0e6Fzq4hzMw
79-
M6LdCk2TJ+5ySq97vQz5AA5B5nf0y4zokCwYn5vdFVVtZeyiVeY0LYSpEvBS0xHY
80-
G3SJWLjMEtql2k1xJ8/1jvFkMx5SJKzruFFTvkRKn4bRAQJBANAuL2HZDjgcqgeF
81-
M7pQOKR+MxJzkN0hWBap1yJ5HQnUPqsEyBD3/BIcffCk2rQkfxnMT7gbnbiWVUd7
82-
/Ioh+FkCQQCAbClrx7wQW26JMjRqSBVyHWm1RoYDi0/USs7B9mct2KNGkPVr8dAB
83-
nm/glt5Rs9ay2QHbI103TjSPP938xs4hAkB6oup4utQcjA5B1d8uH3nutQVDFl89
84-
VRo+Z5j7jttjYev09SEileOhi7VJIORRgLp7KRfBPkuAZNciAFE50l8pAkBYE9bE
85-
yRQ+07aX+grg6ddrkKizX08Cl0WFAFmVxf02AGLbPwhTpGFY+uUYT+DigEk8GIGh
86-
XjvMdqKtrMv/VgqBAkEAjqQqCyiK1R9INladDpt8PjAJZecr1PLVdulyzlHvMm2l
87-
CHd72qhDwZUKVQck56xLGpPVPBGyyMl+cjRhZ+mnIQ==
88-
-----END RSA PRIVATE KEY-----"""
89-
90-
rsakey = RSA.importKey(key) # 导入私钥
82+
with open('pri.pem') as f:
83+
private_key = f.read()
84+
rsakey = RSA.importKey(private_key) # 导入私钥
9185
cipher = PKCS1_v1_5.new(rsakey) # 生成对象
86+
9287
missing_padding = len(en_pwd) % 4
9388
if missing_padding:
9489
en_pwd += '=' * (4 - missing_padding)
95-
text = cipher.decrypt(base64.b64decode(en_pwd.encode('utf-8')), "ERROR") # 将密文解密成明文,返回的是一个bytes类型数据,需要自己转换成str
90+
decode_data = base64.b64decode(en_pwd.encode('utf8'))
9691

92+
text = cipher.decrypt(decode_data, "ERROR") # 将密文解密成明文,返回的是一个bytes类型数据,需要自己转换成str
9793
return text.decode('utf-8')

0 commit comments

Comments
 (0)