Still can't quite get this to work. My question is about how to make the decryption line work. Here is what I have written:
class IVCounter(object):
@staticmethod
def incrIV(self):
temp = hex(int(self, 16)+1)[2:34]
return array.array('B', temp.decode("hex")).tostring()
def decryptCTR(key, ciphertext):
iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext
#convert the key into a 16 byte string
key = array.array('B', key.decode("hex")).tostring()
print AES.new(key, AES.MODE_CTR, counter=IVCounter.incrIV(iv)).decrypt(ciphertext)
return
My error message is:
ValueError: 'counter' parameter must be a callable object
I just can't figure out how pycrypto wants me to organize that third argument to new.
Can anyone help? Thanks!
EDIT New code after implementing the suggestions below. Still stuck!
class IVCounter(object):
def __init__(self, start=1L):
print start #outputs the number 1 (not my IV as hoped)
self.value = long(start)
def __call__(self):
print self.value #outputs 1 - need this to be my iv in long int form
print self.value + 1L #outputs 2
self.value += 1L
return somehow_convert_this_to_a_bitstring(self.value) #to be written
def decryptCTR(key, ciphertext):
iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext
iv = int(iv, 16)
#convert the key into a 16 byte string
key = array.array('B', key.decode("hex")).tostring()
ctr = IVCounter()
Crypto.Util.Counter.new(128, initial_value = iv)
print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)
return
EDIT STILL can't get this to work. very frustrated and completely out of ideas. Here is the latest code: (please note that my input strings are 32-bit hex strings that must be interpreted in two-digit pairs to convert to long integers.)
class IVCounter(object):
def __init__(self, start=1L):
self.value = long(start)
def __call__(self):
self.value += 1L
return hex(self.value)[2:34]
def decryptCTR(key, ciphertext):
iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext
iv = array.array('B', iv.decode("hex")).tostring()
ciphertext = ciphertext[32:]
#convert the key into a 16 byte string
key = array.array('B', key.decode("hex")).tostring()
#ctr = IVCounter(long(iv))
ctr = Crypto.Util.Counter.new(16, iv)
print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)
return
TypeError: CTR counter function returned string not of length 16