I've 336 'Client' in my database
I tried this code to encrypt
and then decrypt
some data
The esit is: Right: 323 - Wrong: 13
What's the reason because mcrypt
is not fully reversible ?
EDIT: Please don't try to change the nature of the problem, ask to my question or I'll downvote your answers. The problem it's this algorithm seems to be not 100% reversible and this is the problem, THE PROBLEM IS NOT WHY I'M USING IT
$wrong = $right = 0;
foreach ($clients as $c) {
$string_to_encode = trim($c->first_field . ":::" . $c->last_field);
$mc_key = Yii::app()->params["rijndael_key"];
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv_1 = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypt = trim(mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$mc_key,
$string_to_encode,
MCRYPT_MODE_ECB,
$iv_1));
$token = urlencode(base64_encode($crypt));
$string_to_decode = base64_decode(urldecode($token));
$string_decoded = trim(mcrypt_decrypt(
MCRYPT_RIJNDAEL_256,
$mc_key,
$string_to_decode,
MCRYPT_MODE_ECB,
$iv_1));
if ($string_to_encode != $string_decoded) {
echo $string_to_encode . PHP_EOL;
echo "***** ERROR ! ***** " . PHP_EOL;
echo $string_to_encode . PHP_EOL;
echo $string_decoded . PHP_EOL;
$wrong ++;
} else {
$right ++;
}
}
echo "Right $right - Wrong $wrong" . PHP_EOL;
An exmple of differences from plain and decoded string [please note that I changed login name .... ]
customer.email@alice.it:::11734
customer.email@alice.it:::11z͉\wo����y�+� �>�d��x�
The algo is not 100% reversibile. This is the problem, this is the question... obviously I'll not use this in production... it's only a case to demo to YOU that this algo has some problem
Others have mentioned it but you are trimming the results of encryption. The cipher text will appear randomly and some of the items you are encrypting will produce whitespace at the end.
If you trim the cipher text you are losing information and the string will not decode properly.