Jelajahi Sumber

设备密码使用密文

locky 4 tahun lalu
induk
melakukan
de7d544fd2
2 mengubah file dengan 43 tambahan dan 2 penghapusan
  1. 3 2
      Controller/EquipmentManager.py
  2. 40 0
      Service/ModelService.py

+ 3 - 2
Controller/EquipmentManager.py

@@ -370,6 +370,7 @@ def addInterface(request):
     version = request_dict.get('version', '')
     isCheckMainUser = request_dict.get('isCheckMainUser', None)
     isMainUserExists = False
+    encrypt_password = ModelService.encrypt_pwd(View_Password)
     if all([UID, NickName, View_Account, Type, ChannelIndex]):
         tko = TokenObject(token)
         response.lang = tko.lang
@@ -497,9 +498,9 @@ def addInterface(request):
                                 for uid_channel_set in uid_channel_set_qs:
                                     data_list.append(
                                         {'userID': userID, 'UID': UID, 'uid_nick': uid_channel_set['channel_name'],
-                                         'channel': uid_channel_set['channel'], 'password': View_Password})
+                                         'channel': uid_channel_set['channel'], 'password': encrypt_password})
                         else:
-                            data_list = [{'userID': userID, 'UID': UID, 'uid_nick': NickName, 'password': View_Password}]
+                            data_list = [{'userID': userID, 'UID': UID, 'uid_nick': NickName, 'password': encrypt_password}]
 
                         # 请求Alexa服务器更新事件网关
                         data_list = json.dumps(data_list)

+ 40 - 0
Service/ModelService.py

@@ -1,9 +1,11 @@
+import base64
 import logging
 import time
 
 import requests
 
 from Ansjer.config import BASE_DIR
+from Controller.CheckUserData import RandomStr
 from Model.models import *
 import json
 from django.db.models import Q
@@ -248,6 +250,44 @@ class ModelService:
         file.flush()
         file.close()
 
+    @staticmethod
+    # 加密
+    def encrypt_pwd(userPwd):
+        for i in range(1, 4):
+            if i == 1:
+                userPwd = RandomStr(3, False) + userPwd + RandomStr(3, False)
+                userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
+            if i == 2:
+                userPwd = RandomStr(2, False) + str(userPwd) + RandomStr(2, False)
+                userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
+            if i == 3:
+                userPwd = RandomStr(1, False) + str(userPwd) + RandomStr(1, False)
+                userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
+        return userPwd
+
+    @staticmethod
+    # 解密
+    def decode_pwd(password):
+        for i in range(1, 4):
+            if i == 1:
+                # 第一次先解密
+                password = base64.b64decode(password)
+                password = password.decode('utf-8')
+                # 截去第一位,最后一位
+                password = password[1:-1]
+            if i == 2:
+                # 第2次先解密
+                password = base64.b64decode(password)
+                password = password.decode('utf-8')
+                # 去前2位,后2位
+                password = password[2:-2]
+            if i == 3:
+                # 第3次先解密
+                password = base64.b64decode(password)
+                password = password.decode('utf-8')
+                # 去前3位,后3位
+                password = password[3:-3]
+        return password
 
 def notify_alexa_delete(userID, UID):
     url = 'https://www.zositech.xyz/deviceStatus/delete'