我正在尝试使我的登录系统安全,并且正在使用PBKDF2哈希算法。
但是,每次我启动程序并尝试将某些内容发送到服务器时,它都会崩溃并出现一些奇怪的错误Process finished with exit code -1073740791 (0xC0000409)
这是Send_login函数的代码:
try:
tcp_client = socket(AF_INET, SOCK_STREAM)
tcp_client.connect((host_ip, server_port))
tcp_client.sendall(('L|' + str(pbkdf2.crypt(Login.encode(), iterations=150, salt="f597beecd1f5be49cce44b7b62316669")) + '|' + str(pbkdf2.crypt(Pass.encode(), iterations=150, salt="f597beecd1f5be49cce44b7b62316669"))))
received = tcp_client.recv(2048)
received = received.decode("utf8")
if received == "Auth succeed":
print("Credentials are correct")
else:
print("Either credentials are incorrect or our servers are unavailable right now")
finally:
tcp_client.close()
PS:Send_login()是与此套接字交互的唯一函数
答案 0 :(得分:1)
首先,如果您是网络应用程序,请使用SSL。其次,如果您仍想加密消息,请使用加密模块。
import ujson as json
import base64, zlib
from config import config_opts
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
SALT = config_opts['SALT']
SALT_SIZE = config_opts['SALT_SIZE']
SALT_OFF_SET = config_opts['SALT_OFF_SET']
NUMBER_OF_ITERATIONS = config_opts['NUMBER_OF_ITERATIONS']
AES_MULTIPLE = config_opts['AES_MULTIPLE']
def cryptkey(password=''):
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(SALT+password)
return Fernet(base64.urlsafe_b64encode(digest.finalize()))
def encrypt(meta, password=''):
meta = json.dumps(meta)
meta = str(zlib.compress(meta, 9))
f = cryptkey(password)
return base64.urlsafe_b64encode(f.encrypt(bytes(meta)))
def decrypt(meta, password=''):
meta = base64.urlsafe_b64decode(meta)
f = cryptkey(password)
meta = f.decrypt(bytes(meta))
meta = zlib.decompress(meta)
return json.loads(meta)
try:
tcp_client = socket(AF_INET, SOCK_STREAM)
tcp_client.connect((host_ip, server_port))
tcp_client.sendall(encrypt([Login.encode(), Pass.encode()]))
received = tcp_client.recv(2048)
received = received.decode("utf8")
if received == "Auth succeed":
print("Credentials are correct")
else:
print("Either credentials are incorrect or our servers are unavailable right now")
finally:
tcp_client.close()
然后,您的后端应使用decrypt
函数(和可选密码)。 Base64使它更适合REST api通信。