1、 Verify the validity of the client
Sign in : You need to log in when you have personalized design
Login and legitimacy verification two of one , If you do login, you don't need to do validation of legitimacy
server
import os
import socket
import hashlib
SECRET_KEY = b'yongliang' # Set secret key
def check_client(conn):
randbytes = os.urandom(32) # Randomly produce a 32 Random bytes of bits
conn.send(randbytes) # Send random bytes to client
# The server performs random bytes hashlib Add salt dynamically ( Secret key ) encryption
md5 = hashlib.md5(SECRET_KEY)
md5.update(randbytes)
code = md5.hexdigest()
code_cli = conn.recv(32).decode('utf-8') # receive client Encrypted string sent
return code == code_cli # Perform client authentication return bool value
sk = socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
while True:
conn,addr = sk.accept()
if not check_client(conn):continue # Verification judgment If you get False -->continue
print(' The process is communicating normally ') # Verification passed Perform other procedures
client
import os
import socket
import hashlib
SECRET_KEY = b'yongliang'
def check_client():
randbytes = sk.recv(32) # The receiving server sends 32 Random bytes of bits
# The client performs a random byte hashlib Add salt dynamically ( Secret key ) encryption
md5 = hashlib.md5(SECRET_KEY)
md5.update(randbytes)
code = md5.hexdigest().encode('utf-8')
sk.send(code) # Send encrypted string to server
sk = socket.socket()
sk.connect(('127.0.0.1',9001))
check_client()
print(' Normal client communication ') # Verification passed Then perform other procedures
Plaintext can also be encrypted with hmac modular
import os
import hmac # Add salt to encrypted content
SECRET_KEY = b'yongliang'
randbytes = os.urandom(32) # Randomly produce a 32 Bytes of bits
mac = hmac.new(SECRET_KEY,randbytes) # Dynamic encryption of random bytes
ret = mac.digest()
print(ret)