Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用256块大小时出现字符串大小的Python AES CBC错误_Python_Encryption_Aes - Fatal编程技术网

使用256块大小时出现字符串大小的Python AES CBC错误

使用256块大小时出现字符串大小的Python AES CBC错误,python,encryption,aes,Python,Encryption,Aes,我目前正在试验各种加密技术,但遇到了一个我无法解决的问题。我尝试在cbc模式下使用pycrypto.cipher的AES实现,块大小为256。所有块大小直到128都可以正常工作,但上面给出的错误如下: 32 1536 Traceback (most recent call last): File "ttt.py", line 38, in <module> crypter.encryptB64('hallo123', randomstring(size=1024))

我目前正在试验各种加密技术,但遇到了一个我无法解决的问题。我尝试在cbc模式下使用pycrypto.cipher的AES实现,块大小为256。所有块大小直到128都可以正常工作,但上面给出的错误如下:

32
1536
Traceback (most recent call last):
  File "ttt.py", line 38, in <module>
    crypter.encryptB64('hallo123', randomstring(size=1024))
  File "ttt.py", line 28, in encryptB64
    return base64.b64encode(self.encrypt(key.encode(), value)).decode()
  File "ttt.py", line 19, in encrypt
    crypted = cipher.encrypt(self.pkcs5_pad(value))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pycrypto-2.6.1-py3.6-macosx-10.6-intel.egg/Crypto/Cipher/blocka
lgo.py", line 244, in encrypt
    return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length

根据定义,AES的块大小为128位。不多也不少。如果块大小是256位,那么它不是AES,而是Rijndael

import base64
import secrets
import hashlib
from Crypto.Cipher import AES
from Crypto import Random

class AesCrypt256:
    BLOCK_SIZE = 256
    def pkcs5_pad(self,s):
        return s + (self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE) * chr(self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE)
    def pkcs5_unpad(self,s):
        return s[0:-s[-1]]
    def encrypt(self, key, value):
        iv = Random.new().read(16)
        key = hashlib.sha256(key).digest()[:self.BLOCK_SIZE]
        cipher = AES.new(key, AES.MODE_CBC, iv)
        print(len(key))
        print(len(self.pkcs5_pad(value)))
        crypted = cipher.encrypt(self.pkcs5_pad(value))
        return iv+crypted
    def decrypt(self, key, value):
        key = hashlib.sha256(key).digest()[:self.BLOCK_SIZE]
        iv = value[:16]
        crypted = value[16:]
        cipher = AES.new(key,AES.MODE_CBC,iv)
        return self.pkcs5_unpad(cipher.decrypt(crypted))
    def encryptB64(self, key, value):
        return base64.b64encode(self.encrypt(key.encode(), value)).decode()
    def decryptB64(self, key, value):    
        return self.decrypt(key.encode(),base64.b64decode(value)).decode()


def randomstring(size=64):
    return secrets.token_urlsafe(size)

crypter = AesCrypt256()

crypter.encryptB64('hallo123', randomstring(size=1024))