Encryption of a JPG file using pycrypro's AES failing

纵饮孤独 提交于 2019-12-06 15:16:20

问题


Given below is the code(not complete yet) I have written to encrypt and decrypt files using python with the pycrypto module.

from Crypto.Hash import SHA256 
from Crypto.Cipher import AES 
import getpass

class ED(object):   
  def getfromuser(self,choice):
    if choice=='key':
        key=getpass.getpass('Enter AES Key (minimum 16 characters): ')
        if len(key)<16:
            print 'Key entered too short. Please try again.'
            self.getfromuser(choice)
        key=key+str(8-len(key)%8)*(8-len(key)%8)
        return key
    if choice=='IV':
        IV_seed=raw_input('Enter a seed for the IV: ')
        IV=SHA256.new()
        IV.update(IV_seed)
        IV.digest()
        return str(IV)[0:16]

  def AESEncrypt(self,key,IV,source,dest):

    f=open(source,"r")
    fstream=f.read()
    f.close()

    AES_stream=AES.new(key,AES.MODE_CBC,IV)
    AES_encrypted=AES_stream.encrypt(fstream)

    with open(dest,"w") as write_file:
        write_file.write(AES_encrypted)

  def AESDecrypt(self,key,IV,source,dest):
    f=open(source,"r")
    fstream=f.read()
    f.close()
    AES_stream=AES.new(key,AES.MODE_CBC,IV)
    AES_decrypted=AES_stream.decrypt(fstream)
    with open(dest,"w") as write_file:
        write_file.write(AES_decrypted)

When I tried to encrypt a JPG file using this I got the following error:

    AES_encrypted=AES_stream.encrypt(fstream)
    ValueError: Input strings must be a multiple of 16 in length

I tried it on an mp4 file and it worked fine: the encryption and the decryption too.

What is the cause of this error and how do I fix it?


回答1:


I found the solution. If one does not want the hassle of padding, one can use the CFB (Cipher feedback) mode as follows:

AES_stream=AES.new(key,AES.MODE_CFB,IV)



来源:https://stackoverflow.com/questions/11384658/encryption-of-a-jpg-file-using-pycrypros-aes-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!