从php加密后,文件解密在Node中不起作用 [英] File Decryption not working in Node when encrypted from php

查看:155
本文介绍了从php加密后,文件解密在Node中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题终于解决了,您可以在下面查看我的答案 从php加密后,文件解密在Node中不起作用 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');
?>

解密在PHP中工作正常

Decryption works fine in PHP

用于解密的JS代码以下方法均不起作用

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?

推荐答案

已解决.问题是PHP openssl接受key和iv作为十六进制.对于openssl256,密钥长度应为64,iv长度应为32,但是在PHP中,密钥长度应为32,iv长度应为16(对于openssl128),因此PHP会添加尾随零.在JS中添加尾随零并将其视为十六进制,可以正常工作.

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;
}

这篇关于从php加密后,文件解密在Node中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