updated IS codes
This commit is contained in:
parent
a5c9c120ff
commit
13bcfbbafd
7 changed files with 356 additions and 1 deletions
49
IS/Lab/Lab6/socket_rsa/client.py
Normal file
49
IS/Lab/Lab6/socket_rsa/client.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
import os
|
||||
import socket
|
||||
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Cipher import PKCS1_OAEP
|
||||
|
||||
|
||||
def recv_exact(conn: socket.socket, n: int) -> bytes:
|
||||
buf = b""
|
||||
while len(buf) < n:
|
||||
chunk = conn.recv(n - len(buf))
|
||||
if not chunk:
|
||||
raise ConnectionError("connection closed")
|
||||
buf += chunk
|
||||
return buf
|
||||
|
||||
|
||||
def main() -> None:
|
||||
host = "127.0.0.1"
|
||||
port = 5003
|
||||
default_msg = os.environ.get("LAB6_MESSAGE", "rsa hello")
|
||||
try:
|
||||
user_msg = input(f"Enter plaintext [{default_msg}]: ").strip()
|
||||
except EOFError:
|
||||
user_msg = ""
|
||||
message = (user_msg or default_msg).encode()
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as c:
|
||||
c.connect((host, port))
|
||||
# Receive server public key
|
||||
klen = int.from_bytes(recv_exact(c, 4), "big")
|
||||
kbytes = recv_exact(c, klen)
|
||||
server_pub = RSA.import_key(kbytes)
|
||||
|
||||
# Encrypt message to server
|
||||
cipher = PKCS1_OAEP.new(server_pub)
|
||||
ctext = cipher.encrypt(message)
|
||||
c.sendall(len(ctext).to_bytes(4, "big") + ctext)
|
||||
|
||||
# Receive acknowledgement
|
||||
rlen = int.from_bytes(recv_exact(c, 4), "big")
|
||||
reply = recv_exact(c, rlen)
|
||||
print("server reply:", reply.decode(errors="ignore"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
43
IS/Lab/Lab6/socket_rsa/server.py
Normal file
43
IS/Lab/Lab6/socket_rsa/server.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
import socket
|
||||
from typing import Tuple
|
||||
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Cipher import PKCS1_OAEP
|
||||
|
||||
|
||||
def generate_rsa(bits: int = 2048) -> Tuple[RSA.RsaKey, RSA.RsaKey]:
|
||||
key = RSA.generate(bits)
|
||||
return key.publickey(), key
|
||||
|
||||
|
||||
def main() -> None:
|
||||
host = "127.0.0.1"
|
||||
port = 5003
|
||||
|
||||
pub, priv = generate_rsa(2048)
|
||||
pub_pem = pub.export_key()
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
|
||||
srv.bind((host, port))
|
||||
srv.listen(1)
|
||||
conn, _ = srv.accept()
|
||||
with conn:
|
||||
# Send server public key
|
||||
conn.sendall(len(pub_pem).to_bytes(4, "big") + pub_pem)
|
||||
|
||||
# Receive client's encrypted message
|
||||
clen = int.from_bytes(conn.recv(4), "big")
|
||||
ctext = conn.recv(clen)
|
||||
|
||||
cipher = PKCS1_OAEP.new(priv)
|
||||
msg = cipher.decrypt(ctext)
|
||||
# Respond: encrypt reply with same public (client will not be able to decrypt without its private),
|
||||
# so just echo plaintext length as acknowledgement for demo.
|
||||
reply = f"received:{len(msg)}".encode()
|
||||
conn.sendall(len(reply).to_bytes(4, "big") + reply)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue