1

I created a function, that uses Crypto.Cipher:

    import os
    from Crypto.Cipher import AES

    def encrypt_pass(user_entered_plaintext):
        encrypt_setup = AES.new(os.urandom(32), AES.MODE_CBC, os.urandom(16))
        plaintext = user_entered_plaintext
        ciphertext = encrypt_setup.encrypt(plaintext)

        print(ciphertext)
        return ciphertext

if I try to run this, like:

encrypt_pass('Test')

Im getting this error:

ValueError: Input strings must be a multiple of 16 in length

if I try changing:

encrypt_setup = AES.new(os.urandom(32), AES.MODE_CBC, os.urandom(16))

to

encrypt_setup = AES.new(os.urandom(32), AES.MODE_CBC, os.urandom(8))

I get:

ValueError: IV must be 16 bytes long

How can I force this input string minimum to 8?

CodeTalk
  • 3,571
  • 16
  • 57
  • 92
  • If someone runs across this thread, here is a good article to follow regarding this topic: http://www.davychiu.com/blog/aes-encryptiondecryption-in-python.html – CodeTalk Dec 22 '15 at 22:31

1 Answers1

2

Just specify PKCS#7 padding. You will have to add that prior to encryption and remove it after decryption because to bozos who wrote pycrypto did not add padding as an option. (And I thought PHP mcrypt was bad!) See the link by CodeTalk.

AES encryption is block based and the block size is 16-bytes so if the input data is not a multiple of the block size padding must be added.

In the second attempt you change the iv to 8-bytes but it must be the block size of 16-byte.

But you are not saving the key or iv so you will not be able to later decrypt the data. You need to use variable for them.

zaph
  • 111,848
  • 21
  • 189
  • 228