首页 > 解决方案 > AES-256-CBC 加密 Node.Js 但无法在 PHP 中解密

问题描述

我正在尝试使用 aes-256-cbc 加密对象数组,但它没有发生:

let key = new Buffer(SSO_key, 'base64');//decoding key in base64
let IV = new Buffer(SSO_IV, 'base64');//decoding IV in base64

Text.push({
   pdf_text: get_content,//getting encrypted pdf here
   course_id: "123",
   candidate_id: "123",
   time_stamp: new Date()
 })

Text = Buffer.from(JSON.stringify(Text));

let cipher1 = crypto.createCipheriv('aes-256-cbc', key, IV);
let get_content1 = cipher1.update(Text, 'utf8', 'base64') + cipher1.final('base64');

标签: phpnode.jspdfencryptionaes

解决方案


只要SSO_key, 当编码base64为 32 字节长并且SSO_IV, 当编码base64为 16 字节长时,您的代码应该可以工作。

我尝试了下面的代码,它与您拥有的代码尽可能接近:

const crypto = require('crypto');
let key = Buffer.alloc(32, 0); //new Buffer("asdakjdsh", 'base64');//decoding key in base64
let IV = Buffer.alloc(16, 0);

let Text = [];
Text.push({
   pdf_text: "get_content",//getting encrypted pdf here
   course_id: "123",
   candidate_id: "123",
   time_stamp: new Date()
 })

Text = Buffer.from(JSON.stringify(Text));

let cipher1 = crypto.createCipheriv('aes-256-cbc', key, IV);
let get_content1 = cipher1.update(Text, 'utf8', 'hex') + cipher1.final('hex');

console.log(`==========getcontent`, get_content1);

输出是:

==========getcontent 367bbed3d629faa8baaad6f9116277006b9b6b8fa80226e04d1bcdbda444b63054dc6250cb1920604988a48b6508dd96b2ec08382921146a71e914aa9e94f2a14fa598ee4aeda064e15020a7791b1e4db513b94f59d7df19d612c66616683075a1d7a7e8f66a2d341c0a234d2c3b4cea

推荐阅读