Using openssl_encrypt in php to encrypt data and then looking to decipher the data in nodejs - the key sizes...
up vote
2
down vote
favorite
I have my php code as follows:
openssl_encrypt($string, 'AES-256-CBC', $key, 0, $iv);
and my nodejs code:
crypto.createDecipheriv('aes-256-cbc', 'key', 'iv')
The key in my php code is 64 characters long when printing to console. However, when I use the same key to decipher my code in nodejs it is only happy when I use 32 characters for my key. I've tried using just the first 32 characters of the key I'm using in my php encryption code, but it's not working. Could anyone explain where I'm going wrong here? Thanks.
php node.js encryption cryptography
add a comment |
up vote
2
down vote
favorite
I have my php code as follows:
openssl_encrypt($string, 'AES-256-CBC', $key, 0, $iv);
and my nodejs code:
crypto.createDecipheriv('aes-256-cbc', 'key', 'iv')
The key in my php code is 64 characters long when printing to console. However, when I use the same key to decipher my code in nodejs it is only happy when I use 32 characters for my key. I've tried using just the first 32 characters of the key I'm using in my php encryption code, but it's not working. Could anyone explain where I'm going wrong here? Thanks.
php node.js encryption cryptography
"The key in my php code is 64 characters long when printing to console." is that because you ran it throughbase64_encode()
?
– miken32
Nov 7 at 18:58
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have my php code as follows:
openssl_encrypt($string, 'AES-256-CBC', $key, 0, $iv);
and my nodejs code:
crypto.createDecipheriv('aes-256-cbc', 'key', 'iv')
The key in my php code is 64 characters long when printing to console. However, when I use the same key to decipher my code in nodejs it is only happy when I use 32 characters for my key. I've tried using just the first 32 characters of the key I'm using in my php encryption code, but it's not working. Could anyone explain where I'm going wrong here? Thanks.
php node.js encryption cryptography
I have my php code as follows:
openssl_encrypt($string, 'AES-256-CBC', $key, 0, $iv);
and my nodejs code:
crypto.createDecipheriv('aes-256-cbc', 'key', 'iv')
The key in my php code is 64 characters long when printing to console. However, when I use the same key to decipher my code in nodejs it is only happy when I use 32 characters for my key. I've tried using just the first 32 characters of the key I'm using in my php encryption code, but it's not working. Could anyone explain where I'm going wrong here? Thanks.
php node.js encryption cryptography
php node.js encryption cryptography
edited Nov 9 at 14:49
asked Nov 7 at 16:49
Chris Laidler
112
112
"The key in my php code is 64 characters long when printing to console." is that because you ran it throughbase64_encode()
?
– miken32
Nov 7 at 18:58
add a comment |
"The key in my php code is 64 characters long when printing to console." is that because you ran it throughbase64_encode()
?
– miken32
Nov 7 at 18:58
"The key in my php code is 64 characters long when printing to console." is that because you ran it through
base64_encode()
?– miken32
Nov 7 at 18:58
"The key in my php code is 64 characters long when printing to console." is that because you ran it through
base64_encode()
?– miken32
Nov 7 at 18:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
32 byte key length should be correct for AES256. Create a dummy key and print it out on both ends to investigate. It is very likely that your issue is the format / encoding of your key. For example, the 64 characters could be explained if you are handling the key in hexadecimals. That would require two characters to present one byte.
The documentation of that openssl_encrypt
doesn't seem to be very detailed when it comes to the key
parameter, but one of the highly voted user contribution comment suggests that the key indeed needs to be in hexadecimal format: "IV and Key parameteres passed to openssl command line must be in hex representation of string." http://php.net/manual/en/function.openssl-encrypt.php
The nodeJS side seems to be more flexible about the format, just make sure your data type and encoding match to each other. https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options
EDIT:
One thing you can try is to convert that 64 character (most likely still the hex format) to a Buffer in the Node side before giving it to the createDecipheriv
function. https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
32 byte key length should be correct for AES256. Create a dummy key and print it out on both ends to investigate. It is very likely that your issue is the format / encoding of your key. For example, the 64 characters could be explained if you are handling the key in hexadecimals. That would require two characters to present one byte.
The documentation of that openssl_encrypt
doesn't seem to be very detailed when it comes to the key
parameter, but one of the highly voted user contribution comment suggests that the key indeed needs to be in hexadecimal format: "IV and Key parameteres passed to openssl command line must be in hex representation of string." http://php.net/manual/en/function.openssl-encrypt.php
The nodeJS side seems to be more flexible about the format, just make sure your data type and encoding match to each other. https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options
EDIT:
One thing you can try is to convert that 64 character (most likely still the hex format) to a Buffer in the Node side before giving it to the createDecipheriv
function. https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array
add a comment |
up vote
1
down vote
32 byte key length should be correct for AES256. Create a dummy key and print it out on both ends to investigate. It is very likely that your issue is the format / encoding of your key. For example, the 64 characters could be explained if you are handling the key in hexadecimals. That would require two characters to present one byte.
The documentation of that openssl_encrypt
doesn't seem to be very detailed when it comes to the key
parameter, but one of the highly voted user contribution comment suggests that the key indeed needs to be in hexadecimal format: "IV and Key parameteres passed to openssl command line must be in hex representation of string." http://php.net/manual/en/function.openssl-encrypt.php
The nodeJS side seems to be more flexible about the format, just make sure your data type and encoding match to each other. https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options
EDIT:
One thing you can try is to convert that 64 character (most likely still the hex format) to a Buffer in the Node side before giving it to the createDecipheriv
function. https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array
add a comment |
up vote
1
down vote
up vote
1
down vote
32 byte key length should be correct for AES256. Create a dummy key and print it out on both ends to investigate. It is very likely that your issue is the format / encoding of your key. For example, the 64 characters could be explained if you are handling the key in hexadecimals. That would require two characters to present one byte.
The documentation of that openssl_encrypt
doesn't seem to be very detailed when it comes to the key
parameter, but one of the highly voted user contribution comment suggests that the key indeed needs to be in hexadecimal format: "IV and Key parameteres passed to openssl command line must be in hex representation of string." http://php.net/manual/en/function.openssl-encrypt.php
The nodeJS side seems to be more flexible about the format, just make sure your data type and encoding match to each other. https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options
EDIT:
One thing you can try is to convert that 64 character (most likely still the hex format) to a Buffer in the Node side before giving it to the createDecipheriv
function. https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array
32 byte key length should be correct for AES256. Create a dummy key and print it out on both ends to investigate. It is very likely that your issue is the format / encoding of your key. For example, the 64 characters could be explained if you are handling the key in hexadecimals. That would require two characters to present one byte.
The documentation of that openssl_encrypt
doesn't seem to be very detailed when it comes to the key
parameter, but one of the highly voted user contribution comment suggests that the key indeed needs to be in hexadecimal format: "IV and Key parameteres passed to openssl command line must be in hex representation of string." http://php.net/manual/en/function.openssl-encrypt.php
The nodeJS side seems to be more flexible about the format, just make sure your data type and encoding match to each other. https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options
EDIT:
One thing you can try is to convert that 64 character (most likely still the hex format) to a Buffer in the Node side before giving it to the createDecipheriv
function. https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array
edited Nov 7 at 17:15
answered Nov 7 at 17:06
quinz
75111222
75111222
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194106%2fusing-openssl-encrypt-in-php-to-encrypt-data-and-then-looking-to-decipher-the-da%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"The key in my php code is 64 characters long when printing to console." is that because you ran it through
base64_encode()
?– miken32
Nov 7 at 18:58