LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need Help with another Cipher. AES CBC & CTR this time (https://www.linuxquestions.org/questions/programming-9/need-help-with-another-cipher-aes-cbc-and-ctr-this-time-4175439713/)

Nabeel 12-02-2012 12:22 PM

Need Help with another Cipher. AES CBC & CTR this time
 
Well I have to built a CBC cipher this time to decrypt a given messege.

Quote:

CBC key: 140b41b22a29beb4061bda66b6747e14
CBC Ciphertext 1:
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee\
2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81
I tried going for Pycrpto and used the following command

Code:

>>> from Crypto.Cipher import AES
>>> key='140b41b22a29beb4061bda66b6747e14'
>>>iv='4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee'
>>>dcrpt=AES.new(k,AES.MODE_CBC,iv)

My Idea was to use "dcrpt.decrypt(messege)"

but I got the following error
Code:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    dcrpt=AES.new(key,AES.MODE_CBC,iv)
  File "/usr/local/lib/python2.7/dist-packages/Crypto/Cipher/AES.py", line 95, in new
    return AESCipher(key, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long

What should I do?

ntubski 12-02-2012 06:28 PM

Quote:

Originally Posted by Nabeel (Post 4841305)
CBC Ciphertext 1:
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee\
2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81
...
>>>iv='4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee'

Do you understand what an IV is? It's not the ciphertext. If they didn't give you an IV, a reasonable assumption is that it should be all zeros.

Also, you should realize that the string containing the characters "4ca0" is not the same as the string the raw byte values 4Chex (76), A0hex (160).

Nabeel 12-02-2012 10:37 PM

Well Finally built a decryptr for both CBC and CTR.
here is how I implemented it

Code:

from Crypto.Cipher import AES
from Crypto.Util import Counter
import binascii
import random


def d_AES_nbl(k,c):
    key=binascii.unhexlify(k)
    iv=binascii.unhexlify(c[:32])
    msg=binascii.unhexlify(c[32:])
    d=AES.new(key,AES.MODE_CBC,iv)
    return d.decrypt(msg)


def d_AES_CTR_nbl(k,c):
    key=binascii.unhexlify(k)
    msg=binascii.unhexlify(c[32:])
    iv=binascii.unhexlify(c[:32])
    ctr=Counter.new(128, initial_value=long(iv.encode("hex"), 16))
    d=AES.new(key, AES.MODE_CTR, counter = ctr)
    return d.decrypt(msg)

Although I would really love some advice on more efficiently building these functions.

ntubski 12-03-2012 09:57 PM

Oh I see the IV was in fact in the ciphertext. It's true that the IV is commonly put in front of the ciphertext when encrypting, but if I was giving an exercise I wouldn't label it part of the ciphertext.

Quote:

Although I would really love some advice on more efficiently building these functions.
You could factor out the unhexlifying part (untested):

Code:

from Crypto.Cipher import AES
from Crypto.Util import Counter
import binascii
import random

def unhexlify_args(f):
    def unhex_and_f(*args):
        f(*map(binascii.unhexlify, args))
    return unhex_and_f

def d_AES_nbl_bin(key, ciphertext):
    d=AES.new(key,AES.MODE_CBC,ciphertext[:16])
    return d.decrypt(ciphertext[16:])

def d_AES_CTR_nbl_bin(key, ciphertext):
    ctr=Counter.new(16*8, initial_value=long(ciphertext[:16].encode("hex"), 16))
    d=AES.new(key, AES.MODE_CTR, counter = ctr)
    return d.decrypt(ciphertext[16:])

d_AES_nbl = unhexlify_args(d_AES_nbl_bin)
d_AES_CTR_nbl = unhexlify_args(d_AES_CTR_nbl_bin)



All times are GMT -5. The time now is 02:00 PM.