Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code igniter MySQL AES with active records?

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

like image 375
Devan Avatar asked Jan 21 '23 08:01

Devan


2 Answers

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 special chars it won't kill the query

like image 160
PottyBert Avatar answered Jan 29 '23 22:01

PottyBert


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.

like image 31
Mitchell McKenna Avatar answered Jan 29 '23 21:01

Mitchell McKenna