Jump to content

Recommended Posts

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?

post-152235-0-20160800-1373166056_thumb.png

Edited by xExekut3x
Link to comment
https://forums.phpfreaks.com/topic/279929-php-mcryptmysql-database-type-question/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.