Search code examples
phpcakephpencryptioncakephp-2.3rijndael

CakePHP rijndael cipher puts an empty string in the database


I have a string I want to save into my DB in an encrypted format. I am using the security utility provided by cake so this is the code I use to encrypt my sensitive data:

// get my encryption key
$encrypt_key = Configure::read('Secret.encrypt_key');

// encrpyt this string to be stored in the database
$this->request->data['User']['message'] = Security::rijndael($this->request->data['User']['message'], $encrypt_key, 'encrypt');

// save this user data
$user_saved = $this->User->save( $this->request->data[ 'User' ] );

This looks like ever guide I have seen for how to do this, but in my case all of the other fields will save and I will get an empty field for message

My question is why is this blank database save happening and how do I fix it. Thank you.


Solution

  • The problem ended up being that the rjindeal function returns a raw binary string 010100100010100101111101010010101 that my database cannot handle. By simply converting the result to hex code via bin2hex($encrypted_message) the data is transformed into a form that my database can handle.