简体   繁体   English

“类型错误:Object 类型<class 'str'>加密图像时无法传递给 C 代码</class>

[英]"TypeError: Object type <class 'str'> cannot be passed to C code when encrypting an image

I am trying to encrypt an image file, but when I try to execute my code, I get the error:我正在尝试加密图像文件,但是当我尝试执行代码时,出现错误:

TypeError: Object type <class 'str'> cannot be passed to C code TypeError: Object 类型 <class 'str'> 无法传递给 C 代码

I will really appreciate any help on how to fix this error, I'm a beginner in using python.我将非常感谢有关如何解决此错误的任何帮助,我是使用 python 的初学者。

Here is my code:这是我的代码:

from PIL import Image
#from PIL.Image import core as image
import os
import sys
from Crypto.Cipher import AES

Block_size=16 
IV_size=16 
def encrypt_file(input_file,output_file,cipher_mode):
          input_img=Image.open('/content/Linux-icon.png')
          key="770A8A65DA156D24EE2A093277530142"
          if cipher_mode=='CBC':
               mode=AES.MODE_CBC
          #elif cipher_mode=='CBC':
              # mode=AES.MODE_CBC
          else:
              mode=AES.MODE_CFB
          i=os.urandom(IV_size)
          aes=AES.new(key,mode,i)
          img_str=input_img.tostring()
          #Pad the image string to the input block size
          img_pad_lenght=Block_size-len(img_str)/Block_size

          img_str+=img_pad_lenght*"~"
          #generate the encrypted image string
          encrypted_img_str=aes.encrypt(encode(img_str))

          #create an image from the encrypted string
          encrypted_img=Image.frombuffer('RGB',input_img.size, encrypted_img_str,'raw','RGB',0,1)
          #create and save the output image
          encrypted_img.save('/content/Linux-icon1.png','PNG')
          print("Encrypted using AES in " + cipher_mode + " mode and saved to \"" + output_filename + "\"!")

Thanks in advance提前致谢

Your code have some issues read the code comments.您的代码有一些问题阅读代码注释。 As a side note: try to encrypt BMP images in ECB mode to see why it's bad to use encryption in ECB mode.附带说明:尝试在 ECB 模式下加密BMP图像,看看为什么在 ECB 模式下使用加密不好。

# deleted non needed imports
from PIL import Image
import os
from Crypto.Cipher import AES

Block_size = 16
IV_size = 16
def encrypt_file(input_file,output_file,cipher_mode):
            # use function input file name not a fixed one
          input_img = Image.open(input_file)

          # must be in bytes
          key = b"770A8A65DA156D24EE2A093277530142"
          if cipher_mode=='CBC':
               mode = AES.MODE_CBC
          else:
              mode = AES.MODE_CFB

          iv = os.urandom(IV_size)
          aes = AES.new(key, mode, iv)
          img_str = input_img.tobytes()

            # To get padding needed use mod operator
          img_pad_lenght = Block_size - (len(img_str) % Block_size)

            # Add padding of null bytes
          img_str += img_pad_lenght * b"\0"
          print(len(img_str))
            # There is no built in encode function in python encode is a string method
            # must be applied to a string
          encrypted_img_str = aes.encrypt(img_str)

          encrypted_img = Image.frombuffer('RGB', input_img.size, encrypted_img_str, 'raw', 'RGB', 0, 1)
          encrypted_img.save(output_file, 'PNG')

            # there is no variable as outputfilename only output_file
          print("Encrypted using AES in " + cipher_mode + " mode and saved to \"" + output_file + "\"!")

暂无
暂无

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

相关问题 类型错误:对象类型<class 'str'>不能传递给 C 代码 - TypeError: Object type <class 'str'> cannot be passed to C code Object型<class 'tuple'>在 django 中加密文件时无法传递给 C 代码</class> - Object type <class 'tuple'> cannot be passed to C code when encrypting file in django PyCryptoDome版本3.6.6引发TypeError:对象类型 <class 'str'> 无法传递给C代码 - PyCryptoDome Version 3.6.6 raises an TypeError: Object type <class 'str'> cannot be passed to C code python 中的 DES 加密错误(TypeError: Object 类型<class 'str'>不能传给 C 码)</class> - DES Encryption in python error(TypeError: Object type <class 'str'> cannot be passed to C code) Object型<class 'str'>无法传递给 C 代码 - 虚拟环境</class> - Object type <class 'str'> cannot be passed to C code - virtual environment 类型错误:无法连接类型为 &#39; 的对象<class 'str'> &#39;; 只有 Series 和 DataFrame 对象有效 - TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid TypeError: 无法连接 ' 类型的 object<class 'str'> '; 只有 Series 和 DataFrame objs 是有效的我得到了这个错误</class> - TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid I got this error 当我尝试运行我的代码时,我不断收到 TypeError: 'str' object 不能解释为 integer - I keep getting TypeError: 'str' object cannot be interpreted as an integer when i try to run my code pandas 中的 pd.concat 给出 TypeError: cannot concatenate object of type '<class 'str'> '; 只有 Series 和 DataFrame 对象有效</class> - pd.concat in pandas is giving a TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid 使用套接字时,Python 3 [TypeError:&#39;str&#39;对象无法解释为整数] - Python 3 [TypeError: 'str' object cannot be interpreted as an integer] when working with sockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM