【问题标题】:AES-256-CBC encryption Node.Js but fails to Decrypt in PHPAES-256-CBC 加密 Node.Js 但无法在 PHP 中解密
【发布时间】:2020-01-14 02:41:02
【问题描述】:

我正在尝试使用 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');

【问题讨论】:

标签: php node.js pdf encryption aes


【解决方案1】:

只要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

【讨论】:

  • 我的 pdf 大小为 1 MB,但加密后“get_content1”大小约为 8 MB。
  • 解密看看能不能找回原PDF
  • @Gaurav 字符串化变量Text 加密之前的大小是多少?您还可以编码为十六进制,与二进制相比,输出的大小加倍。
  • @MaartenBodewes 嗨,实际上我必须在 nodejs 中加密并在 php 中解密,因为我已经进行了更改,而不是十六进制,我已将其更改为 base64,因为默认情况下在 php 中它是这样工作的。我们也使用相同的密钥和 IV 来加密和解密。但我面临的问题是在节点 js 中它加密数据但在 php 中它不解密相同。
猜你喜欢
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 2017-09-28
  • 2021-04-22
  • 2013-08-11
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多