简体   繁体   English

Python中的加密/解密; 解密不起作用

[英]Encryption/Decryption in Python; decryption not working

I am trying to decrypt some encrypted .jpg images using python Crypto 我正在尝试使用python Crypto解密一些加密的.jpg图像

def decrypt(input_filename, output_filename, password, cipher):
       ciphers = ['AES_ECB']
       key = generate_key(password)
       #Loading Pixel Data
       im = Image.open(input_filename)
       pix = im.load()
       rgb_im = im.convert('RGB')
       size = im.size
       image = Image.new('RGB', im.size)
       pix1=image.load()
       print "Filesize: " + str(size)
       for x in range(256):
          for y in range (256):
              pixel = rgb_im.getpixel((x, y))
              print pixel#DEBUG
              if cipher == ciphers[0]:
                 print x,y
                 pix1[x,y] = decryptPixel(pixel, key)
     image.save(output_filename)

But I've got this error: 但是我有这个错误:

       17             if cipher == ciphers[0]:
       18                 print x,y
  ---> 19                 pix1[x,y] = decryptPixel(pixel, key)
       20     image.save(output_filename)

OverflowError: Python int too large to convert to C long

Could you please help me to figure out the problem? 您能帮我解决问题吗?

def decryptPixelComponent(component, key):
     decryptor = AES.new(key, AES.MODE_ECB)
     component_hash = hashlib.sha256(str(component)).digest()
     component = decryptor.decrypt(component_hash).encode('hex')
     #print int(component, 16)
     return int(component, 16)
def decryptPixel(pixel, key):
        (red, green, blue) = pixel
        return (decryptPixelComponent(red, key),
        decryptPixelComponent(green, key),
        decryptPixelComponent(blue, key))

The result from "decryptPixel" is tuple and pix1 is tuple as well. 来自“ decryptPixel”的结果是元组,而pix1也是元组。 However, when I try to update the pix1 I got this overflow error. 但是,当我尝试更新pix1时,出现了此溢出错误。

Looking at the "decryptPixelComponent" function it seems you are decrypting the hash value of the pixel ("component_hash"). 查看“ decryptPixelComponent”函数,似乎您正在解密像素的哈希值(“ component_hash”)。 I think you should try to decrypt without the hash ("component"). 我认为您应该尝试不使用散列(“ component”)进行解密。

def decryptPixelComponent(component, key):
     decryptor = AES.new(key, AES.MODE_ECB)
     component = decryptor.decrypt(component).encode('hex')
     #print int(component, 16)
     return int(component, 16)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM