Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Can I encrypt data in node.js and decrypt in PHP? #1067

Closed
murvinlai opened this issue May 18, 2011 · 26 comments
Closed

Can I encrypt data in node.js and decrypt in PHP? #1067

murvinlai opened this issue May 18, 2011 · 26 comments
Labels

Comments

@murvinlai
Copy link

If I have the same key, can I encrypt data in node.js using built in crypt lib, with AES-256-CBC, then decrypt it in PHP?
if so, should I juse RIJNDAEL-256 or RIJNDAEL-128? what I should set for iv in PHP decryption?

@fictorial
Copy link

This isn't an issue.

@murvinlai
Copy link
Author

That's an issue.

@aslilac
Copy link

aslilac commented May 31, 2011

Makes me wonder how many other of the 364 "bugs" are just stupid little questions like this.

@murvinlai
Copy link
Author

@tyler point it out how stupid the question is. show me a working code in both node.js and php. dude.

@goatslacker
Copy link

@murvinlai hit up #node.js on irc.freenode.org for answers to questions.

@aslilac
Copy link

aslilac commented May 31, 2011

@murvinlai You're basically asking if you can eat a hamburger.

@murvinlai
Copy link
Author

@tyler, If I have solved it and made it work, i couldn't made a post here. THINK about it.

@tyler
Copy link

tyler commented May 31, 2011

@murvinlai, everytime you make a reference to @tyler in this thread, it sends me an email. Please reference @tylermwashburn, not @tyler.

Also, for the record, you're misusing GitHub's Issues interface. Issues is for bugs and feature requests. You're asking a question regarding how to use node. Please listen to the advice above and visit #node.js on irc.freenode.org to ask questions.

Thanks.

@murvinlai
Copy link
Author

@tyler Thanks Tyler. :) sorry about the trouble.

On Tue, May 31, 2011 at 2:45 PM, tyler <
reply@reply.github.com>wrote:

@murvinial, everytime you make a reference to @tyler in this thread, it
sends me an email. Please reference @tylermwashburn, not @tyler.

Also, for the record, you're misusing GitHub's Issues interface. Issues is
for bugs and feature requests. You're asking a question regarding how to
use it. Please listen to the advice above and visit #node.js on
irc.freenode.org to ask questions.

Thanks.

Reply to this email directly or view it on GitHub:
#1067 (comment)

@murvinlai
Copy link
Author

and actually, This may be a bug. I raise the issue because the one that you
encrypt in node cannot be decrypted in php. I have other post that related
to this issue. It seems to me that the "iv" is the problem.

On Tue, May 31, 2011 at 3:16 PM, Murvin Lai murvinlai@gmail.com wrote:

@tyler Thanks Tyler. :) sorry about the trouble.

On Tue, May 31, 2011 at 2:45 PM, tyler <
reply@reply.github.com>wrote:

@murvinial, everytime you make a reference to @tyler in this thread, it
sends me an email. Please reference @tylermwashburn, not @tyler.

Also, for the record, you're misusing GitHub's Issues interface. Issues is
for bugs and feature requests. You're asking a question regarding how to
use it. Please listen to the advice above and visit #node.js on
irc.freenode.org to ask questions.

Thanks.

Reply to this email directly or view it on GitHub:
#1067 (comment)

@zmaril
Copy link

zmaril commented Jul 7, 2011

@murvinlai If this is still broken, could you please provide some code as an example of when it doesn't work? It would really help figuring out what is going wrong.

@murvinlai
Copy link
Author

I will do a test tomorrow. :)

On Wed, Jul 6, 2011 at 5:09 PM, zmaril <
reply@reply.github.com>wrote:

@murvinlai If this is still broken, could you please provide some code as
an example of when it doesn't work? It would really help figuring out what
is going wrong.

Reply to this email directly or view it on GitHub:
#1067 (comment)

@zmaril
Copy link

zmaril commented Jul 7, 2011

Excellent, thank you!

On Wed, Jul 6, 2011 at 7:20 PM, murvinlai <
reply@reply.github.com>wrote:

I will do a test tomorrow. :)

On Wed, Jul 6, 2011 at 5:09 PM, zmaril <
reply@reply.github.com>wrote:

