Art Swri
Art Swri

Reputation: 2814

How to use AES CBC using a key longer than 256 bits in Python

Anyone have a way to encrypt/decrypt using Python to handle AES in CBC mode with a key of 1024 bits (128 bytes)? All the AES pkgs found so far seem to be limited to 256 bit keys. My crypto background is limited....

Upvotes: 0

Views: 1121

Answers (1)

Artjom B.
Artjom B.

Reputation: 61952

AES is only defined for key sizes of 128, 192 and 256 bit. The is no way to use some other key size and still call it AES. If you want to be compatible with other implementations, you will have to stick to the defined key sizes.

Two common ways to get a key of the correct size are:

  • Simply slice only a part of your key off toatch one of the valid sizes. This should only be done if the big key was created with much entropy. If not then you might make brute forcing much easier.

  • Run the big key through some hash function such as SHA-256 to get a 256 bit key. Again, if the big key has low entropy the you should regard it as a long password and run it for example through PBKDF2 with many iterations.

Upvotes: 2

Related Questions