So I'm making a client and server end to end encryption (trust me this doesn't have to do with web development) and i get this error message:
Error:
TypeError: '>=' not supported between instances of 'int' and 'str'
This is my code:
encrypted = RSA.Message.from_hex(new_pass).encrypt(serverkey)
And this is the library:
https://github.com/mcdallas/cryptotools
Please help!!
BTW if you want to replicate it then just make a public key by:
server code:
from cryptotools import PrivateKey, RSA
key = PrivateKey.random()
pkey = key.to_public()
server_socket = socket.socket() # get instance
server_socket.bind((host, port)) # bind host address and port together
server_socket.listen(1)
conn, address = server_socket.accept() # accept new connection
conn.sendall(pkey)
client code:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
serverkey = s.recv(1024).decode()
encrypted = RSA.Message.from_hex(new_pass).encrypt(serverkey)
Please, post the full traceback you get, verbatim, not just the last line.
Also the full code, e.g. what is new_pass
?
(Mar-10-2021, 05:57 PM)buran Wrote: [ -> ]Please, post the full traceback you get, verbatim, not just the last line.
Also the full code, e.g. what is new_pass
?
Okay.
Server:
import socket
from cryptotools import RSA, PrivateKey
hashed = None
Content = None
Password = None
File = None
f = None
feil = None
FileLoc = None
UUID = None
AccountExists = None
rawUUID = None
key = PrivateKey.random()
pkey = key.to_public()
def server_program():
# get the hostname
global hashed, Content, Password, f, File, feil, FileLoc, rawUUID, AccountExists, UUID
host = "127.0.0.1"
port = 65432 # initiate port no above 1024
server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
if data != "":
print(data)
if not data:
break
if data[0:4] == "PSWD": # PSWD
Password = data.split("PSWD")[1]
try:
AccountExists = open("logins.txt", 'w+').read().split(Password)[1]
except IndexError as e:
conn.sendall(RSA.Message.from_hex(b'AccountDoesntExist').encrypt(UUID))
print(e)
continue
PSWD = AccountExists.split("\n")[0]
if PSWD == Password:
conn.sendall(RSA.Message.from_hex(b'True').encrypt(UUID))
else:
conn.sendall(RSA.Message.from_hex(b'False').encrypt(UUID))
elif data[0:4] == "UUID": # UUID
Content = data.split("UUID")[1]
UUID = Content
msg = f'{pkey}'
conn.sendall(msg.encode())
server_socket.close()
server_program()
if __name__ == '__main__':
server_program()
Client:
import socket
from cryptotools import PrivateKey, RSA
HOST = "127.0.0.1"
PORT = 65432
private = PrivateKey.random()
public = private.to_public()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
msg = f'UUID{public}'
s.sendall(msg.encode())
serverkey = s.recv(1024).decode()
new_pass = input('Please enter a password: ')
msg = f'PSWD{RSA.Message.from_hex(new_pass).encrypt((serverkey[0], serverkey[1]))}'
s.sendall(msg.encode())
rawdata = s.recv(1024).decode()
Recieved = RSA.Message.from_hex(rawdata).decrypt(private)
s.close()
print(Recieved)
(Mar-10-2021, 06:28 PM)buran Wrote: [ -> ] (Mar-10-2021, 05:57 PM)buran Wrote: [ -> ]Please, post the full traceback you get, verbatim, not just the last line.
Error:
Traceback (most recent call last):
File "C:\Users\####\PycharmProjects\pythonProject\main.py", line 15, in <module>
msg = f'PSWD{RSA.Message.from_hex(new_pass).encrypt((serverkey[0], serverkey[1]))}'
File "C:\Python39\lib\site-packages\cryptotools\RSA\rsa.py", line 26, in encrypt
if self.int() >= n:
TypeError: '>=' not supported between instances of 'int' and 'str'
You have problem sending the public key from the server. You send a str representation of PublicKey instance. Then on client side after decoding you just take first 2 char from that str and try to use them when encrypt you password message
not tested, but try
in server replace line 56 msg = f'{pkey}'
with msg = pkey.hex()
in client replace line 13 serverkey = s.recv(1024).decode()
with serverkey = PublicKey.from_hex(s.recv(1024).decode())
note, you need to import PublicKey
before use it
and then line 15 will become msg = f'PSWD{RSA.Message.from_hex(new_pass).encrypt(serverkey)}'
Also, not sure you want to use Message.from_hex()
to create message for the new password.
ALSO, Same apply when sending client public key to server - try to make same changes on your own, otherwise you will get the same error when server try to use client public key.
(Mar-10-2021, 07:18 PM)buran Wrote: [ -> ]You have problem sending the public key from the server. You send a str representation of PublicKey instance. Then on client side after decoding you just take first 2 char from that str and try to use them when encrypt you password message
not tested, but try
in server replace line 56 msg = f'{pkey}'
with msg = pkey.hex()
in client replace line 13 serverkey = s.recv(1024).decode()
with serverkey = PublicKey.from_hex(s.recv(1024).decode())
note, you need to import PublicKey
before use it
and then line 15 will become msg = f'PSWD{RSA.Message.from_hex(new_pass).encrypt(serverkey)}'
Also, not sure you want to use Message.from_hex()
to create message for the new password.
ALSO, Same apply when sending client public key to server - try to make same changes on your own, otherwise you will get the same error when server try to use client public key.
now i just get this:
Error:
Traceback (most recent call last):
File "C:\Users\####\PycharmProjects\pythonProject\main.py", line 15, in <module>
msg = f'PSWD{RSA.Message.from_hex(new_pass).encrypt(serverkey)}'
File "C:\Python39\lib\site-packages\cryptotools\RSA\rsa.py", line 25, in encrypt
e, n = key
TypeError: cannot unpack non-iterable PublicKey object
It took some to dig into this poorly documented library
client:
import socket
import json
from cryptotools import RSA
HOST = "127.0.0.1"
PORT = 65432
private, public = RSA.generate_keypair(512)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
msg = f'UUID{json.dumps(public)}'
s.sendall(msg.encode())
serverkey = json.loads(s.recv(1024).decode())
new_pass = input('Please enter a password: ')
new_pass = f'PSWD{new_pass}'
message = RSA.Message.from_str(new_pass)
message.encrypt(serverkey)
s.sendall(message.hex().encode())
rawdata = s.recv(1024).decode()
recieved = RSA.Message.from_hex(rawdata)
recieved.decrypt(private)
print(recieved.str())
s.close()
server
import socket
import json
from cryptotools import RSA
hashed = None
Content = None
Password = None
File = None
f = None
feil = None
FileLoc = None
UUID = None
AccountExists = None
rawUUID = None
key, pkey = RSA.generate_keypair(512)
def server_program():
# get the hostname
global hashed, Content, Password, f, File, feil, FileLoc, rawUUID, AccountExists, UUID
host = "127.0.0.1"
port = 65432 # initiate port no above 1024
server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
if data != "":
print(f'Decoded raw data: {data}')
if data == 'quit':
break
elif data[0:4] == "UUID": # UUID
Content = data.split("UUID")[1]
UUID = json.loads(Content)
msg = json.dumps(pkey)
conn.sendall(msg.encode())
else:
data = RSA.Message.from_hex(data)
data.decrypt(key)
data = data.str()
if data[0:4] == "PSWD": # PSWD
Password = data.split("PSWD")[1]
PSWD = 'mypass'
response = 'Correct' if PSWD == Password else 'Incorrect'
message = RSA.Message.from_str(response)
message.encrypt(UUID)
conn.sendall(message.hex().encode())
server_socket.close()
if __name__ == '__main__':
server_program()
Note, I hardcoded the correct password to be
'mypass'
.
Also, I didn't bother to refactor all of your code - e.g. style, no functions, antipatterns, etc.
(Mar-11-2021, 07:40 AM)buran Wrote: [ -> ]It took some to dig into this poorly documented library
client:
import socket
import json
from cryptotools import RSA
HOST = "127.0.0.1"
PORT = 65432
private, public = RSA.generate_keypair(512)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
msg = f'UUID{json.dumps(public)}'
s.sendall(msg.encode())
serverkey = json.loads(s.recv(1024).decode())
new_pass = input('Please enter a password: ')
new_pass = f'PSWD{new_pass}'
message = RSA.Message.from_str(new_pass)
message.encrypt(serverkey)
s.sendall(message.hex().encode())
rawdata = s.recv(1024).decode()
recieved = RSA.Message.from_hex(rawdata)
recieved.decrypt(private)
print(recieved.str())
s.close()
server
import socket
import json
from cryptotools import RSA
hashed = None
Content = None
Password = None
File = None
f = None
feil = None
FileLoc = None
UUID = None
AccountExists = None
rawUUID = None
key, pkey = RSA.generate_keypair(512)
def server_program():
# get the hostname
global hashed, Content, Password, f, File, feil, FileLoc, rawUUID, AccountExists, UUID
host = "127.0.0.1"
port = 65432 # initiate port no above 1024
server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
if data != "":
print(f'Decoded raw data: {data}')
if data == 'quit':
break
elif data[0:4] == "UUID": # UUID
Content = data.split("UUID")[1]
UUID = json.loads(Content)
msg = json.dumps(pkey)
conn.sendall(msg.encode())
else:
data = RSA.Message.from_hex(data)
data.decrypt(key)
data = data.str()
if data[0:4] == "PSWD": # PSWD
Password = data.split("PSWD")[1]
PSWD = 'mypass'
response = 'Correct' if PSWD == Password else 'Incorrect'
message = RSA.Message.from_str(response)
message.encrypt(UUID)
conn.sendall(message.hex().encode())
server_socket.close()
if __name__ == '__main__':
server_program()
Note, I hardcoded the correct password to be 'mypass'
.
Also, I didn't bother to refactor all of your code - e.g. style, no functions, antipatterns, etc.
Before checking it out, I appreciate your effort in literally going deep into a poorly documented library,
Here goes nothing!
It worked! Thank you!
Using JSON is a great idea!
P.S. I did change some code to make it actually work how I intended it.