0

I am trying to do basic encryption in Python, in below program I encrypt whatever user types in, and then I am displaying it back to the user after decryption. I am using the pyCrypto library which I downloaded from here: http://www.voidspace.org.uk/python/modules.shtml#pycrypto

Below is the code I have written so far:

from Crypto.Cipher import AES

AES_key = AES.new('This is a key')
#message = "The answer is no"
message = input("Enter text:")

ciphertext = AES_key.encrypt(message)
print (ciphertext)

decrypted_message = AES_key.decrypt(ciphertext)
print (decrypted_message)

The problem is when I taken input from the user, my code does not work, but when I give static input the one which I have commented my code works fine.

Can anyone please help what should I do so that my accepts user input and encrypts it?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
jubins
  • 1
  • 1
  • 2

1 Answers1

0

AES is a block cipher algorithm so it requires you to have the text to be a multiple of 16 bytes. your message "The answer is no" is of 16 bytes.But the input wouldn't be so you can use the MOD_CFB of the AES to get around this.

from Crypto.Cipher import AES
# Encryption
encryption_suite = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
cipher_text = encryption_suite.encrypt("A really secret message. Not for prying eyes.")

# Decryption
decryption_suite = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
plain_text = decryption_suite.decrypt(cipher_text)

this would work for user input.

For more info on IV(initialization vectors) go Here

GIRISH RAMNANI
  • 614
  • 6
  • 18
  • Thanks Girish, but I tried this and its not working. – jubins Mar 04 '16 at 06:52
  • which python version are you using? – GIRISH RAMNANI Mar 04 '16 at 06:52
  • Using a fixed IV for a streaming mode like CFB is extremely insecure. It leads to a many-time pad when you use the same key and IV multiple times. The IV has to be chosen randomly (actually uniquely) and doesn't have to be secret. It can be sent along with the ciphertext to be used during decryption. – Artjom B. Mar 04 '16 at 09:35