AES Encryption AES Encryption Porting from PHP to Yii
#41
Posted 06 December 2011 - 01:41 PM
Warning: openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended in Command line code on line 1
#42
Posted 06 December 2011 - 01:46 PM
$method = 'aes-128-ecb';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
openssl_encrypt('abc', $method, 'def', true, $iv);
#43
Posted 06 December 2011 - 01:56 PM
#44
Posted 06 December 2011 - 01:58 PM
HiB+O欵F??*
#45
Posted 06 December 2011 - 02:43 PM
I think there is something missing on the encryption and decryption and it is the utf8_encode/decode. Please, use the following behavior and follow the instructions Da:Sourcerer told you to be AES (Rijndael-128) -.
Here is my behavior:
class CryptBehavior extends CActiveRecordBehavior {
public $attributes = array();
/**
* Encrypts the value of specified attributes before saving to database
* @param CEvent $event
* @return parent::beforeSave
*/
public function beforeSave($event)
{
foreach ($this->getOwner()->getAttributes() as $key => $value)
{
if (in_array($key, $this->attributes) && !empty($value))
// saving utf8_encoded result... we could also use other type of bin conversion
$this->getOwner()->{$key} = utf8_encode(Yii::app()->securityManager->encrypt($value));
}
return parent::beforeSave($event);
}
/**
* Decripts the values of specified attributes after finding from database
* @param CEvent $event
* @return parent::afterFind
*/
public function afterFind($event)
{
foreach ($this->getOwner()->getAttributes() as $key => $value)
{
if (in_array($key, $this->attributes) && !empty($value))
// database value is utf8_decode before decryption
$this->getOwner()->{$key} = Yii::app()->securityManager->decrypt(utf8_decode($value));
}
return parent::afterFind($event);
}
}
See if it works for you...
PS: Another good resource to look at
Cheers
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#46
Posted 06 December 2011 - 02:52 PM
christomurr, on 06 December 2011 - 01:58 PM, said:
HiB+O欵F??*
That's the raw binary output. Nothing to worry about. I wrapped my output in HEX()/bin2hex() for readability.
#47
Posted 06 December 2011 - 03:15 PM
I plugged in what you provided and now get this error:
Property "CryptBehavior.key" is not defined.
#48
Posted 06 December 2011 - 03:36 PM
#49
Posted 06 December 2011 - 03:38 PM
christomurr, on 06 December 2011 - 03:15 PM, said:
I plugged in what you provided and now get this error:
Property "CryptBehavior.key" is not defined.
?
$key is part of the loop... how did you use the behavior? You know that they have to be set on your model right -is the easiest way? Here is an example:
// on the model
public function behaviors()
{
return array(
'crypt'=>array(
// this assumes that the behavior is in the folder: protected/behaviors/
'class'=>'application.behaviors.CryptBehavior',
// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
'attributes'=>array('encryptedfieldname'),
)
);
}
Remove the comments inside the behavior (they shouldn't affect but I included them to explain you what they do.
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#50
Posted 06 December 2011 - 03:58 PM
public function behaviors()
{
return array(
'crypt'=>array(
'class'=>'application.behaviors.CryptBehavior',
'attributes'=>array('clientSocialSecurity'),
)
);
}
And then this is my CryptBehavior.php (in protected/behaviors):
class CryptBehavior extends CActiveRecordBehavior {
public $attributes = array();
/**
* Encrypts the value of specified attributes before saving to database
* @param CEvent $event
* @return parent::beforeSave
*/
public function beforeSave($event)
{
foreach ($this->getOwner()->getAttributes() as $key => $value)
{
if (in_array($key, $this->attributes) && !empty($value))
// saving utf8_encoded result... we could also use other type of bin conversion
$this->getOwner()->{$key} = utf8_encode(Yii::app()->securityManager->encrypt($value));
}
return parent::beforeSave($event);
}
/**
* Decripts the values of specified attributes after finding from database
* @param CEvent $event
* @return parent::afterFind
*/
public function afterFind($event)
{
foreach ($this->getOwner()->getAttributes() as $key => $value)
{
if (in_array($key, $this->attributes) && !empty($value))
// database value is utf8_decode before decryption
$this->getOwner()->{$key} = Yii::app()->securityManager->decrypt(utf8_decode($value));
}
return parent::afterFind($event);
}
}
In my CSecurityManager I have this:
public $cryptAlgorithm='rijndael-128-ecb';
I currently get this error:
mcrypt_module_open() [<a href='function.mcrypt-module-open'>function.mcrypt-module-open</a>]: Could not open encryption module
#51
Posted 06 December 2011 - 04:07 PM
christomurr, on 06 December 2011 - 03:58 PM, said:
In my CSecurityManager I have this:
public $cryptAlgorithm='rijndael-128-ecb';
That is wrong, please follow Da:Sourcerer configuration settings on YOUR MAIN.PHP config file, located on protected/config subfolder. In the COMPONENTS section of the file. You do not change the CSecurityManager like that directly and, the $cryptAlgorithm is an array not a direct variable... Please, follow instructions and links of Da:sourcerer.
The rest is fine
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#52
Posted 06 December 2011 - 04:08 PM
array( 'rijndael-128', '', 'ecb', '', )
Just rijndael-128-ecb won't work because mcrypt's module isn't called that way
#53
Posted 06 December 2011 - 04:11 PM
Please, wrap your code with "[ code ][ / code ] " tags
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#54
Posted 06 December 2011 - 05:56 PM
mdecrypt_generic() [<a href='function.mdecrypt-generic'>function.mdecrypt-generic</a>]: An empty string was passed
I'm also wondering where my secret key gets included. I have it in main.php as :
'params'=>array( // this is used in contact page 'adminEmail'=>'webmaster@example.com', 'secretKey'=>'mySecretKey', ),
I also have this in the main.php components:
'securityManager'=>array(
'cryptAlgorithm'=>array(
'rijndael-128',
'',
'ecb',
''
),
Thanks/
#55
Posted 06 December 2011 - 06:19 PM
#56
Posted 06 December 2011 - 06:30 PM
#57
Posted 06 December 2011 - 07:04 PM
http://www.yiiframew...ptionKey-detail
'securityManager'=>array(
'cryptAlgorithm'=>array(
'rijndael-128',
'',
'ecb',
''
),
'encryptionKey'=>'mysecretkeytoencryptdecrypt'
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#58
Posted 06 December 2011 - 07:41 PM
What you just posted is what I have ...
I'm still seeing this:
mdecrypt_generic() [<a href='function.mdecrypt-generic'>function.mdecrypt-generic</a>]: An empty string was passed
And these below:
$decrypted=mdecrypt_generic($module,substr($data,$ivSize));
$this->getOwner()->{$key} = Yii::app()->securityManager->decrypt(utf8_decode($value));
Thanks again for your patience.
#59
Posted 07 December 2011 - 03:35 AM
$value supposed to have the attribute that has something... I do not really understand why you have that error as the behavior DOES CHECK against empty values...
if (in_array($key, $this->attributes) && !empty($value))
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#60
Posted 07 December 2011 - 05:35 AM
I'm running out of ideas. Is it the value it is not getting or the encrypt key?

Help













