Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:00:59+00:00 2026-06-08T16:00:59+00:00

Still can’t quite get this to work. My question is about how to make

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:01:00+00:00Added an answer on June 8, 2026 at 4:01 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading all over the web about this and still can't understand
I already asked a similar question but still can't get it to work. I
I know there are multiple posts on this but I still can't get it
This question seems to be asked freqeuently over the internet but I still can't
Still can't get this working...Rails 3.1.3, Ruby 1.9.2 on Heroku's Cedar Stack. Trying to
I have looked here but still can't figure it out. How do I change
I still can't get it to work.. After I had to separate implementation from
I've been googling around and I still can't get it. Some people say: here
Well this kind of n00b question but I still can't figure it out. I
I did my reading on this and still can't make sense why my query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.