2

In PHP 5.6 there were plenty of solutions that worked perfectly that were based on http://php.net/manual/en/function.mcrypt-decrypt.php

For example

public function encrypt($data)
{
    //don't use default php padding which is '\0'
    $pad = $this->blocksize - (strlen($data) % $this->blocksize);
    $data = $data . str_repeat(chr($pad), $pad);
    return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
        $this->encryptKey,
        $data, MCRYPT_MODE_CBC, $this->iv));
}

But PHP7 has a WARNING that discourages using this function.

"This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged."

Any ideas for safe encryption using keywords on both ends; PHP + Node.js?

3
  • This explains why its deprecated and makes a suggestion on what to use instead php.net/manual/en/migration71.deprecated.php Commented Jan 22, 2017 at 15:49
  • Try using openssl_encrypt php.net/manual/en/function.openssl-encrypt.php Commented Jan 22, 2017 at 15:52
  • As suggested, use openssl, not only because LibMcrypt is deprecated, but also because NodeJS Crypto module relays on openssl, so you can expect good compatibility of algorithms. Commented Jan 22, 2017 at 16:51

1 Answer 1

3

LibMcrypt was abandoned in 2007. More information https://wiki.php.net/rfc/mcrypt-viking-funeral

You have to use openssl encrypt http://php.net/manual/en/function.openssl-encrypt.php

PHP

<?php
$textToEncrypt = "Secret Text to Encrypt";
$encryptionMethod = 'aes-256-cbc';
$secretHash = "315a5504d921f8327f73a356d2bbcbf1"; // <---- you have to use some persistent key.

$iv_size = openssl_cipher_iv_length($encryptionMethod);
$iv = openssl_random_pseudo_bytes($iv_size);

//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash, 0, $iv);

//Concatenate iv with data
$encryptedMessageWithIv = bin2hex($iv) . $encryptedMessage;

//To Decrypt
$iv_size = openssl_cipher_iv_length($encryptionMethod);
$iv = hex2bin(substr($encryptedMessageWithIv, 0, $iv_size * 2));

$decryptedMessage = openssl_decrypt(substr($encryptedMessageWithIv, $iv_size * 2), $encryptionMethod, $secretHash, 0, $iv);

echo "Encrypted: $encryptedMessageWithIv <br>Decrypted: $decryptedMessage";

Try it here https://3v4l.org/r9pYv

Node.JS(i really not a node.js programmer, there can be more efficient way)

var data = "ad699a2537ec2a7f699acbf97ca0080eh3z5EgvnTAvlc76YeR6HdWPmkDDt+pHiG//qo7xnqyQ=";
var key = "315a5504d921f8327f73a356d2bbcbf1";
var iv = new Buffer(data.substring(0,32), 'hex');
var dec = crypto.createDecipheriv('aes-256-cbc',key,iv);
var decrypted = Buffer.concat([dec.update(new Buffer(data.substring(32),'base64')), dec.final()]);
console.log('DECRYPTED TEXT: '+decrypted.toString());

Try here: https://repl.it/FQyo/2

Sign up to request clarification or add additional context in comments.

7 Comments

This gives 3 different outputs for 3 recent PHP7 versions, that's weird and not common when it comes to decrypting on node js
Of course - it will be diffrent outputs even you are using one version of PHP on every new run(because of initialization vector - iv) - this is the key aim of strong cryptography. What you need is only to set some static key for $secretHash variable in both - nodejs and php.
how to maintain compatability with node js then if the output keeps changing?
Ok. challenge accepted. I updated answer with node.js part. I set persistent key in php (note php link updated too) You can replace data with ANY one of the PHP link output and see THE MAGIC of cryptography )
works like charm, let me hear other opinions before i accept your answer. thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.