繁体   English   中英

AES中的AES加密在Node.js解密。 失败。

[英]AES encrypt in Node.js Decrypt in PHP. Fail.

在node.js中,我使用build in函数来加密数据,如下所示:

var text = "Yes";
var password = "123456";
var encrypt = crypto.createCipher('aes-256-cbc', password);
var encryptOutput1 = encrypt.update(text, 'base64', 'base64');
var encryptOutput2 = encrypt.final('base64');
var encryptedText = encryptOutput1 + encryptOutput2;

输出(加密文本)是:OnNINwXf6U8XmlgKJj48iA ==

然后我用PHP解密它:

$encrypted = 'OnNINwXf6U8XmlgKJj48iA==';
(or $encrypted = base64_decode('OnNINwXf6U8XmlgKJj48iA==')  );
$dtext2 = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CBC);
echo "Decrypted: $dtext2";

我会得到一些有趣的角色,我无法解密它。 我尝试使用/不使用base64_decode或MCRYPT_RIJNDAEL_128 ..都失败了。

然后我检查PHP中的加密方式,它看起来与node.js的输出有很大的不同。

$text = "Yes";
    $key = "123456"; 


    $eText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC);
    echo "Encrypted: $eText \n";
    echo "base64: " . base64_encode($eText) . " \n";

    $dtext1 = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $eText, MCRYPT_MODE_CBC);
    echo "Decrypted: $dtext1 \n\n";

它可以加密和解密。 加密数据为:njCE / fk3pLD1 / JfiQuyVa6w5H + Qb / utBIT3m7LAcetM =

这与node.js的输出有很大不同,请告诉我如何在node.js和php之间加密和解密。 谢谢。 :)


@Mel这里是我在PHP中的东西:

$text = "Yes";

$key = "32BytesLongKey560123456789ABCDEF"; 
$iv =  "sixteenbyteslong";

/* Open the cipher */
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);

/* Encrypt data */
$eText = mcrypt_generic($td, $text);

echo "Encrypted Data: $eText \n";
echo "base64: " . base64_encode($eText) . " \n";

/* Terminate encryption handler */
mcrypt_generic_deinit($td);

/* Initialize encryption module for decryption */
mcrypt_generic_init($td, $key, $iv);

/* Decrypt encrypted string */
$dText = mdecrypt_generic($td, $eText);

/* Terminate decryption handle and close module */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

/* Show string */
echo trim($dText) . "\n";

但是,它仍然无效。

PHP中的加密基数为64: 80022AGM4 / 4qQtiGU5oJDQ == nodejs中的加密基数64为: EoYRm5SCK7EPe847CwkffQ ==

因此,我无法在PHP中解密nodejs。

我想知道是不是因为nodejs不需要$ iv?

七个月晚了,但我也在努力解决这个问题,并找到了解决方案。 显然,PHP使用零字节填充输入,使其大小为块大小的倍数。 例如,使用AES-128,14字节输入“contrabassists”将填充两个零字节,如下所示:

"contrabassists\0\0"

AN * blocksize字节输入保持不变。

但是,标准节点加密功能使用称为PKCS5的不同填充方案。 PKCS5不会添加零,但会增加填充的长度,因此再次使用AES-128,“contrabassists”将变为:

"contrabassists\2\2"

甚至N *块大小的字节输入也会在PKCS5中填充。 否则,解码后无法删除填充。 输入“spectroheliogram”将变为:

"spectroheliogram\16\16\16\16\16\16\16\16\16\16\16\16\16\16\16\16"

要使PHP m_crypt加密与Node解密兼容,您必须自己填充输入:

$pad = $blocksize - (strlen($input) % $blocksize);
$input = $input . str_repeat(chr($pad), $pad);

反过来说,你必须读取解码数据的最后一个字节并自己切断填充。

示例功能:(已添加01-14-2012)

在PHP中,此函数将返回AES-128加密的十六进制编码数据,这些数据可由Node解密:

function nodeEncrypt($data, $key, $iv) {
    $blocksize = 16; // AES-128
    $pad = $blocksize - (strlen($data) % $blocksize);
    $data = $data . str_repeat(chr($pad), $pad);
    return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv));
}

在Node中,以下内容将解密数据:

function nodeDecrypt(data, key, iv) {
    var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    var chunks = []
    chunks.push(decipher.update(data.toString(),'hex','binary'))
    chunks.push(decipher.final('binary'))
    return chunks.join('')
}

我还没有反过来,但是一旦你理解了填充方案,它应该是直截了当的。 我没有对key / iv生成做出任何假设。

我刚刚开始讨论node.js,但我认为你的问题与IV不匹配有关。 请尝试执行以下操作:

var encrypt = crypto.createCipheriv('aes-256-cbc', password, /* password.createHash('md5').toHex()*/);

PS:我不确定如何在node.js中创建MD5哈希,你必须自己弄清楚并相应地更改上面的代码。

在PHP中:

$decrypt = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, md5($key)), "\0");

这应该确保两个实现使用相同的初始化向量。

我还建议您进行以下更改:

  • 密码:md5(original_password)
  • iv = md5(md5(original_password))

这将确保PHP不会抛出任何愚蠢的错误。 请参阅使用PHP加密和解密密码的最佳方法?

我发现了一些可能是PHP和Node.js上的解密/加密不一样的原因。

PHP使用了MCRYPT_RIJNDAEL_256 algorythm。 AES 256基于MCRYPT_RIJNDAEL_256,但不相同。 AES 256这是加密标准,但不是algorythm。

如果你试图通过使用标准的简单函数(例如PHP上的“mcrypt_encrypt”和“mcrypt_decrypt”)来加密某些东西,你就看不到所有的步骤,你肯定不知道为什么你不能解密那个你加密。 对于Node.js可以是相同的,因为需要使用可以逐步加密的功能来防止替换为默认参数。

要加密/解密你需要知道的一些事情(设置):

encryption method (algorythm)
encryption mode (CBF, ECB, CBC...)
key to decryption
key lenght
initialisation vector lenght

并检查双方。 应该是一样的。 还需要找到合适的组合“加密方法”+“加密模式”,这肯定在双方都有效。

我的解决方案是RIJNDAEL_256 + ECB 您应该安装node-rijndael ,因为它肯定使用RIJNDAEL_256。 如果没有 - 我的例子将无效。

这是加密的Node.js示例

在某个文件夹中安装node-rijndael,其中应该是两个.js文件。

r256.js - 它是加密/解密的函数。 我在这里找到

var Rijndael = require('node-rijndael');

/**
 * Pad the string with the character such that the string length is a multiple
 * of the provided length.
 *
 * @param {string} string The input string.
 * @param {string} chr The character to pad with.
 * @param {number} length The base length to pad to.
 * @return {string} The padded string.
 */
function rpad(string, chr, length) {
  var extra = string.length % length;
  if (extra === 0)
    return string;

  var pad_length = length - extra;
  // doesn't need to be optimized because pad_length will never be large
  while (--pad_length >= 0) {
    string += chr;
  }
  return string;
}

/**
 * Remove all characters specified by the chr parameter from the end of the
 * string.
 *
 * @param {string} string The string to trim.
 * @param {string} chr The character to trim from the end of the string.
 * @return {string} The trimmed string.
 */
function rtrim(string, chr) {
  for (var i = string.length - 1; i >= 0; i--)
    if (string[i] !== chr)
      return string.slice(0, i + 1);

  return '';
}

/**
 * Encrypt the given plaintext with the base64 encoded key and initialization
 * vector.
 *
 * Null-pads the input plaintext. This means that if your input plaintext ends
 * with null characters, they will be lost in encryption.
 *
 * @param {string} plaintext The plain text for encryption.
 * @param {string} input_key Base64 encoded encryption key.
 * @param {string} input_iv Base64 encoded initialization vector.
 * @return {string} The base64 encoded cipher text.
 */
