views:

16

answers:

2

Hey,

How would i use code igniters active records to insert/update/select data from a database using mysql's built in aes encrypt/decrypt functions?

I know i could just use the normal sql query, but i'd like to use active records ideally.

Thanks

+1  A: 

You can still use AES_Encrypt if you turn off escaping for that particular clause by passing FALSE as the last parameter:

$pass = $this->db->escape($data['password']);
$this->db->set('password', "AES_ENCRYPT('{$pass}','my_key')", FALSE);

Also point you to the CI built-in Encryption Class, and an article on considering 1-way encryption.

Mitchell McKenna
+2  A: 

If you use the code provided previously:

$this->db->set('password',"AES_ENCRYPT('{$data['password']}','my_key')",FALSE);

you should still escape the password before passing it into db->set

use:

$pass = $this->db->escape($data['password']);

That way if the password contains apostrophes it won't kill the query

Kris
Nice addition Kris, worth an upvote in my books, welcome to SO ;)
Mitchell McKenna