使用 python 和 RSA 算法的加密聊天应用程序
Encrypted chat app using python and RSA algorithm
我正在尝试使用 python 中的 RSA 算法创建一个加密的聊天应用程序,但我收到了错误消息。我找不到代码中的错误,聊天系统根本无法正常工作。 client.py 正在终止并显示此错误
文件 "client.py",第 17 行,在
中
server_string = server.recv(1024)
OSError: [Errno 107] Transport endpoint is not connected
代码是:
server.py 是:
import socket
from Crypto.PublicKey import RSA
from Crypto import Random
#Generate private and public keys
random_generator = Random.new().read
private_key = RSA.generate(1024, random_generator)
public_key = private_key.publickey()
#Declartion
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.getfqdn())
port = 8888
encrypt_str = "encrypted_message="
if host == "127.0.1.1":
import commands
host = commands.getoutput("hostname -I")
print("host = " + host)
#Prevent socket.error: [Errno 98] Address already in use
mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mysocket.bind((host, port))
mysocket.listen(5)
c, addr = mysocket._accept()
while True:
#Wait until data is received.
data = c.recv(1024)
data = data.replace("\r\n", '') #remove new line character
if data == "Client: OK":
c.send("public_key=" + public_key.exportKey() + "\n")
print("Public key sent to client.")
elif encrypt_str in data: #Reveive encrypted message and decrypt it.
data = data.replace(encrypt_str, '')
print("Received:\nEncrypted message = "+str(data))
encrypted = eval(data)
decrypted = private_key.decrypt(encrypted)
c.send("Server: OK")
print("Decrypted message = " + decrypted)
elif data == "Quit": break
#Server to stop
c.send("Server stopped\n")
print("Server stopped")
c.close()
client.py 是
import socket
from Crypto.PublicKey import RSA
import sys
import os
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 7777
server.bind(("my_IP_addr_goes_here", 8880))
#data= "Client: OK"
#Tell server that connection is OK
#server.sendall("Client : OK")
#Receive public key string from server
server_string = server.recv(1024)
#Remove extra characters
server_string = server_string.replace("public_key=", '')
server_string = server_string.replace("\r\n", '')
#Convert string to key
server_public_key = RSA.importKey(server_string)
#Encrypt message and send to server
message = "This is my secret message."
encrypted = server_public_key.encrypt(message, 32)
server.sendall("encrypted_message="+str(encrypted))
#Server's response
server_response = server.recv(1024)
server_response = server_response.replace("\r\n", '')
if server_response == "Server: OK":
print("Server decrypted message successfully")
#Tell server to finish connection
server.sendall("Quit")
print(server.recv(1024)) #Quit server response
server.close()
错误是:
服务器脚本是 运行 但是当我终止脚本时它显示
对于server.py:
^CTraceback(最后一次调用):
File "server.py", line 28, in <module>
c, addr = mysocket._accept()
KeyboardInterrupt
对于client.py:
文件 "client.py",第 17 行,在
中
server_string = server.recv(1024)
OSError: [Errno 107] Transport endpoint is not connected
您想捕获 SIGINT...参见 How do I capture SIGINT in Python?
import signal
import sys
def stop_server(sig, frame):
server.sendall("Quit")
print(server.recv(1024)) #Quit server response
server.close()
sys.exit(0)
signal.signal(signal.SIGINT, stop_server)
我正在尝试使用 python 中的 RSA 算法创建一个加密的聊天应用程序,但我收到了错误消息。我找不到代码中的错误,聊天系统根本无法正常工作。 client.py 正在终止并显示此错误
文件 "client.py",第 17 行,在
中 server_string = server.recv(1024)
OSError: [Errno 107] Transport endpoint is not connected
代码是:
server.py 是:
import socket
from Crypto.PublicKey import RSA
from Crypto import Random
#Generate private and public keys
random_generator = Random.new().read
private_key = RSA.generate(1024, random_generator)
public_key = private_key.publickey()
#Declartion
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.getfqdn())
port = 8888
encrypt_str = "encrypted_message="
if host == "127.0.1.1":
import commands
host = commands.getoutput("hostname -I")
print("host = " + host)
#Prevent socket.error: [Errno 98] Address already in use
mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mysocket.bind((host, port))
mysocket.listen(5)
c, addr = mysocket._accept()
while True:
#Wait until data is received.
data = c.recv(1024)
data = data.replace("\r\n", '') #remove new line character
if data == "Client: OK":
c.send("public_key=" + public_key.exportKey() + "\n")
print("Public key sent to client.")
elif encrypt_str in data: #Reveive encrypted message and decrypt it.
data = data.replace(encrypt_str, '')
print("Received:\nEncrypted message = "+str(data))
encrypted = eval(data)
decrypted = private_key.decrypt(encrypted)
c.send("Server: OK")
print("Decrypted message = " + decrypted)
elif data == "Quit": break
#Server to stop
c.send("Server stopped\n")
print("Server stopped")
c.close()
client.py 是
import socket
from Crypto.PublicKey import RSA
import sys
import os
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 7777
server.bind(("my_IP_addr_goes_here", 8880))
#data= "Client: OK"
#Tell server that connection is OK
#server.sendall("Client : OK")
#Receive public key string from server
server_string = server.recv(1024)
#Remove extra characters
server_string = server_string.replace("public_key=", '')
server_string = server_string.replace("\r\n", '')
#Convert string to key
server_public_key = RSA.importKey(server_string)
#Encrypt message and send to server
message = "This is my secret message."
encrypted = server_public_key.encrypt(message, 32)
server.sendall("encrypted_message="+str(encrypted))
#Server's response
server_response = server.recv(1024)
server_response = server_response.replace("\r\n", '')
if server_response == "Server: OK":
print("Server decrypted message successfully")
#Tell server to finish connection
server.sendall("Quit")
print(server.recv(1024)) #Quit server response
server.close()
错误是: 服务器脚本是 运行 但是当我终止脚本时它显示
对于server.py: ^CTraceback(最后一次调用):
File "server.py", line 28, in <module>
c, addr = mysocket._accept()
KeyboardInterrupt
对于client.py: 文件 "client.py",第 17 行,在
中 server_string = server.recv(1024)
OSError: [Errno 107] Transport endpoint is not connected
您想捕获 SIGINT...参见 How do I capture SIGINT in Python?
import signal
import sys
def stop_server(sig, frame):
server.sendall("Quit")
print(server.recv(1024)) #Quit server response
server.close()
sys.exit(0)
signal.signal(signal.SIGINT, stop_server)