arun-r
arun-r

Reputation: 3381

File Decryption not working in Node when encrypted from php

It's solved finally, you can see my answers below File Decryption not working in Node when encrypted from php PHP Code to Encrypt

<?php

$key = "f9036c20bdb656106fd176d260878c63";
$iv = "7152201381f54b46";




exec('openssl enc -aes-256-cbc   -K '.$key.' -iv '.$iv.' -in a.txt -out b.txt');



exec('openssl enc -d -aes-256-cbc  -K '.$key.' -iv '.$iv.' -in b.txt -out outr.txt');
?>

Decryption works fine in PHP

JS code for Decryption both the below approach is not working

var CryptoJS = require('crypto-js');
var key ="f9036c20bdb656106fd176d260878c63";

var iv1 = "7152201381f54b46";


var text = require('fs').readFileSync('../b.txt');

var bytes  = CryptoJS.AES.decrypt(text,key,{iv:iv1, mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7 });

console.log(CryptoJS.enc.Utf8.stringify(bytes));
require('fs').writeFile('../out.txt', CryptoJS.enc.Utf8.stringify(bytes), function (err) {
        if (err) {
            return console.error(err);
        }
    });

Also tried with crypto no luck

    const crypto = require('crypto');
const fs = require('fs');

var secret = "f9036c20bdb656106fd176d260878c63";
const buf_secret = Buffer.from(secret);

var iv = "7152201381f54b46";
const buf_iv = Buffer.from(iv);


const decipher = crypto.createCipheriv('aes-256-cbc', buf_secret, buf_iv);
decipher.setAutoPadding(true);
fs.readFile('../b.txt', function (err, data) {
    if (err) {
        return console.log(err);
    }
    const buf_data = Buffer.from(data);
    console.log(buf_data);
    let decrypted = decipher.update(buf_data, 'utf8');
    decrypted += decipher.final('utf8');
    console.log(decrypted);

});

I am sure some padding issue is there, can someone point out what error its having?

Upvotes: 0

Views: 128

Answers (1)

arun-r
arun-r

Reputation: 3381

It's solved. The problem is PHP openssl accepts key and iv as hex. For openssl256 key length should be 64 and iv length should be 32, but in PHP key length was 32 and iv length was 16 which is for openssl128, so PHP is adding trailing zeros. In JS after adding trailing zeros and considering it as hex its working fine.

const crypto = require('crypto');
const fs = require('fs');
const key_size = 64;
const iv_size = 32;

var secret = "f9036c20bdb656106fd176d260878c63";
secret = pad(secret,key_size,"0"); //pad with trailing zeros

const buf_secret = Buffer.from(secret,'hex');

var iv = "7152201381f54b46";
iv = pad(iv,iv_size,"0");//pad with trailing zeros

const buf_iv = Buffer.from(iv,'hex');



const decipher = crypto.createDecipheriv('aes-256-cbc', buf_secret, buf_iv);
decipher.setAutoPadding(true);

const input = fs.createReadStream('../b.txt');
const output = fs.createWriteStream('../decrypted.txt');

input.pipe(decipher).pipe(output);

//content if you want instead of direct writing
//fs.readFile('../b.txt', function (err, data) {
//    if (err) {
//        return console.log(err);
//    }
//    const buf_data = Buffer.from(data);
//    console.log(buf_data);
//    let decrypted = decipher.update(buf_data, 'utf8');
//    decrypted += decipher.final('utf8');
//    console.log(decrypted);
//
//});

//for padding trailing zeros
function pad(value, width, padchar) {

    while (value.length < width) {
        value += padchar;
    }
    return value;
}

Upvotes: 1

Related Questions