function encrypt(plaintext, input_key, input_iv) {
  var rijndael = new Rijndael(input_key, {
    mode: Rijndael.MCRYPT_MODE_ECB,
    encoding: 'base64',
    iv: input_iv
  });
console.log("Rijndael.blockSize", Rijndael.blockSize);
  var padded = rpad(plaintext, '\0', Rijndael.blockSize);

  return rijndael.encrypt(padded, 'binary', 'base64');
}

/**
 * Decrypt the given ciphertext with the base64 encoded key and initialization
 * vector.
 *
 * Reverses any null-padding on the original plaintext.
 *
 * @param {string} ciphertext The base64 encoded ciphered text to decode.
 * @param {string} input_key Base64 encoded encryption key.
 * @param {string} input_iv Base64 encoded initialization vector.
 * @param {string} The decrypted plain text.
 */
function decrypt(ciphertext, input_key, input_iv) {
  var rijndael = new Rijndael(input_key, {
    mode: Rijndael.MCRYPT_MODE_ECB,
    encoding: 'base64',
    iv: input_iv
  });
console.log('lol', rijndael.decrypt(ciphertext, 'base64', 'binary'));
  return rtrim(rijndael.decrypt(ciphertext, 'base64', 'binary'), '\0');
}

exports.decrypt = decrypt;
exports.encrypt = encrypt;

encrypt.js - 它是加密的例子。

var crypto = require('crypto');

var key = new Buffer('theonetruesecretkeytorulethemall', 'utf-8').toString('base64'); //secret key to decrypt

var iv = crypto.randomBytes(32).toString('base64');

console.log({"key":key, "iv":iv});
var rijndael = require('./r256'); 
var plaintext = 'lalala'; //text to encrypt

var ciphertext = rijndael.encrypt(plaintext, key, iv);
console.log({"ciphertext":ciphertext});

这是解密的PHP示例

<?php
echo "<PRE>";
$mcrypt_method = MCRYPT_RIJNDAEL_256;
$mcrypt_mode = MCRYPT_MODE_ECB;
$mcrypt_iv = '123456'; //needed only for encryption, but needed for mcrypt_generic_init, so for decryption doesn't matter what is IV, main reason it is IV can exist.
$mcrypt_key = 'theonetruesecretkeytorulethemall';
$data_to_decrypt = base64_decode('ztOS/MQgJyKJNFk073oyO8KklzNJxfEphu78ok6iRBU='); //node.js returns base64 encoded cipher text


$possible_methods = array_flip(mcrypt_list_algorithms());

if(empty($possible_methods[$mcrypt_method]))
{
    echo "method $mcrypt_method is impossible".PHP_EOL;
    exit();
}

$possible_modes = array_flip(mcrypt_list_modes());
if(empty($possible_modes[$mcrypt_mode]))
{
    echo "mode $mcrypt_mode is impossible".PHP_EOL;
    exit();
}

if(!@mcrypt_get_block_size($mcrypt_method, $mcrypt_mode))
{
    echo "method $mcrypt_method does not support mode $mcrypt_mode".PHP_EOL;
    exit();
}

$mcrypt = mcrypt_module_open($mcrypt_method,'', $mcrypt_mode, '');

$ivsize = mcrypt_enc_get_iv_size($mcrypt);

if($ivsize != strlen($mcrypt_iv))
{
    $mcrypt_iv = str_pad($mcrypt_iv, $ivsize, '#');
}

if($ivsize < strlen($mcrypt_iv))
{
    $mcrypt_iv=substr($mcrypt_iv,0,$ivsize);
}

$keysize = mcrypt_enc_get_key_size($mcrypt);
if($keysize != strlen($mcrypt_key))
{
    $mcrypt_key = str_pad($mcrypt_key, $keysize, '#');
}

if($keysize < strlen($mcrypt_key))
{
    $mcrypt_key=substr($mcrypt_key,0,$keysize);
}


$mcrypt_isblock = (int)mcrypt_enc_is_block_mode($mcrypt);
$mcrypt_blocksize = mcrypt_enc_get_block_size($mcrypt);
$mcrypt_method = mcrypt_enc_get_algorithms_name($mcrypt);
$mcrypt_mode = mcrypt_enc_get_modes_name($mcrypt);

