Javascript 为什么node.js不删除python代码加密的消息解密上的填充

Javascript 为什么node.js不删除python代码加密的消息解密上的填充,javascript,python,encryption,pycrypto,cryptojs,Javascript,Python,Encryption,Pycrypto,Cryptojs,我试图用Python加密一些内容,然后在nodejs应用程序中解密,反之亦然 import base64 import hashlib from Crypto import Random from Crypto.Cipher import AES def Encrypt(self, PlainText, SecurePassword): pw_encode = SecurePassword.encode('utf-8') text_encode= Pla

我试图用Python加密一些内容,然后在nodejs应用程序中解密,反之亦然

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

    def Encrypt(self, PlainText, SecurePassword):
        pw_encode = SecurePassword.encode('utf-8')
        text_encode= PlainText.encode('utf-8')

        key = hashlib.md5(pw_encode).digest()
        iv = Random.new().read(16)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        pad_text = pkcs7.encode(text_encode)
        msg = iv + cipher.encrypt(pad_text)

        EncodeMsg = base64.b64encode(msg)
        return EncodeMsg

    def Decrypt(self, Encrypted, SecurePassword):
        decodbase64 = base64.b64decode(Encrypted.decode("utf-8"))
        pw_encode = SecurePassword.decode('utf-8')

        iv = decodbase64[:AES.block_size]
        key = hashlib.md5(pw_encode).digest()

        cipher = AES.new(key, AES.MODE_CBC, iv)
        msg = cipher.decrypt(decodbase64[AES.block_size:])
        pad_text = pkcs7.decode(msg)

        decryptedString = pad_text.decode('utf-8')
        return decryptedString;
这是pcks7填充

import StringIO
import binascii


def decode(bytestring, k=16):
    nl = len(bytestring)
    val = int(binascii.hexlify(bytestring[-1]), 16)
    if val > k:
        raise ValueError('Input is not padded or padding is corrupt')

    l = nl - val
    return bytestring


def encode(bytestring, k=16):
    l = len(bytestring)
    output = StringIO.StringIO()
    val = k - (l % k)
    for _ in xrange(val):
        output.write('%02x' % val)
    return bytestring + binascii.unhexlify(output.getvalue())
下面是Node.js代码

const crypto = require('crypto');

var AESalgoProv = 'aes-128-cbc';
const IV= Buffer.from([0x15, 0x14, 0x13, 0x12, 0x11,
                0x10, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00]);

    exports.AesEncryption = function(data, key) {
        var encodeKey = crypto.createHash('md5').update(new Buffer(key), 'utf-8').digest();
        var cipher = crypto.createCipheriv(AESalgoProv, encodeKey, IV);
        var encrypted = cipher.update(data, 'utf-8', 'base64');
        encrypted += cipher.final('base64');
        return encrypted;
    };
    exports.AesDecryption = function(encryptedata, key) {
        var encodeKey = crypto.createHash('md5').update(new Buffer(key), 'utf-8').digest();
        var decipher = crypto.createDecipheriv(AESalgoProv, encodeKey, IV);
        var decoded  = decipher.update(encryptedata,'base64', 'utf8');

        decoded += decipher.final('utf8');
        return decoded;
    };
下面是Node.js解密时的输出

�����!����̪�T�范例

正如您在上面看到的,我使用了我加密的解密消息“示例”。我还得到了这个奇怪的符号,它看起来像是node.js未能解码的填充。为什么会发生这种情况,我该如何解决?我不认为我做错了什么,我无法理解

这是解码时的Python输出

File"C:\Users\user\...\pkcs7.py", line 7, in decode
    val = int(binascii.hexlify(text[-1]), 16)
IndexError: string index out of range