Question

I'm having totally confounding encryption/decryption results. I have two different strings that I need to encode and later decode, the encrypted string being stored in a MySQL database in between. The first of these strings comes out just fine. However, the second one always returns from decryption with the value FALSE. I've stripped out all of the non-essential factors, and am directly passing a plaintext value of "test" to both encryption routines. Again, the first one returns correctly (as "test") the second one returns as "false").

I'm banging my head against the wall trying to figure out what I am doing wrong. I'm using the same password and the same salt in both files. How is it possible that one will work but the second won't???

One clue: if I put this code into a single php file and bypass the database, it all works just fine. Not sure what to make of this, but it's interesting at least.

Here is the code. The encryption/decryption routine comes from a user post on the php site for mcrypt. Can anyone see it? It's probably something stupid.

setValues.php

$encrypted_email_pw = encrypt("test", $password);
$encrypted_default_pw = encrypt("test", $password);

$sql = "UPDATE Settings 
        SET email_password='$encrypted_email_pw', 
            default_pw='$encrypted_default_pw'
        WHERE id='$id'";
$result = mysql_query($sql);

getValues.php

$sql = "SELECT * FROM Settings";
$result = mysql_query($sql);
$row = mysql_fetch_array($result); //there is only one row in this table

$decrypted_email_pw = decrypt($row['email_password'], $password);
$decrypted_default_pw = decrypt($row['default_pw'], $password);

echo $decrypted_email_pw . " | " . $decrypted_default_pw;
//output:  test | false
die();

crypto.php

<?php

    function encrypt($decrypted, $password, $salt='6rVDB?zKe6batB+k') { 

        // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
        $key = hash('SHA256', $salt . $password, true);

        // Build $iv and $iv_base64.  
        // We use a block size of 128 bits (AES compliant) and CBC mode.  
        // (Note: ECB mode is inadequate as IV is not used.)
        srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
        if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;

        // Encrypt $decrypted and an MD5 of $decrypted using $key.  
        // MD5 is fine to use here because it's just to verify successful decryption.
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));

        // We're done!
        return $iv_base64 . $encrypted;
    } 

    function decrypt($encrypted, $password, $salt='6rVDB?zKe6batB+k') {

        // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
        $key = hash('SHA256', $salt . $password, true);

        // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
        $iv = base64_decode(substr($encrypted, 0, 22) . '==');

        // Remove $iv from $encrypted.
        $encrypted = substr($encrypted, 22);

        // Decrypt the data.  
        // rtrim won't corrupt the data because the last 32 characters are the md5 hash; 
        // thus any \0 character has to be padding.
        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");

        // Retrieve $hash which is the last 32 characters of $decrypted.
        $hash = substr($decrypted, -32);

        // Remove the last 32 characters from $decrypted.
        $decrypted = substr($decrypted, 0, -32);

        // Integrity check.  If this fails, either the data is corrupted, or the password/salt was incorrect.
        if (md5($decrypted) != $hash) return false;

        return $decrypted;
    }

?>
Was it helpful?

Solution

Have you checked the two columns in the Settings table? Do they have the same data types? And are you sure the encrypt() and decrypt() methods work correctly?

After you get this working, you should consider using a random generated salt for each password and store the salt in the table together with the password.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top