1

I am trying to encrypt my data using pycrypto. I have written below code for that.

from Crypto.Cipher import AES
obj = AES.new('1234567891011123', AES.MODE_CBC, 'This is an IV456')
message = "Jeannine"
ciphertext1 = obj.encrypt(message)
print(ciphertext1)
message1 = "Jeannine"
ciphertext2 = obj.encrypt(message1)
print(ciphertext2)
obj2 = AES.new('1234567891011123', AES.MODE_CBC, 'This is an IV456')
dciphertext1 = obj2.decrypt(ciphertext1)
print(dciphertext1)
dciphertext2=obj2.decrypt(ciphertext2)
print(dciphertext2)

but i am getting below error

Traceback (most recent call last):
  File "cipher.py", line 4, in <module>
    ciphertext1 = obj.encrypt(message)
ValueError: Input strings must be a multiple of 16 in length

How can i keep control in my input string ? Input string can be of any length.

animal
  • 994
  • 3
  • 13
  • 35

1 Answers1

1

You're using AES in CBC mode, it requires the string in length that's multiple of 16, so you might need to add some padding.

Follow the steps described in this topic (this doesn't look like a duplicate to me, but the answer is useful for you).

Community
  • 1
  • 1
Fernando Cezar
  • 858
  • 7
  • 22
  • I tried `CFB` and it worked but my encrypted result is different in both cases though the input is same. why it is behaving like that ? – animal Mar 16 '17 at 14:33
  • Because they treat the blocks in different ways. Here's a more detailed description of how the modes work: http://pythonhosted.org/pycrypto/Crypto.Cipher.blockalgo-module.html#MODE_CBC – Fernando Cezar Mar 16 '17 at 14:44
  • @animal All modes are incompatible and have different properties: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation (there are much more modes, but those are the common ones) – Artjom B. Mar 16 '17 at 19:52
  • @FernandoCezar is their any way i can get same encrypted result for same input ? – animal Mar 17 '17 at 08:21
  • @ArtjomB. is their any way i can get same encrypted result for same input ? – animal Mar 17 '17 at 08:22
  • @animal not with different modes. They are different ways to hash the same input, so the output has to be different. – Fernando Cezar Mar 17 '17 at 10:20
  • if i am using same mode as i have used in my question. Than will it be possible ? – animal Mar 17 '17 at 10:25
  • Thanks @FernandoCezar for your help. Please upvote my question. – animal Mar 20 '17 at 11:22