Search code examples
pythonencryptionaespycryptodome

Error 65537 while instatiating the CBC mode


I have been using the PyCryptoDome library to encrypt some communications between a client and a server. Here is how the protocol goes.

  1. The server sends an RSA public key to the client.
  2. The client generates an AES key
  3. The client encrypts it. encrypts it using the public key (with the PKCS1_OAEP encrypt function)
  4. The encrypted key is sent to the server.
  5. The server decrypts the key
  6. The server and client switch to AES-CBC to encrypt communications.

After that, when the client sends a message, the server decrypts it, uses it for what needs to be done with it, and then sends it back. All goes well until the client tries to decrypt the message that is sent back by the server. The thread for receiving messages stops, due to:

Error 65537 while instatiating the CBC mode

Different error codes happen when using different AES modes.

Here is my modified AESCipher Class:

import binascii
from Crypto import Random
from Crypto.Cipher import AES


class AESCipher(object):

    def __init__(self, key):
        self.key = key
        self.pad = lambda s: s + (AES.block_size - len(s) % AES.block_size) * \
            chr(AES.block_size - len(s) % AES.block_size)
        self.unpad = lambda s: s[:-ord(s[len(s) - 1:])]

    def encrypt(self, raw):
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return binascii.b2a_hex(iv + cipher.encrypt(self.pad(raw)))

    def decrypt(self, enc):
        enc = binascii.a2b_hex(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self.unpad(cipher.decrypt(enc[AES.block_size:]))

Client receiving side (the error-causing side):

def recvMesgThread(netcat, output, aescipher):
    while True:
        try:
            data = netcat.recv_until('\r\n').replace('\r\n', '')
        except NetcatError:
            print('Lost connection to server!')
            sys.exit(0)

        if data[:5] == '/MESG' and data[-5:] =='MESG/' :
            try:
                output.append(aescipher.decrypt(buf[5:-5]))
            except Exception as e:
                print(e)

Solution

  • Well, this is embarrassing...

    The problem was a typo. Line 11 tries to decrypt the buf variable, which doesn't exist. The data variable was the one that contained what I needed.