top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Usage of crypto in python

+1 vote
266 views

I'm trying to encrypt a string using AES in CBC mode. Here is the code:

from Crypto import AES
import hashlib

text_to_encrypt = 'This is a encrypted message.'
key = '**********abcdef'
mode = AES.MODE_CBC
encryptor = AES.new(key, mode)

ciphertext = encryptor.encrypt(text)

When I run the code above, I am told that the IV must be 16 bytes long. I'm assuming that the IV (I know that means "Initialization Vector") is either the key OR something else I can set. But I don't know how or what to do.

Does anyone see what is wrong with the code above and could suggest ways to make it work? I've spent a lot of time googling around and nothing comes up specific to my problem.

posted Aug 18, 2013 by Majula Joshi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Check if following link helps? It looks to me like you aren't defining an initialization vector at all.

http://stackoverflow.com/questions/14716338/pycrypto-how-does-the-initialization-vector-work

answer Aug 18, 2013 by Deepankar Dubey
+1 vote

You don't say exactly what module you're using. I'm assuming https://www.dlitz.net/software/pycrypto/api/current/, yes?

There's a good explanation of CBC in Wikipedia: http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29

In a nutshell, AES-CBC works is to break the input up into 16-byte blocks. Each 16-byte block of input plaintext is encrypted to yield a 16-byte block of ciphertext. This 16-byte ciphertext block is also fed
forward into the encryption step for the next block.

The IV is just the feed-forward block used for the first step. So, it has to be the same size as the cipher block (i.e. 16 bytes).

AES can have different length keys (128, 192, or 256 bits), but the size of the cipher block is always 256 bits. This can get confusing because most people just use a 256 bit key and incorrectly assume that the key size and block size are somehow related. They're not. When you say:

answer Aug 18, 2013 by Deepak Dasgupta
Similar Questions
+2 votes

I have to write a python script which has to give CPU usage for particular process.
Ex: Lets say i have a process ABC which is running continuously for 10 hours.(Process may have 'n' number of threads.)
So,
1st) My script should have to check the CPU usage and Memory consumption for process ABC, and also for threads of it after every 5 seconds.

2nd) Script has to take an average of CPU usage and memory consumption and it has to print final result.

i have tried using this command ,

top -bn1 | grep "Cpu(s)" | \
           sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
           awk '{print 100 - $1"%"}'

Can anyone help in find out?

+1 vote

I want to find the maximal number of elements contained in a nested dictionary, e.g.

data = {
 'violations':
 {
 'col1': {'err': [elem1, elem2, elem3]},
 'col2': {'err': [elem1, elem2]}
 }
 }

so to find the maximal number of elements in the lists for key 'err' in key 'col1' and 'col2'. Also key 'violations' may contain many keys (e.g. 'col1' , 'col2', 'col3' etc), so what's the best way to do this (using a loop)?

max = 0for col in data.violations:
 if max < len(data.violations.col.err):
 max = len(data.violations.col.err)
+2 votes

I saw three different types of quotes in python. For me all gave same result.
Can anyone explain what could be the reason of having three different quotes in python and which one is used in what circumstances ?

...