Node.JS和Laravel转换代码之间的加密 [英] Encryption between Node.JS and Laravel Converting Code

查看:181
本文介绍了Node.JS和Laravel转换代码之间的加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Node.js上有以下代码

so I have the following code on Node.js

var crypto = require('crypto');

function encrypt (key = "9055935C641A3CD243337FD149C793DF", data) {
    var key  = (key instanceof Buffer) ? key : new Buffer(key, 'hex');
    var iv = crypto.randomBytes(16);
    var cipher = crypto.createCipheriv( "aes-128-cbc", key, iv); 
    var result = Buffer.concat([iv, cipher.update(data), cipher.final()]);
    return new Buffer( result ).toString('base64');
};

在Laravel我有:

And in Laravel I have:

<?php
function encrypt($key = "9055935C641A3CD243337FD149C793DF", $data) {
    $encrypter = new Encrypter($key, 'AES-128-CBC');
    $dataEncrypted = $encrypter->encryptString($data);
    return $dataEncrypted;
}

问题是我在Laravel州收到错误:唯一支持的密码是具有正确密钥长度的AES-128-CBC和AES-256-CBC。

The problem is that I get an error on Laravel states: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

如何使用我在Laravel中使用Node.js的关键?

How can I use the key that I use on Node.js in Laravel?

推荐答案

我看到PHP代码有两个问题:

I see two issues with the PHP code:


  1. 您正在使用 $ this-> key 而不是 $密钥

  2. Encrypter()似乎在期待一个二进制字符串(不是一个十六进制字符串) ,因此您需要首先对hex进行解码(例如使用 hex2bin() )。

  1. You're using $this->key instead of $key
  2. Encrypter() appears to be expecting a binary string (not a string of hex characters), so you need to decode the hex first (e.g. using hex2bin()).

这篇关于Node.JS和Laravel转换代码之间的加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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