Search code examples
pythonpython-3.xinstantiationselfpycrypto

Instances & classes: requiring x arguments when x-1 given


I have written the following classes to be able to test different encryption schemes. However, I'm having trouble instantiating objects from the different encryption scheme. Could someone point out to something that doesn't make sense that I'm not catching atm? I'm not sure why it doesn't work. It gives a TypeError: encrypt() takes exactly 3 arguments (2 given) but it does have self passed, so I don't know how to fix it on the basis of the rest of them.

class AXU:
    def __init__(self, sec_param):
        self.sec_param = sec_param

    def getHash(self):
        # sample a, b and return hash function
        a = random.randrange(self.sec_param) 
        b = random.randrange(self.sec_param)

        return lambda x : a*x+b % sec_param

class BC(object):
    def __init__(self, sec_param):
        # generate a key 
        self.sec_param = sec_param

    def encrypt(self, message, key):
        #encrypt with AES?
        cipher = AES.new(key, MODE_CFB, sec_param)
        msg = iv + cipher.encrypt(message)
        return msg

class tBC(object):
    def __init__(self, sec_param):
        self.sec_param = sec_param

    def encrypt(self, tweak, message):
        #pass
        return AES.new(message, tweak)

class Trivial(tBC):
    def __init__(self):
        self.bcs = {}

    def encrypt(self, tweak, message):
        if tweak not in self.bcs.keys():
            bc = BC()
            self.bcs[tweak] = bc
        return self.bcs[tweak].encrypt(message)

class Our(tBC):
    def __init__(self, sec_param):
        self.bc1 = BC(sec_param)
        self.bc2 = BC(sec_param)
        self.bc3 = BC(sec_param)
        self.bc4 = BC(sec_param)
        # encryption over GF field
    def encrypt(self, tweak, message):
        return self.bc1.encrypt(self.bc2.encrypt(tweak) * self.bc3.encrypt(message) + self.bc4.encrypt(tweak))

Solution

  • You are passing in one argument to a bound method:

    return self.bc1.encrypt(
        self.bc2.encrypt(tweak) * self.bc3.encrypt(message) + 
        self.bc4.encrypt(tweak))
    

    That's one argument to the BC.encrypt() method each, and this method takes 2 beyond self, message and key.

    Either pass in a value for key, or remove that argument from the BC.encrypt() method definition (and get the key from some place else; perhaps from an instance attribute set in __init__).