Python Forum
TypeError: '>=' not supported between instances of 'int' and 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: '>=' not supported between instances of 'int' and 'str'
#1
Exclamation 
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!!
Reply
#2
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)
Reply
#3
Please, post the full traceback you get, verbatim, not just the last line.
Also the full code, e.g. what is new_pass?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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)
Reply
#5
(Mar-10-2021, 05:57 PM)buran Wrote: Please, post the full traceback you get, verbatim, not just the last line.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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'
Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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
Reply
#9
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,090 Apr-30-2021, 07:37 PM
Last Post: quest
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,488 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 8,849 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: Not supported between instances of 'function' and 'int' palladium 9 19,652 Dec-06-2019, 12:40 AM
Last Post: palladium
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 10,538 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 3,994 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 13,096 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 7,954 Apr-09-2019, 03:25 PM
Last Post: perfringo
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 5,575 Mar-05-2019, 09:51 PM
Last Post: stranac
  '<=' not supported between instances of 'str' and 'int' Loom 1 7,782 Aug-11-2018, 07:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020