Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Python PyScriptodome AES ValueError(“PKCS”7填充不正确。)_Python_Cryptography_Aes - Fatal编程技术网

Python PyScriptodome AES ValueError(“PKCS”7填充不正确。)

Python PyScriptodome AES ValueError(“PKCS”7填充不正确。),python,cryptography,aes,Python,Cryptography,Aes,我创建了一个AES加密/解密包装器。它与示例键一起工作,如: 键=b'0123456789abcdef0123456789abcdef' 但是如果我生成这样的随机AES密钥(从字符串) 然后返回此错误: 文件“C:\Python36\lib\site packages\Crypto\Util\Padding.py”,第93行, 在联署 raise VALUE ERROR(“PKCS#7填充不正确。”)VALUE ERROR:PKCS#7填充不正确 你知道为什么它会返回这个,并使用一个示例密钥吗?

我创建了一个AES加密/解密包装器。它与示例键一起工作,如:

键=b'0123456789abcdef0123456789abcdef'

但是如果我生成这样的随机AES密钥(从字符串)

然后返回此错误:

文件“C:\Python36\lib\site packages\Crypto\Util\Padding.py”,第93行, 在联署 raise VALUE ERROR(“PKCS#7填充不正确。”)VALUE ERROR:PKCS#7填充不正确


你知道为什么它会返回这个,并使用一个示例密钥吗?

我设法修复了它:如果您的密钥不匹配,您会收到这个错误消息。

我认为问题在于我的密钥生成。对于AES,我需要一个固定大小的密钥,我想从整数生成一个密钥,但现在我做错了。
from Crypto.Cipher import AES

import hashlib

def encryptString(plaintext, key):
    # Encryption#
    plaintext = Padding.pad(plaintext, AES.block_size);

    iv = get_random_bytes(AES.block_size)
    print('How many:' , sys.getsizeof(key));
    cipher = AES.new(key, AES.MODE_CBC, iv)

    ciphertext = cipher.encrypt(plaintext);
    return (iv + ciphertext).hex();

def decryptString(ciphertextHex, key):
    ciphertext = binascii.unhexlify(ciphertextHex)
    iv = ciphertext[:AES.block_size]
    ciphertext = ciphertext[AES.block_size:]

    cipher = AES.new(key, AES.MODE_CBC, iv)

    plaintext = cipher.decrypt(ciphertext)
    plaintext = Padding.unpad(plaintext, AES.block_size)

    return plaintext.decode('utf-8');
def convertKey(self,key):
    return hashlib.sha256(key.encode()).digest();