从Python 2转换为python 3时的Python加密unicode错误

时间:2018-07-15 17:12:48

标签: python python-3.x python-2.7 pycrypto python-unicode

我找到了一些要合并到我的Python加密程序中的代码。它应该加密代码相同目录中的文件,并且我希望它以目录为目标。但是,它是用Python 2编写的,当我更改一些代码以适合Python 3时,出现以下错误:

Traceback (most recent call last):
  File "/home/pi/Desktop/Projects/FyleCript/Dev Files/encryption.py", line 77, in <module>
    encrypt(SHA256.new(password).digest(), str(Tfiles))
  File "/usr/lib/python3/dist-packages/Crypto/Hash/SHA256.py", line 88, in new
    return SHA256Hash().new(data)
  File "/usr/lib/python3/dist-packages/Crypto/Hash/SHA256.py", line 75, in new
    return SHA256Hash(data)
  File "/usr/lib/python3/dist-packages/Crypto/Hash/SHA256.py", line 72, in __init__
    HashAlgo.__init__(self, hashFactory, data)
  File "/usr/lib/python3/dist-packages/Crypto/Hash/hashalgo.py", line 51, in __init__
    self.update(data)
  File "/usr/lib/python3/dist-packages/Crypto/Hash/hashalgo.py", line 69, in update
    return self._hash.update(data)
TypeError: Unicode-objects must be encoded before hashing

但是该代码在Python 2中运行良好。我曾尝试在SO和Google上寻找类似的问题,但没有帮助。


代码:

#-*- coding:utf-8 -*-

#Python 
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
import os, random, sys, pkg_resources

def encrypt(key, filename):
        chunksize = 64 * 1024
        outFile = os.path.join(os.path.dirname(filename), "(encrypted)"+os.path.basename(filename))
        filesize = str(os.path.getsize(filename)).zfill(16)
        IV = ''

        for i in range(16):
                IV += chr(random.randint(0, 0xFF))

        encryptor = AES.new(key, AES.MODE_CBC, IV)

        with open(filename, "rb") as infile:
                with open(outFile, "wb") as outfile:
                        outfile.write(filesize)
                        outfile.write(IV)
                        while True:
                                chunk = infile.read(chunksize)

                                if len(chunk) == 0:
                                        break

                                elif len(chunk) % 16 !=0:
                                        chunk += ' ' *  (16 - (len(chunk) % 16))

                                outfile.write(encryptor.encrypt(chunk))


def decrypt(key, filename):
        outFile = os.path.join(os.path.dirname(filename), os.path.basename(filename[11:]))
        chunksize = 64 * 1024
        with open(filename, "rb") as infile:
                filesize = infile.read(16)
                IV = infile.read(16)

                decryptor = AES.new(key, AES.MODE_CBC, IV)

                with open(outFile, "wb") as outfile:
                        while True:
                                chunk = infile.read(chunksize)
                                if len(chunk) == 0:
                                        break

                                outfile.write(decryptor.decrypt(chunk))

                        outfile.truncate(int(filesize))

def allfiles():
        allFiles = []
        for root, subfiles, files in os.walk(os.getcwd()):
                for names in files:
                        allFiles.append(os.path.join(root, names))

        return allFiles


choice = input("Do you want to (E)ncrypt or (D)ecrypt? ")
password = input("Enter the password: ") 

encFiles = allfiles()

if choice == "E" or 'e':
        for Tfiles in encFiles:
                if os.path.basename(Tfiles).startswith("(encrypted)"):
                        print("%s is already encrypted" %str(Tfiles))
                        pass

                elif Tfiles == os.path.join(os.getcwd(), sys.argv[0]):
                        pass
                else:
                        encrypt(SHA256.new(password).digest(), str(Tfiles))
                        print("Done encrypting %s" %str(Tfiles))
                        os.remove(Tfiles)


elif choice == "D" or 'd':
        filename = input("Enter the filename to decrypt: ")
        if not os.path.exists(filename):
                print("The file does not exist")
                sys.exit()
        elif not filename.startswith("(encrypted)"):
                print("%s is already not encrypted" %filename)
                sys.exit()
        else:
                decrypt(SHA256.new(password).digest(), filename)
                print("Done decrypting %s" %filename)
                os.remove(filename)

else:
        print("Please choose a valid command.")
        sys.exit()

有人可以帮助我解决这个问题吗?我使用了Python 2到3工具,但仍然无法正常工作。

另外,您能解决目录问题吗?没必要,但是我想要。


编辑:我已将str替换为bytesbytearray,但它返回了相同的错误。

1 个答案:

答案 0 :(得分:2)

您的“密码”变量是一个字符串,但是<cfparam name="url.clientid" default="0"> <cfparam name="variables.response" default="#StructNew()#"> <cfset variables.response['error'] = ""> <cfif Val(url.clientid)> <cftry> <cfquery datasource="yourDSN"> UPDATE yourDBTable SET Arrived = <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">, Submission_date = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#"> WHERE ClientID = <cfqueryparam cfsqltype="cf_sql_integer" value="#url.clientid#"> </cfquery> <cfcatch> <cfset variables.response['error'] = "An error occurred whilst trying to update the database"> </cfcatch> </cftry> </cfif> <cfoutput> #SerializeJSON(variables.response)# </cfoutput> 需要字节(例如,允许使用unicode)。 Crypto.Hash.SHA256 documentation中列出了您需要的字节。

解决方案是在哈希之前将密码编码为字节。错误消息几乎就是这句话(如果您知道python 3中的所有字符串都是unicode对象):

SHA256.new

例如,解决方案是使用utf8进行编码:

TypeError: Unicode-objects must be encoded before hashing