0

I got a problem with aes in python 2.7

import pyelliptic iv = pyelliptic.Cipher.gen_IV('aes-256-cfb') ctx = pyelliptic.Cipher("secretkey", iv, 1, ciphername='aes-256-cfb') ciphertext = ctx.update('test1') ciphertext += ctx.final() ctx2 = pyelliptic.Cipher("secretkey", iv, 0, ciphername='aes-256-cfb') 

Now I don't know how to send this msg to server, and decrypt it on server, because I don't know the IV and my server can't decrypt it. The server has the secret key.

1
  • I wrote everithing myself Commented Nov 12, 2014 at 15:39

1 Answer 1

4

The IV does not need to be kept secret, but it needs to unique (random) for every encrypt operation with the same key.

Many implementations just add the IV bytes to the front of the ciphertext. You have to know how long the IV is for your implementation so that you can slice it off before decrypting.

# encrypt ciphertext = iv + ciphertext # decrypt blocksize = pyelliptic.Cipher.get_blocksize('aes-256-cfb') iv = ciphertext[0:blocksize] ciphertext = ciphertext[blocksize:] 

From the code it is apparent that the IV is generated in the same size as the cipher blocksize, so it is safe to slice a block from the ciphertext to get the IV.

Sign up to request clarification or add additional context in comments.

2 Comments

The IV is often the same size as the block size, it certainly is for CFB. So it is often beneficial to use something like Cipher.blocksize instead of a hard coded literal if such a property exists.
@owlstead Thanks, I edited the correct blocksize in.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.