转换并在python 3中运行的Python 2加密程序返回错误

时间:2018-07-16 13:01:28

标签: python python-3.x encryption pycrypto

  

这是this问题的延续。请不要将此问题标记为重复问题,因为它存在我需要解决的其他错误。

链接的TL; DR:

所以我当时在问我的加密程序在python中出现Unicode错误,有人告诉我只需在utf-8中对密码进行编码即可。


现在我有第二个问题,它说IV不是16字节,因此我通过在IV后面放置print(len(IV))并进行了3次测试后,在中间将它只返回16次来进行检查。字符,并且结束字符大于16(例如:37、35、28等)。

如何解决这个问题,使IV始终返回16个字节?

完整错误:

Traceback (most recent call last):
  File "/home/pi/Desktop/Projects/FyleCript/Dev Files/encryption.py", line 77, in <module>
    encrypt(SHA256.new(password.encode('utf-8')).digest(), str(Tfiles))
  File "/home/pi/Desktop/Projects/FyleCript/Dev Files/encryption.py", line 17, in encrypt
    encryptor = AES.new(key, AES.MODE_CBC, IV)
  File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 94, in new
    return AESCipher(key, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long

代码:

#-*- 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 in ("E", "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.encode('utf-8')).digest(), str(Tfiles))
                        print("Done encrypting %s" %str(Tfiles))
                        os.remove(Tfiles)


elif choice in ("D", "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.decode('utf-8')).digest(), filename)
                print("Done decrypting %s" %filename)
                os.remove(filename)

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

好吧,让我们看一下IV可能包含的内容:

IV = ''

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

让我们看看range(0, 0xff)中的一个字符消耗了多少字节:

>>> [len(chr(i).encode()) for i in range(0, 0xff)]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

这是问题的根源:您假设每个字符长一个个字节,但这不是事实。

您可以使用以下代码生成N个字节的随机IV:

import os

N = 16
IV = os.urandom(N)

代码中的另一个问题是,您以'rb'模式打开所有文件,该模式代表“读取二进制文件”,但尝试向其中写入str实例,例如您的{ {1}}。那是行不通的,因为在这种模式下,您只能读写IV,而不能读写bytes。在我计算str的解决方案中,这个问题完全消失了。

答案 1 :(得分:1)

您尚未将GetSession()字符串转换为字节字符串。在Python 3中,IV不是字节字符串,而是字符串。 str摆脱了如何将字符表示为字节的概念。

您需要将您的str变量(也许我没有检查过其他变量)转换为IV的实例。在Python 3中创建字节串也容易一些。

bytes