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
In Python, it is perfectly valid to treat functions as objects. It is also perfectly valid to treat any object that defines __call__(self, ...)
as a function.
So what you want might something like this:
class IVCounter(object):
def __init__(self, start=1L):
self.value = long(start)
def __call__(self):
self.value += 1L
return somehow_convert_this_to_a_bitstring(self.value)
ctr = IVCounter()
... make some keys and ciphertext ...
print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)
However, PyCrypto provides a counter method for you that should be much faster than pure Python:
import Crypto.Util.Counter
ctr = Crypto.Util.Counter.new(NUM_COUNTER_BITS)
ctr
is now a stateful function (and, simultaneously, a callable object) that increments and returns its internal state every time you call it. You can then do
print AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(ciphertext)
just as before.
Here's a working example using Crypto.Cipher.AES in CTR mode with a user-specified initialization vector:
import Crypto.Cipher.AES
import Crypto.Util.Counter
key = "0123456789ABCDEF" # replace this with a sensible value, preferably the output of a hash
iv = "0000000000009001" # replace this with a RANDOMLY GENERATED VALUE, and send this with the ciphertext!
plaintext = "Attack at dawn" # replace with your actual plaintext
ctr = Crypto.Util.Counter.new(128, initial_value=long(iv.encode("hex"), 16))
cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CTR, counter=ctr)
print cipher.encrypt(plaintext)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With