0

Using xcrud data manipulation and more specifically from where it says "Database instanse In all external files you can use xcrud database instanse:", I have my table displaying using $xcrud = Xcrud::get_instance()->table('users');.

Now the issue is that all my data is encrypted using AES_ENCRYPT with a $salt. I need to do an AES_DECRYPT but unsure where or how I can go about it with xcrud methods.

Can I even go about using the MySQL AES_DECRYPT function purely in PHP instead and just use a callback in the functions.php?

Edit: I've tried this method in PHP however it is displaying weird characters (���,��ŝA����,�At�nz��M�F)...

function mysql_aes_key($key)
{
    $new_key = str_repeat(chr(0), 16);
    for($i=0,$len=strlen($key);$i<$len;$i++)
    {
        $new_key[$i%16] = $new_key[$i%16] ^ $key[$i];
    }
    return $new_key;
}

function decrypt_info($value)
{
    $key = mysql_aes_key('mysalt');
    $value = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM));
    return rtrim($value, "0..10");
}

function decrypt_name($value)
{
    $decrypted = decrypt_info($value);
    return $decrypted;
}
esqew
  • 42,425
  • 27
  • 92
  • 132
DT.DTDG
  • 765
  • 1
  • 13
  • 31
  • FWIW, salting is something you do with one-way hashing functions. There is no benefit to using a salt with a block encryption functions like AES. – Bill Karwin Oct 02 '14 at 00:05
  • Thanks @BillKarwin appreciate the "FWIW" :) any idea though what the issue is here? – DT.DTDG Oct 02 '14 at 00:11
  • What kind of padding are you applying to the key? MySQL pads the key using PKCS7. – Ja͢ck Oct 02 '14 at 00:20
  • Thanks @Jack I'm actually using no padding. When I do an `INSERT` it's literally like this: `AES_ENCRYPT('".$email."','".$salt."')` – DT.DTDG Oct 02 '14 at 00:23
  • I don't have any insight into why your strings are not decrypting correctly. But if it were me, I would decrypt using the same interface I used to encrypt. So if I used MySQL's `AES_ENCRYPT()`, then I would stick with MySQL's `AES_DECRYPT()`. The reason is that I don't know if the implementation of the encryption algorithm (and its options like padding) is necessarily the same between MySQL and PHP. Or even if it is true today, perhaps some later version of either MySQL or PHP will change its implementation. – Bill Karwin Oct 02 '14 at 00:25
  • I mean the padding you apply in `mysql_aes_key()` seems to be wrong; the key should be padded with `CHR(16 - strlen($salt) % 16)`; see also [this question](http://stackoverflow.com/questions/17039628/aes-encryption-in-mysql-and-php). – Ja͢ck Oct 02 '14 at 00:26
  • Thanks @BillKarwin and @Jack - @Jack could you please provide the updated `mysql_aes_key` function as an answer? Thank you! – DT.DTDG Oct 02 '14 at 00:29

0 Answers0