144
社区成员
发帖
与我相关
我的任务
分享课程:《Python程序设计》
班级:2221
姓名:李新卓
学号:20222127
实验教师:王志强
实验日期:2023年4月13日
必修/选修:公选课
1.实验内容
创建服务端和客户端,服务端在特定端口监听多个客户请求。客户端和服务端通过Socket套接字(TCP/UDP)进行通信。
2. 实验过程及结果
加密算法:
# -*- coding: utf-8 -*-
# 文件名: DesModule
# 描 述:
# 作 者: besti
# 日 期: 2023/4/11
# Have you ever seen Los Angeles at 4 a.m?
from pyDes import *
import binascii
def des_encrypt(s, KEY):
secret_key = KEY
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
en = k.encrypt(s, padmode=PAD_PKCS5)
return binascii.b2a_hex(en).decode()
def des_descrypt(s, KEY):
secret_key = KEY
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
return de.decode()
if __name__ == "__main__":
ciphertext = des_encrypt("hello, world!", "12345678")
print("密文:",ciphertext)
plaintext = des_descrypt(ciphertext, "12345678")
print("解密后的明文:", plaintext)
import os
import socket
from DesModule import des_encrypt, des_descrypt
if os.path.exists("C:\\Users\\84690\\PycharmProjects\\pythonProject1\\lxz"):
file1 = open("lxz", 'r+')
else:
file1 = open("lxz", 'w+')
file1.write("howsitgoing")
file1.seek(0)
howsitgoing = file1.readline()
ciphertext = des_encrypt(howsitgoing, "12345")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 8000))
s.sendall(ciphertext.encode())
data = s.recv(1024)
print("解密前", data.decode())
plaintext = des_descrypt(data, "lxz12345")
print("从服务器接受的数据是:", plaintext)
file1.write(plaintext)
file1.close()
s.close()
import socket
import os
from DesModule import *
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 8000))
s.listen()
conn, address = s.accept()
data = conn.recv(1024)
ciphertext = des_encrypt("howsitgoing", "12345678")
print("解密前", data.decode())
plaintext = des_descrypt(data, "12345678")
print("来自客户端的信息是", plaintext)
if os.path.exists("C:\\Users\\84690\\PycharmProjects\\pythonProject1\\lxz"):
file = open("lxz", 'a+')
file.write(plaintext)
else:
file = open("lxz", 'w+')
file.write(plaintext)
file.seek(0)
file.close()
conn.sendall(ciphertext.encode())
上传gitee:
