【问题标题】:Encrypting image files in python3 using RSA algo使用 RSA 算法在 python3 中加密图像文件
【发布时间】:2019-05-04 20:12:49
【问题描述】:

我正在使用 RSA 算法在 python3 中加密图像,但是当我运行代码时出现一些错误; 错误是:

 File "encrypt_blob.py", line 59, in <module>
 encrypted_blob = encrypt_blob(unencrypted_blob, public_key)
 File "encrypt_blob.py", line 37, in encrypt_blob
 chunk += " " * (chunk_size - len(chunk))
 TypeError: can't concat str to bytes

我之前已经生成了密钥,并使用此脚本中的密钥来加密图像文件。但我收到了这个错误。因为我在博客上找到了这段代码,我认为代码是用python2编写的,但我使用的是python3,我不知道如何解决问题

代码是:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import zlib
import base64

def encrypt_blob(blob, public_key):
  #Import the Public Key and use for encryption using PKCS1_OAEP
  rsa_key = RSA.importKey(public_key)
  rsa_key = PKCS1_OAEP.new(rsa_key)

  #compress the data first
  blob = zlib.compress(blob)

  #In determining the chunk size, determine the private key length used in bytes
#and subtract 42 bytes (when using PKCS1_OAEP). The data will be in encrypted
#in chunks
chunk_size = 470
print(type(chunk_size))
offset = 0
end_loop = False
encrypted =  ""
print(type(encrypted))

while not end_loop:
    #The chunk
    chunk = (blob[offset:offset + chunk_size])

    #If the data chunk is less then the chunk size, then we need to add
    #padding with " ". This indicates the we reached the end of the file
    #so we end loop here
    if len(chunk) % chunk_size != 0:
        end_loop = True
        chunk += " " * (chunk_size - len(chunk))

    #Append the encrypted chunk to the overall encrypted file
    encrypted += (rsa_key.encrypt(chunk))

    #Increase the offset by chunk size
    offset += chunk_size

#Base 64 encode the encrypted file
return base64.b64encode(encrypted)

#Use the public key for encryption
fd = open("public_key.pem", "rb")
public_key = fd.read()
fd.close()

#Our candidate file to be encrypted
fd = open("img.jpg", "rb")
unencrypted_blob = fd.read()
fd.close()

encrypted_blob = encrypt_blob(unencrypted_blob, public_key)

#Write the encrypted contents to a file
fd = open("encrypted_img.jpg", "wb")
fd.write(encrypted_blob)
fd.close()

期待建议。谢谢。

【问题讨论】:

    标签: python python-3.x python-2.7 rsa public-key-encryption


    【解决方案1】:

    存在一些类型转换错误并且未提及编码


    使用的评论:#Append the encrypted chunk to the overall encrypted file

    错误: encrypted += (rsa_key.encrypt(chunk))

    正确: encrypted += str(rsa_key.encrypt(chunk))


    使用的评论:#Base 64 encode the encrypted file

    错误: return base64.b64encode(encrypted)

    正确: return base64.b64encode(encrypted.encode('ascii'))


    整个代码:

    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    import zlib
    import base64
    def encrypt_blob(blob, public_key):
        rsa_key = RSA.importKey(public_key)
        rsa_key = PKCS1_OAEP.new(rsa_key)
        blob = zlib.compress(blob)
        chunk_size = 470
        offset = 0
        end_loop = False
        encrypted =  ""
        while not end_loop:    
            chunk = blob[offset:offset + chunk_size]
            if len(chunk) % chunk_size != 0:
                end_loop = True
                chunk += bytes(" " * (chunk_size - len(chunk)),'utf-8')
            encrypted += str(rsa_key.encrypt(chunk))
            offset += chunk_size
        #print(type(encrypted))
        return base64.b64encode(encrypted.encode('ascii'))
    
    fd = open("public_key.pem", "rb")
    public_key = fd.read()
    fd.close()
    
    fd = open("img.jpg", "rb")
    unencrypted_blob = fd.read()
    fd.close()
    
    encrypted_blob = encrypt_blob(unencrypted_blob, public_key)
    fd = open("encrypted_img.jpg", "wb")
    fd.write(encrypted_blob)
    fd.close()
    

    【讨论】:

      【解决方案2】:

      原始代码是为 Python2 编写的。 在 Python3 中,您不能将 bytes 对象隐式转换为 str 。 所以基本上要解决关于“将str(不是“字节”)连接到str”的错误,你必须将变量声明为字节字符串。

      1. 加密 = b""
      2. chunk += b" " * (chunk_size - len(chunk))

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-23
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        • 2019-05-04
        相关资源
        最近更新 更多