xExekut3x Posted July 7, 2013 Share Posted July 7, 2013 (edited) I'm using PHP mcrypt to encrypt data, specifically passwords. The Encryption works fine, I'm guessing. I can encrypt and decrypt data just fine, at least, but I'm having issues storing it in MySQL. Let me post some information. Here is my MySQL structure. CREATE DATABASE db CHARACTER SET utf8 COLLATE utf8_unicode_ci ; use db ; CREATE TABLE IF NOT EXISTS `test` ( `id` int(15) unsigned zerofill NOT NULL AUTO_INCREMENT, `password` char(32) COLLATE utf8_unicode_ci NOT NULL, `password_key` char(128) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; Here is my encryption class. class Encryption { private $_cipher, $_mode, $_hash, $_salt; public function __construct($cipher = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_ECB, $hash = 'sha512') { $this->_cipher = $cipher; $this->_mode = $mode; $this->_hash = $hash; $this->_salt = 'K~1[G50*?2GV42I.47517Oe67>WOZw=l'; } public function encrypt($data, $key) { $key = hash($this->_hash, $this->_salt . $key . $this->_salt); $key = substr($key, 0, mcrypt_get_key_size($this->_cipher, $this->_mode)); $iv_size = mcrypt_get_iv_size($this->_cipher, $this->_mode); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $crypt = mcrypt_encrypt($this->_cipher, $key, $data, $this->_mode, $iv); return $crypt; } public function decrypt($data, $key) { $key = hash($this->_hash, $this->_salt . $key . $this->_salt); $key = substr($key, 0, mcrypt_get_key_size($this->_cipher, $this->_mode)); $iv_size = mcrypt_get_iv_size($this->_cipher, $this->_mode); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $decrypt = mcrypt_decrypt($this->_cipher, $key, $data, $this->_mode, $iv); return $decrypt; } } I use this to create the key. $key = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true)); My maximum password length is 30 characters, and any password between 8-30 characters always outputs 32 characters. As you can see, my password field is 32 characters. Everything gets put into the database just fine except the password. Using this method I sometimes get crazy looking passwords such as what's in the image attached (I had to attach it because it wouldn't paste properly) Which is fine, in my opinion, but apparently my MySQL table doesn't like this format. I'm guessing utf8_unicode_ci si the wrong format. Anyone have any idea what the correct format is or any other suggestions? Edited July 7, 2013 by xExekut3x Quote Link to comment https://forums.phpfreaks.com/topic/279929-php-mcryptmysql-database-type-question/ Share on other sites More sharing options...
xExekut3x Posted July 7, 2013 Author Share Posted July 7, 2013 Forgot to add - I'm using XAMPP 1.8.2 which uses MySQL 5.5.32. Quote Link to comment https://forums.phpfreaks.com/topic/279929-php-mcryptmysql-database-type-question/#findComment-1439746 Share on other sites More sharing options...
mac_gyver Posted July 7, 2013 Share Posted July 7, 2013 you would use a BLOB data type for binary data. Quote Link to comment https://forums.phpfreaks.com/topic/279929-php-mcryptmysql-database-type-question/#findComment-1439779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.