AES Encryption AES Encryption Porting from PHP to Yii
#81
Posted 08 December 2011 - 05:41 PM
#82
Posted 08 December 2011 - 06:02 PM
christomurr, on 08 December 2011 - 05:41 PM, said:
Just use standard array syntax, it's just an array of attribute (column) names, so clientSocialSecurity, I'm guessing, is the field you're currently encrypting/decrypting in the database. So replace the 'Some' and 'More' etc. parts of this line with those attribute (column) names:
array('clientSocialSecurity', 'Some', 'More', 'Attribute', 'Names'),
#83
Posted 08 December 2011 - 06:09 PM
'attributes'=>array('clientSocialSecurity','insurancePolicyNumber','insuranceSubscriberDOB','insuranceSubscriberSSN'),
The behavior is parsing a key and a value so I think I need to pass things differently:
foreach ($this->getOwner()->getAttributes() as $key => $value)
#84
Posted 08 December 2011 - 06:12 PM
I'll give it read tomorrow morning if antonio hasn't already answered. I've got a bit of a vested interest in this as I'm looking at something similar in the future
#85
Posted 09 December 2011 - 06:58 AM
Is the field in the database already encrypted or in plain text? A plain text in the DB will be decrypted to gobbledegook on the way out and then encrypted back to "plain text" on the way in, therefore you won't notice a difference. Make sure it's the encrypted value that's in the database when you begin.
Other than this, sorry, I've no idea that seems like exactly how it should work. You could try posting your whole model class as you have it here and I'll take a look over it make sure theres nothing that should stop you?
#86
Posted 09 December 2011 - 08:47 AM
Thanks again for looking at this with me.
Yes, the values in the database already are encrypted. I can run a query directly against the database and see that they can be decrypted properly using a simple AES_DECRYPT in MySQL.
In the behavior function call, I can call the clientSocialSecurity and it displays properly decrypted in the view page. But if I add another field (insurancePolicyNumber, for example), the clientSocialSecurity appears fine, but the insurancePolicyNumber is empty. The same is true if I add multiple fields (which I eventually need to do).
If I print the key and value from within the CryptBehavior class, I see that it is indeed getting the right fields and encrypted values:
clientSocialSecurity | C� ��zH{��\-$m�
insurancePolicyNumber | :7³”»ÆºcÕÅáN½óÔ
So, it appears something is happening perhaps with how it is returned?
Here is how the afterFind works:
public function afterFind($event)
{
foreach ($this->getOwner()->getAttributes() as $key => $value)
{
if (in_array($key, $this->attributes) && !empty($value))
{
echo "$key | $value<br />";
if ($this->useAESMySql)
$this->getOwner()->{$key} = $this->mysqlAESDecrypt($value, Yii::app()->securityManager->getEncryptionKey());
else
$this->getOwner()->{$key} = Yii::app()->securityManager->decrypt(utf8_decode($value));
}
}
return parent::afterFind($event);
}
Thanks for your time.
Christopher
#87
Posted 09 December 2011 - 09:37 AM
#88
Posted 09 December 2011 - 09:41 AM
Da:Sourcerer, on 09 December 2011 - 09:37 AM, said:
That is exactly what is happening... if empty/failed... then NULL/FALSE/GIBERISH data is returned...
Very happy you got decryption working
Cheers
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#89
Posted 09 December 2011 - 11:18 AM
Or if you know how, just add the insurancePolicyNumber to the safe rules.
If not then like Da:Sourcerer says it could just be failing the decryption somehow? No idea how though as it's using the same mechanism to decrypt both and the same mysql method works to decrypt both I guess?
#90
Posted 09 December 2011 - 01:18 PM
#91
Posted 09 December 2011 - 03:39 PM
Quote
I wasn't sure, I know on saving it does the validation check, but wasn't sure if it could be stopping the $this->getOwner()->{key} = from setting it too. Slim chance I know but I had no time to look it up. But as I say, probably not.
#92
Posted 09 December 2011 - 05:14 PM
#93
Posted 09 December 2011 - 06:33 PM
Da:Sourcerer, on 09 December 2011 - 05:14 PM, said:
Then it is my fault for not including what you just said... but if he tries to make a model out of a Form and setting its attributes, the ecryption will fail on save.
www.ramirezcobos.com
www.yiianswers.com
www.2amigos.us
www.getyiistrap.com
www.github.com/tonydspaniard
www.github.com/2amigos
#94
Posted 09 December 2011 - 07:31 PM
#95
Posted 19 December 2012 - 02:46 PM
Emily Dickinson, on 05 December 2011 - 03:30 PM, said:
I was glad to find this example when I kept getting errors trying to use CSecurityManager. Thanks!
But I'd like to point out a potential flaw with these particular routines. The code works for two-way encryption-decryption -- but only because ECB mode ignores the initial vector (iv) parameter. A random iv (as coded) just isn't going to work if you use a mode, such as CBC, that actually uses the iv parameter. In that case you either have to always use the same iv for encryption and decryption, or you have to store the generated iv along with the encrypted data so you can use the same iv to decrypt.
ETA: I've beem examining the code in CSecurityManager. If you look at the encrypt and decrypt methods you see that the encrypt method generates a random iv when encrypting and prepends it to the encrypted value. The decrypt method then slices the string, using the first part as the iv and the second as the value. That's a behind-the-scenes implementation of the second option I mentioned in the previous paragraph.
#96
Posted 08 April 2013 - 05:29 PM
I have implemeneted a model for USER that I would like to use on login but it seems to be doing some strange things.
I set password to the AES Encrypted string....maybe that is my problem user_password in the table is VARCHAR(50)
Is that a problem?
I think I am pretty close based on my debugging the first attempt. I followed Mr Ramirez's directions and updated the value in the table so that it was encrypted. I put a couple debug lines on the login form but the password decrypted did not
work the same...that might be due to the internal function mysqlAESDecrypt($val, $ky) being invoked. I could not call it from my simple test in the login.php file.
Do I need to use VARBINARY ?
#97
Posted 09 April 2013 - 07:49 AM
As far as your question goes: Yes, you would need a VARBINARY field.
And ragarding the problems a few pages earlier: It seems CSecurityManager is actually using the MD5 sum of the supplied key for en- and decryption.
#98
Posted 09 April 2013 - 12:23 PM
I will look into bcrypt and other options.
#99
Posted 09 April 2013 - 04:40 PM
One additional question. If I use the phpass example you posted, (which seems to work if I set my password hashed in the db)
what kinds of modifications to the User model do I need to make for maintaining the user record. Is it as simple
as adding Password1 and Password2 fields to the _form and removing the password field?
Thanks !
#100
Posted 10 April 2013 - 12:07 AM

Help













