2017-05-08 122 views
1

我想製作一個腳本來解密我的文件,但是當我嘗試運行我的腳本時,請向我顯示此消息,我該如何解決它?AES:輸入字符串的長度必須是16的倍數

Traceback (most recent call last): File "F:\bug_bounty\decrypt.py", line 46, in File "F:\bug_bounty\decrypt.py", line 24, in decrypt File "C:\Python27\lib\site-packages\Crypto\Cipher\blockalgo.py", line 295, in decrypt return self._cipher.decrypt(ciphertext) ValueError: Input strings must be a multiple of 16 in length

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


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 


password = 'M4st3rRul3zs' 
files = allfiles(); 
for filename in files: 
    if os.path.basename(filename).startswith("(encrypted)"): 
     print "%s is already encrypted" %filename 
     pass 

    else: 
     decrypt(SHA256.new(password).digest(), filename) 
     print "Done decrypting %s" %filename 
     """os.remove(filename)""" 
+1

http://stackoverflow.com/a/14205319/3527520可能是有用的代碼應該 –

回答

0

ValueError: Input strings must be a multiple of 16 in length

這是因爲AES作品128位(16個字符)的塊。你可以考慮加入padding來解決這個問題。

+0

如何呢? (密鑰,AES.MODE_CBC,IV) –

+0

@Aron帝國谷歌'pkcs7填充'或'零填充'他們''''''都很容易實現 –

0

Crypto++ wiki

The block size is determined by AES::BLOCKSIZE. For AES, this is always 16 bytes

AES是一種分組密碼,它在16字節(128位)塊上工作。它不能使用小於或大於16字節的數據。較小的數據需要爲填充,直到它們是16字節,而較大的數據需要分成16字節的塊。

此外還有一些算法可以幫助您實現這一點(對大於密碼塊大小的數據進行處理),它們被稱爲block cipher modes of operation

看一看這個How to encrypt more than 16 bytes using AES?

相關問題