在缩进错误上需要一些python帮助

时间:2016-02-26 14:01:52

标签: python encryption

更新抱歉特此完整的错误消息: 文件" C:\ XXX \ crypto.py",第26行   elif len(chunk)%16!= 0:

IndentationError:期望一个预期的块

这是完整的错误消息。

我的脚本引发IndentationError,你能帮我解决一下吗?

import os, random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256

def encrypt(key, filename):
        chunksize = 64*1024
        outputFile = "(encrypted)"+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(outputFile, 'wb') as outfile:
                        outfile.write(filesize)
                        outfile.write(IV)

                        while True:
                                chunk = infile.read(chunksize)

                                if len(chunk) == 0:

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

                                outfile.write(encryptor.ecrypt(chunk))

def decrypt(key. filename):
        chunksize = 64*1024
        outputFile = filename[11:]

        with open(filename, 'rb') as infile:
                filesize = long(infile.read(16))
                IV = infile.read(16)

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

                with open(outputFile, 'wb') as outfile:
                        while True:
                                chunk = infile.read(chunksize)

                                if len(chunk) == 0:
                                        break
                                outfile.write(decryptor.decrypt(chunk))
                        outfile.truncate(filesize)

def getKey(password)
        hasher = SHA256.new(password)
        return hasher.digest()

def Main():
        choice = raw_input("Would you like to (E)ncrypt or (D)ecrypt?: ")

        if choice == 'E':
                filename = raw_input("File to encrypt: ")
                password = raw_input("Password: ")
                encrypt(getKey(password), filename)
                print "Done."
        elif choice == 'D':
                filename = raw_input("File to decrypt")
                password = raw_input("Password: ")
                decrypt(geetkey(password), filename)
                print "Done."
        else: 
                print "No option selected, closing..."

if _name_ == '_name_':
        Main()

1 个答案:

答案 0 :(得分:4)

您在以下情况后错过了冒号:

def getKey(password)

你之后也没有:

if len(chunk) == 0:

如果您不想要任何内容​​,可以添加pass语句作为占位符。如果没有任何内容,Python会给出错误。以下内容适用:

if len(chunk) == 0:
    pass

另外,在以下一行:

def decrypt(key. filename):

这段时间必须是逗号。