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.










share|improve this question
























  • "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















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.










share|improve this question
























  • "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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















"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












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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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






    share|improve this answer



























      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






      share|improve this answer

























        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






        share|improve this answer














        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 17:15

























        answered Nov 7 at 17:06









        quinz

        75111222




        75111222






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()