微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决错误: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 举报,一经查实,本站将立刻删除。