错误:0606508A:数字包络例程:EVP_DecryptFinal_ex:数据不是块长度的倍数

问题描述

我正在尝试使用 node js 解密 PDF 文件,第三方使用 C# 加密的 PDF 文件

我很难过,因为我不断收到此错误


    Error: error:0606508A:digital envelope routines:EVP_DecryptFinal_ex:data not multiple of block length
        at Decipheriv._flush (internal/crypto/cipher.js:141:29)
        at Decipheriv.prefinish (_stream_transform.js:142:10)
        at Decipheriv.emit (events.js:311:20)
        at Decipheriv.EventEmitter.emit (domain.js:482:12)
        at prefinish (_stream_writable.js:676:14)
        at finishMaybe (_stream_writable.js:684:5)
        at endWritable (_stream_writable.js:704:3)
        at Decipheriv.Writable.end (_stream_writable.js:633:5)
        at decryptAES (D:\IMP\AESENCRYPTION\index.js:91:11)
        at D:\IMP\AESENCRYPTION\index.js:189:22
    Emitted 'error' event on Decipheriv instance at:
        at done (_stream_transform.js:209:19)
        at _stream_transform.js:143:7
        at Decipheriv._flush (internal/crypto/cipher.js:143:5)
        at Decipheriv.prefinish (_stream_transform.js:142:10)
        [... lines matching original stack trace ...]
        at Decipheriv.Writable.end (_stream_writable.js:633:5) {
      library: 'digital envelope routines',function: 'EVP_DecryptFinal_ex',reason: 'data not multiple of block length',code: 'ERR_OSSL_EVP_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH'
    }

我们使用以下代码进行加密(C#)

public async Task<byte[]> Encrypt(byte[] textToEncrypt,string masterKeyId)
        {
            var kmsClient = new AmazonKeyManagementServiceClient("AKIATQHPMFI7PCSZ2EPR","9PKzz5BdJgpcs1dGiamwzsyhl3rQfWzxwkJGMpfF",Amazon.RegionEndpoint.USEast1);

            using (var algorithm = Aes.Create())
            {
                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    // Generates the data key under the master key
                    var dataKey = await kmsClient.GenerateDataKeyAsync(new GenerateDataKeyRequest
                    {
                        KeyId = masterKeyId,KeySpec = DataKeySpec.AES_256
                    });

                    msEncrypt.WriteByte((byte)dataKey.CiphertextBlob.Length);
                    dataKey.CiphertextBlob.copyTo(msEncrypt);
                    algorithm.Key = dataKey.Plaintext.ToArray();

                    // Writing algorithm.IV in output stream for decryption purpose.
                    msEncrypt.Write(algorithm.IV,algorithm.IV.Length);

                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform encryptor = algorithm.CreateEncryptor(algorithm.Key,algorithm.IV);

                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
                    {
                        using (MemoryStream input = new MemoryStream(textToEncrypt))
                        {
                            input.copyTo(csEncrypt);
                            csEncrypt.FlushFinalBlock();
                        }
                        return msEncrypt.ToArray();
                    }
                }
            }
        } 

我们使用以下代码进行解密(Node js)

const algorithm = 'AES-256-CBC';

    const iv = Buffer.from('00000000000000000000000000000000');
 
    decipher = crypto.createDecipheriv(algorithm,key,iv);
  decipher.setAutopadding(false)
// we are checking different ways but facing same issue.
//type 1
 decipher.write(buffer);
 decipher.end();
 return decipher.read();

//type 2

return Buffer.concat([
       decipher.update(buffer),decipher.final()
      ]).toString()

//type 3
var decrypted = decipher.update(buffer,'utf8','biary') + decipher.final('binary');
//type 4
decoded = decipher.update(buffer);
decipher += decipher.final();

无法在节点 js 中使用“aes-256-cbc”算法解密 pdf 文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)