2016-02-26 28 views
0

更新對不起特此完整的錯誤消息,一些Python幫助: 文件 「C:\ XXX \ crypto.py」 26行 ELIF LEN(塊)%16 = 0:需要對indentationerror

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() 
+0

當您尋求錯誤搜索幫助時,請始終包含完整的錯誤消息。它包含有關錯誤發生的原因和*哪裏的信息。 – timgeb

+0

因爲我是新的,我不知道什麼是空的塊。你知道如何解決它嗎?該錯誤說,順便說一句,它在第26行。這一個:elif len(塊)%16!= 0 :.我需要改變什麼?謝謝! – dyionisus

+0

看到我發佈的答案,它解決了這個問題。 –

回答

4

你缺少一個冒號後:

def getKey(password) 

你也沒有什麼後:

if len(chunk) == 0: 

如果你不想要的東西在那裏,你可以添加一個pass聲明作爲佔位符。如果沒有任何東西,Python會給出錯誤。下面的工作:

if len(chunk) == 0: 
    pass 

此外,在下面一行:

def decrypt(key. filename): 

週期必須是一個逗號。

+0

該死的,你是否注意到了這些東西,或者你是否將它粘在了它上面?好的眼睛,如果它是前者。 –

+0

@Eddo我只是做了一個快速的視覺掃描。 –

+0

謝謝,就像你所說的那樣。現在我有一個新的錯誤:文件「C:\ crypto.py」,第31行def decrypt(key.filename):SyntaxError:無效的語法。你知道該怎麼辦? – dyionisus