@murvinlai If this is still broken, could you please provide some code as
an example of when it doesn't work? It would really help figuring out
what
is going wrong.

Reply to this email directly or view it on GitHub:
#1067 (comment)

Reply to this email directly or view it on GitHub:
#1067 (comment)

@murvinlai
Copy link
Author

Doesn't really work. It may be the way I encrypt it. Here is my code:

Node.js

var text = "Yes";
var encryption_key = '12345678901234561234567890123456';
var iv = '1234567890123456';

var cipher = crypto.createCipheriv('aes-256-cbc', encryption_key, iv);
var ciph = cipher.update(text, 'utf8', 'base64');
ciph += cipher.final('base64');
console.log("IV encryption " + ciph);

and here is the PHP:
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, '1234567890123456');
//$iv = '1234567890123456'; // doesn't work
$key = '12345678901234561234567890123456';
$text = "Yes";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
echo base64_encode($crypttext) . "\n";

the output don't match and I can't decrypt in nodejs->php or php->node.js

@zmaril
Copy link

zmaril commented Jul 8, 2011

This is what I get from the node code:
hcA/eHdeX+qESJ+41S7NrA==

Here is what I get from the PHP code:
7y347+6EU/U33gzZ5n9D940UTQ8n1EwRZEkVhHHQSRw=

They are different, but I don't know why. I don't know enough php/node to really know whether the two code snippets are trying to do the same thing or whether there is something different between them. Wish I could help more.

@bnoordhuis
Copy link
Member

echo -n "Yes" | openssl enc -aes-256-cbc -a -K 12345678901234561234567890123456 -iv 1234567890123456 prints v7xgegPMvNBa5T5qVsxCaQ== so it seems the node example is at least closer to the mark.

@bnoordhuis
Copy link
Member

@murvinlai - is this still an issue for you?

@murvinlai
Copy link
Author

It still failed with my latest test on v0.4.9 last week. I will try again tmr.

Sent from Murvin's iPhone

On 2011-07-24, at 2:02 PM, bnoordhuisreply@reply.github.com wrote:

@murvinlai - is this still an issue for you?

Reply to this email directly or view it on GitHub:
#1067 (comment)

@murvinlai
Copy link
Author

It is still a problem. The encrypted data in PHP & Node.js are different. thus, you cannot encrypt with one language and decrypt in another language.

@johnae
Copy link

johnae commented Jul 25, 2011

Php pads with null bytes, perhaps that's why you see different output.

@sh1mmer
Copy link

sh1mmer commented Oct 26, 2011

I don't see any updates here. I'm closing this as a non-issue until I see a real failing test-case.

@sh1mmer sh1mmer closed this as completed Oct 26, 2011
@bitraft
Copy link

bitraft commented Apr 28, 2013

I have the same problem with nodejs 10.5, php can decode nodejs eccode data, but nodejs can not decode php encode data.

@indutny
Copy link
Member

indutny commented Apr 28, 2013

Please post example.

@skeggse
Copy link

skeggse commented Sep 25, 2013

Here's one basic issue. Openssl implements a limited form of Rijndael as specified by AES, with a block size of 128 bits. PHP binds to libmcrypt, which implements a more generic form of Rijndael. The constant MCRYPT_RIJNDAEL_256 specifies Rijndael encryption with a block size of 256 bits, while AES-256-CBC specifies Rijndael encryption with a key size of 256 bits (and implicitly a block size of 128 bits). Note that one specifies AES and on specifies Rijndael in the name.

As a solution for those wishing to bridge the gap between PHP and Node.js (or some other environment which uses libmcrypt), I created node-rijndael which binds to libmcrypt and provides limited functionality for encrypting and decryption rijndael with a block size of 256 bits.

If you're using this in PHP

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

You can do the same in 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);

@inieto
Copy link

inieto commented Jan 27, 2015

Hi @murvinlai !
I know that some time has passed, but I was struggling with the same problem this week and found your issue.
If it helps, please check my solution here: http://stackoverflow.com/a/28181444/3739618
Regards,
Ignacio

@murvinlai
Copy link
Author

Hi @inieto. Thanks. I will take a look. I remembered that at last I used some other ways to achieve that encrypt & decrypt across node & PHP

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests