php - AES 加密与 Node 和 PHP 不同

标签 php node.js encryption

我有两个脚本,都使用相同的算法来加密具有相同 key 和 IV 的字符串。但结果不同。 openssl_crypt 是否仍然使用与 Node 不同的填充方案,或者我是否遗漏了其他内容?

Node

const crypto = require('crypto');

var passphrase = '29486a7a37664140';
var iv = '76e69938cdf5bb64';
var text = '1234567890123456';

var cipher = crypto.createCipheriv('aes-128-ctr', passphrase, iv)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');

var result = crypted + ':' + iv;

console.log('crypted: (' + crypted.length + ' chars)', crypted);
// crypted: (32 chars) b94107e56900ec8270a847bbf457eaa6

PHP

<?php

$encrypt_method = "AES-128-CTR";
$passphrase = '29486a7a37664140';
$iv = '76e69938cdf5bb64';
$text = '1234567890123456';

$encrypted = openssl_encrypt( $text, $encrypt_method, $passphrase, 0, $iv );

echo 'crypted (' . strlen($encrypted) . ' chars): ' . $encrypted;
// crypted (24 chars): uUEH5WkA7IJwqEe79Ffqpg==
?>

最佳答案

好吧,好吧……愚蠢。编码问题:

更改我的 Node 部分以使用 bas64 有效

var cipher = crypto.createCipheriv('aes-128-ctr', passphrase, iv)
var crypted = cipher.update(text,'utf8','base64')
crypted += cipher.final('base64');

关于php - AES 加密与 Node 和 PHP 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46005433/

相关文章:

javascript - jQuery 提取 token 并将其传递给 php 位置 header

php - MYSQL 返回日期乱序

node.js - 在 Mongo NodeJS 中使用 bulkinsert 处理错误

python - cookies.sqlite 在 Firefox 3.6 及更高版本上加密吗?

c++ - 使用 S.E.A.L. 对产品进行添加。图书馆

SSL/TLS : Why will the server be the only one to be able to decrypt the encrypted number if it's a public key?

php - 需要Sql查询

php - 回显重复分组的数字

node.js - nodejs express css 和 js 未加载

node.js - 在 EC2 上使用 node.js 驱动的服务器,如何减少 TCP 连接时间?