Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've done cross-language encryption before using PHP and Coldfusion. There were some tricks because of slight differences in their implementation, but it should be even easier in Javascript since Javascript has no native encryption functions. You will have to provide your own, so you can provide exactly what you need.</p> <p>To encrypt in PHP you will need the <code>mcrypt</code> libraries installed. I chose Blowfish as my algorithm, and used the ECB mode since it is easier to work with--you don't need to worry about initialization vectors, but it is theoretically less secure. However, given that you are only encrypting a short user ID, this should be sufficient.</p> <p>Here is an example of using mcrypt to encrypt a string:</p> <pre><code>$data = $user_id; $data = base64_encode($data); // Add PKCS5 padding to data string for compatibility // This may not be necessary, depending on your Javascript implementation $pad = 8 - (strlen($data) % 8); $data = $data . str_repeat(chr($pad), $pad); $algorithm = MCRYPT_BLOWFISH; $mode = MCRYPT_MODE_ECB; $key = "SECRET"; $ciphertext = mcrypt_encrypt($algorithm, $key, $data, $mode, null); $base64 = base64_encode($ciphertext); </code></pre> <p>Note that <code>$ciphertext</code> will be in binary, so you will need to encode it in base 64 to make it easier to transfer between languages.</p> <p>Once you transfer the encrypted string to your Node JS app and <a href="https://stackoverflow.com/questions/14573001/nodejs-how-to-decode-base64-encoded-string-back-to-binary">convert it back into binary from base64</a>, you should then be able to decrypt it with a blowfish implementation for Javascript. Here is one example that is easy to use: <a href="http://dren.ch/js_blowfish/" rel="nofollow noreferrer">http://dren.ch/js_blowfish/</a></p> <p>You can safely ignore the warning on that page, since it refers to client-side decryption of large strings (which is silly). If you're doing it on the server you can safely keep your secret key secret.</p> <p>Note that in the above example the string was base64-encoded BEFORE it was encrypted, so you will have to base64-decode it again after decrypting.</p> <p>Also be wary of the minimum and maximum key sizes for Blowfish ECB to save some headache. You can check this using the PHP <a href="http://www.php.net/manual/en/function.mcrypt-enc-get-supported-key-sizes.php" rel="nofollow noreferrer">mcrypt_enc_get_supported_key_sizes</a> function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload