Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your encryption looks very bogus:</p> <pre><code> $result = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, (SALTKEY . str_repeat(chr(0x00), 16)), base64_encode($text), 'ecb'); $pad_char = ord(substr($result, -1)); return substr($result, 0, strlen($result) - $pad_char); </code></pre> <p>You encode the text with base64, then encrypt it, and then and then try to <strong>remove</strong> padding?</p> <p>Instead, you have to</p> <ul> <li><strong>add</strong> padding (if the encryption function doesn't already do this),</li> <li>encrypt</li> <li>then base-64-encode the result (if you want to have it somehow readable by humans or transmit over a non-binary-safe channel).</li> </ul> <p>This could look like this:</p> <pre><code>$padded = pad($text); $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, (SALTKEY . str_repeat(chr(0x00), 16)), $padded, 'ecb'); $result = base64_encode($encrypted); </code></pre> <p>(Have a look at the user-contributed notes at the <a href="http://php.net/mcrypt-encrypt" rel="nofollow">mcrypt_encrypt documentation</a> for an example on how to create the padding.)</p> <p>Of course, there are some more things to remark:</p> <ul> <li><p>Don't ever use ECB-mode, if you don't know anything about cryptography. It is an unsafe mode of operation. Use CBC-mode (with a random initialization vector, which is sent with the data).</p></li> <li><p>You create your key by padding the SALTKEY with zeros. This makes your key in effect weaker than necessary. (Having a key hard-coded in the code is a bad idea anyways.) Supply a full 128-bit key, or derive one from a password using salt and a key derivation function (like PBKDF-2) with a high iteration count.</p></li> <li><p>Your decryption function should also check that the padding is valid (i.e. consists of identical bytes), not simply removing it.</p></li> <li><p>You should also use a message authentication code (MAC) with your message, to avoid some chosen-ciphertext attacks which allow decrypting a message.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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