Note that there are some explanatory texts on larger screens.

plurals
  1. POEncrypt/Decrypt using mcrypt
    primarykey
    data
    text
    <p>Trying to achieve encrypting and decryption using following strategy, but ending up with random characters mostly.</p> <pre><code>class Crypt { public static function encrypt($string, $account) { // create a random initialization vector to use with CBC encoding $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $key = pack('H*', $account . $account); $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv); $output = $iv . $output; $output = base64_encode($output); $output = urlencode($output); return $output; } public static function decrypt($token, $account) { $ciphertext_dec = base64_decode($token); // retrieves the IV, iv_size should be created using mcrypt_get_iv_size() $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv_dec = substr($ciphertext_dec, 0, $iv_size); // retrieves the cipher text (everything except the $iv_size in the front) $ciphertext_dec = substr($ciphertext_dec, $iv_size); $key = pack('H*', $account . $account); $token = urldecode($token); $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); $output = rtrim($output, ""); return $output; } } </code></pre> <p>Can't get exact values back, sometimes it decrypts but I see some garbage values, but mostly just random characters.</p> <pre><code>$a = \Crypt::encrypt("MyPassword", "1974246e"); echo \Crypt::decrypt($a, "1974246e"); </code></pre> <hr> <h2>Edits after the discussion</h2> <pre><code>class Crypt { public static function encrypt($data, $passphrase) { $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); //create a random initialization vector to use with CBC encoding $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $key = pack('H*', $passphrase . $passphrase); return base64_encode($iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv)); } public static function decrypt($data, $passphrase) { $data = base64_decode($data); $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); //retrieves the IV, iv_size should be created using mcrypt_get_iv_size() $iv = substr($data, 0, $iv_size); $data = substr($data, $iv_size); //retrieves the cipher text (everything except the $iv_size in the front) $key = pack('H*', $passphrase . $passphrase); return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv), chr(0)); } } </code></pre> <p>Usage:</p> <pre><code>$pass = "MyPassword*&amp;^*&amp;^(*&amp;^("; $token = \Crypt::encrypt($pass, "1974246e8e8a479bb0233495e8a3ed12"); $answer = \Crypt::decrypt($token, "1974246e8e8a479bb0233495e8a3ed12"); echo $answer == $pass ? "yes" : "no"; </code></pre>
    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. 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