LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-02-2012, 12:22 PM   #1
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Rep: Reputation: 17
Question 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?
 
Old 12-02-2012, 06:28 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by Nabeel View Post
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).
 
1 members found this post helpful.
Old 12-02-2012, 10:37 PM   #3
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
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.

Last edited by Nabeel; 12-03-2012 at 01:49 AM.
 
Old 12-03-2012, 09:57 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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)

Last edited by ntubski; 12-03-2012 at 09:58 PM. Reason: match argname to closure name
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OPENSSL AES-128-CBC Encoding Format AbdulKabani Linux - Newbie 0 05-20-2012 10:29 AM
PDFs are AES cipher-able as of the acrobat 7 spec- any GNU tools to handle it? gnuweenie Linux - Security 2 06-11-2011 06:57 AM
dm-crypt aes-xts-plain64 vs aes-cbc-essiv for volumes > 2TiB Molly Linux - Security 1 09-13-2010 05:24 PM
Slackware 13 (64 bit) Wireless with WPA2-PSK and WPA Cipher set to AES KrazyKanuk Slackware 3 01-26-2010 05:52 AM
CBC Recommends Linux to Average User DragonSlayer48DX Linux - News 1 03-28-2007 12:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration