【问题标题】:AES encryption different from node and PHPAES 加密不同于 node 和 PHP
【发布时间】:2018-02-10 19:48:36
【问题描述】:

我有两个脚本,都使用相同的算法来加密具有相同密钥和 IV 的字符串。但结果不一样。 openssl_encrypt 是否仍在使用与 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==
?>

【问题讨论】:

    标签: php node.js encryption


    【解决方案1】:

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

    更改我的节点部分以使用 bas64 作品

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

    【讨论】:

    • 感谢您花时间发布解决方案
    猜你喜欢
    • 2022-01-18
    • 2022-11-16
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多