echo "used method=$mcrypt_method  \nmode=$mcrypt_mode \niv=$mcrypt_iv \nkey=$mcrypt_key \nkey with blocksize=$mcrypt_blocksize \nisblock=$mcrypt_isblock".PHP_EOL;

if(mcrypt_generic_init($mcrypt,$mcrypt_key,$mcrypt_iv)< 0)
{
    echo "mcrypt_generic_init failed...".PHP_EOL;
    exit();
}


$result = mdecrypt_generic($mcrypt, $data_to_decrypt);

echo PHP_EOL."decryption result|".$result.'|';

mcrypt_generic_deinit($mcrypt);

PS我不知道为什么,但是Node.js忽略了IV(在我的例子中),所以密码将始终是相同的。 PHP总是使用IV,它应该是严格的长度,所以PHP总是返回不同的密码。 但我反过来尝试它(由PHP加密并由Node.js解密),它的工作原理。

AES是rijndael,固定大小为16字节IV。 细节 在这里 不能用来解密。 更重要的是,我无法使用openssl解密你的字符串:

% openssl aes-256-cbc -d -in dec.txt -a
enter aes-256-cbc decryption password:
bad magic number

或使用PHP:

$encrypted = 'OnNINwXf6U8XmlgKJj48iA==';
$text = 'Yes';
$pw = '123456';
$decrypted = @openssl_decrypt($encrypted, 'aes-256-cbc', $pw);
var_dump($decrypted);
var_dump(@openssl_encrypt($text, 'aes-256-cbc', $pw, FALSE, $pw));
var_dump(@openssl_encrypt($text, 'aes-256-cbc', $pw));

输出:

bool(false)
string(24) "xrYdu2UyJfxhhEHAKWv30g=="
string(24) "findrYaZVpZWVhEgOEVQwQ=="

所以似乎node.js正在使用一些未记录的功能来创建IV,我看不到在node.js中提供IV的方法。

如果您遇到使用MCRYPT_RIJNDAEL_256的第三方库,请知道256指定块大小,而不是密钥大小。 AES使用128位的固定块大小,而openssl不实现更通用的Rijndael算法。 为了避免这种情况,我发布了一个绑定到libmcrypt的模块,就像PHP一样。 这是一个非常有限的用例,但它确保它将与256位块大小的rijndael兼容。

如果你在PHP中使用它

mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plaintext, MCRYPT_MODE_ECB);
mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $ciphertext, MCRYPT_MODE_ECB);

你可以在Node中做同样的事情:

var rijndael = require('node-rijndael');

// straight through (must be buffers)
rijndael.encrypt(plaintext, key);
rijndael.decrypt(ciphertext, key);

// or bound (can take a string for the key and an encoding)
var rijn = rijndael(key);
rijn.encrypt(plaintext); // gotta be a buffer again for implementation simplicity
rijn.decrypt(ciphertext);

GitHub上的node-rijndael

node-rijndael on npm

如果对其他人有帮助,我在另一篇文章中有另一个工作示例。

如果你确保在PHP和Node中使用32个字符长度的“密钥/机密”和16个字符长度的IV,并且在Node中使用base64加密编码和utf8消息编码,那么你不应该在任何差异方面遇到任何问题。填充模式。

此致,伊格纳西奥

Node.js正在使用您的输入密码来获取密钥和iv。 除非PHP完全使用相同的密钥和iv派生魔术,否则很难看出它在PHP中是如何工作的。

为什么不使用createCipheriv。 使用基于密码的密钥派生函数从密码创建密钥。 例如:

http://en.wikipedia.org/wiki/PBKDF2

这种功能在Node.js的更高版本中可用

http://nodejs.org/docs/latest/api/crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_callback

提供良好的静脉注射; 你可以使用crypto.randomBytes创建一个。 如果您控制key和iv参数,那么您将更容易确定是否可以将数据往返到PHP。

您不能只是哈希密码来生成iv。 对于每个加密的消息,iv应该是不同的,否则它是无用的。

另外,您告诉Node.js您的输入字符串“是”是Base64编码的,但我认为它确实是ASCII或UTF-8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM