I am trying to decrypt a PDF file using node js,PDF file encrypted by third party using C#
I am having a hard time because I keep getting this error:
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:IMPAESENCRYPTIONindex.js:91:11)
at D:IMPAESENCRYPTIONindex.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'
}
we are using below code for encryption(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, 0, 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();
}
}
}
}
we are using below code for decryption(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();
Unable to decrypt a pdf file Using 'aes-256-cbc' algorithm in node js
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…