Home > Mobile >  How to use AES-CTR with same IV in Python?
How to use AES-CTR with same IV in Python?

Time:12-10

I'm trying to perform a frequency analysis attack on AES-CTR and to my knowledge I need two different ciphertexts generated using the same key and IV for me to be able to carry out the attack. The problem is that for the pycryptodome library, when using AES-CTR, the iv keyword argument is unavailable for the MODE_CTR. Setting the nonce gives me the same ciphertext for the first message when rerunning the program but if encrypting the same message twice the second ciphertext is still different.

Can anyone assist me?

from Crypto.Cipher import AES

key = '1111111111111111'
message = 'Hello, world!'

cipher = AES.new(key, AES.MODE_CTR, nonce='0')
ct_1 = cipher.encrypt(message)
ct_2 = cipher.encrypt(message)

CodePudding user response:

You have to start afresh with a new instance, so:

cipher1 = AES.new(key, AES.MODE_CTR, nonce='0')
ct_1 = cipher1.encrypt(message)
cipher2 = AES.new(key, AES.MODE_CTR, nonce='0')
ct_2 = cipher2.encrypt(message)

Otherwise the counter will have progressed, and that means that you are encrypting with a different part of the generated key stream.

Remember that cipher instances are stateful. Fortunately they are also pretty lightweight, so re-instantiating a new one is next to harmless.

  • Related