首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用RSA算法加密python3中的图像文件

用RSA算法加密python3中的图像文件
EN

Stack Overflow用户
提问于 2018-12-03 13:59:43
回答 2查看 1.3K关注 0票数 4

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

代码语言:javascript
复制
 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,我不知道如何解决这个问题

守则是:

代码语言:javascript
复制
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()

期待着大家的建议。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-12-10 05:06:50

存在一些类型转换错误&编码没有提到

使用的评论:#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'))

整个代码:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-12-27 12:49:36

原始代码是为Python2编写的。在Python3中,不能隐式地将字节对象转换为str。因此,基本上要解决“串联str (不是”字节“)到str的错误,必须将变量声明为字节字符串。

  1. 加密= b"“
  2. 块+= b“”* (chunk_size -len(块))

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53595365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档