Linux - SecurityThis forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm using the following code to encrypt and decrypt a string. It all seems to work fine until I try and compare the decrypted string with the encrypted string:
<?php
// encrypt
$secret="a string I don't want people to see";
$key="A secret key for my cipher";
echo("<BR>secret = $secret");
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $secret, MCRYPT_MODE_ECB, $iv);
echo("<BR>cipher = $cipher");
//decrypt
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$checkSecret=mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cipher, MCRYPT_MODE_ECB, $iv);
echo("<BR>checkSecret = $checkSecret");
if ($checkSecret==$secret) {
echo ("<br>The decrypt has worked");
} else {
echo ("<br>It failed");
}
?>
Results:
secret = a string I don't want people to see
cipher = ¶%‡¿(Œ`O@ƒ¨%–„ü‹ï%ñG'MÇÈ¢¬vƒ¿{¢—›MºÔd??Lp)ô ÂõæSÙ„@ñF‡mœ
checkSecret = a string I don't want people to see
It failed
Try it yourself and see what I mean. The strings are displayed on screen exactly as expected, but the comparison doesn't work.
I've run into this myself. Sometimes when decrypting, there are some non-blank binary characters added to the end of the string. This *may* represent itself as a box or some other odd character when the de-crypted text is displayed. I overcome this by doing a trim() on the de-crypted text, which ususally does the trick (although occasionally I've had to do something like a preg_replace('/[^a-zA-Z0-9 <whatever other valid characters> ]/', '', $decrypted_string)).
TruckStuff, thanks for the advice, trim() seems to work.
However, I'm a bit concerend about your comments "(although occasionally I've had to do something like a preg_replace('/[^a-zA-Z0-9 <whatever other valid characters> ]/', '', $decrypted_string))".
Do you use the preg_replace in any specific circumstances, I'm thinking that although trim seems to work okay for the now, it might break at a later time?
Its more a matter of data filtering/programmer preference. If trim() does the job, use it. If you want to filter the de-crypted data a little more, use preg_replace(). Neither is better than the other for this purpose, although I suppose one could make the argument that one requires a bit more horsepower than the other in a given scenario.
Btw, I think the extra data has something to do with the encryption algorythm being block-level vs. byte-level, but I'm not familiar enough with encryption to know for sure.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.