Cory94bailly Posted July 20, 2009 Share Posted July 20, 2009 <? if (!extension_loaded('mcrypt')) { print "Mcrypt not loaded!"; exit; } // Encrypt Function function mc_encrypt($encrypt, $mc_key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND); $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv)); $encode = base64_encode($passcrypt); return $encode; } // Decrypt Function function mc_decrypt($decrypt, $mc_key) { $decoded = base64_decode($decrypt); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv)); return $decrypted; } echo "<b>Encrypt:</b> ". mc_encrypt("test", "key"); echo "<br>"; echo "<br>"; echo "<b>Decrypt:</b> ".mc_decrypt("test", "key"); ?> Well there's my code to try it out and here's what is being displayed: Encrypt: JmZvVd0H+KeqflJNoQWwS3oJ4LxPvL9yd5R4QG7J Decrypt: ‚Î7'#d;0XÒk†zÈ#×âMeǪ‚šbÃóéMˆ (Incase the forum changes it's format, here:) Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/ Share on other sites More sharing options...
MadTechie Posted July 20, 2009 Share Posted July 20, 2009 and the problem is ? EDIT: Unless the you where expecting the same result the reason your not is because your trying to decrypt unencrypted data.. try $data = mc_encrypt("test", "key"); echo "<b>Encrypt:</b> ". $data; echo "<br>"; echo "<br>"; echo "<b>Decrypt:</b> ".mc_decrypt($data, "key"); Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878678 Share on other sites More sharing options...
Cory94bailly Posted July 20, 2009 Author Share Posted July 20, 2009 NEVER MIND! I'm really stupid.. I'm trying something else, give me a second Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878680 Share on other sites More sharing options...
Cory94bailly Posted July 20, 2009 Author Share Posted July 20, 2009 Ok whatever I do, I can't get them to be the same.. :wtf: I am not getting this: Encrypt: JmZvVd0H+KeqflJNoQWwS3oJ4LxPvL9yd5R4QG7J Decrypt: ƒ£`€<8Òö:0>/ˆ_‹YYVfk¤K‰ƒgc And my code is this: <? if (!extension_loaded('mcrypt')) { print "Mcrypt not loaded!"; exit; } // Encrypt Function function mc_encrypt($encrypt, $mc_key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND); $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv)); $encode = base64_encode($passcrypt); return $encode; } // Decrypt Function function mc_decrypt($decrypt, $mc_key) { $decoded = base64_decode($decrypt); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv)); return $decrypted; } echo "<b>Encrypt:</b> ". mc_encrypt("test", "key"); echo "<br>"; echo "<br>"; echo "<b>Decrypt:</b> ".mc_decrypt("JmZvVd0H+KeqflJNoQWwS3oJ4LxPvL9yd5R4QG7J", "key"); ?> Any help? Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878684 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 It seems to work for other keys. Maybe it has to do with this (from mcrypt_encrypt): The key with which the data will be encrypted. If it's smaller that the required keysize, it is padded with '\0'. It is better not to use ASCII strings for keys. Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878691 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 Hmm... Check this out: $key = 'key'; $message = 'test'; $encrypted = mc_encrypt($message, $key); $decrypted = mc_decrypt($encrypted, $key); if ($decrypted == $message) { echo 'Works'; } else { echo 'Broken'; } That will output "Broken", but if you do $key = 'abc'; it will output "Works". I cannot give an explanation to that. Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878694 Share on other sites More sharing options...
Cory94bailly Posted July 20, 2009 Author Share Posted July 20, 2009 Hmm... Check this out: $key = 'key'; $message = 'test'; $encrypted = mc_encrypt($message, $key); $decrypted = mc_decrypt($encrypted, $key); if ($decrypted == $message) { echo 'Works'; } else { echo 'Broken'; } That will output "Broken", but if you do $key = 'abc'; it will output "Works". I cannot give an explanation to that. Yep.. Same thing for me.. Is there a better encrypt/decrypt function available (Using any form of mcrypt..) Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878711 Share on other sites More sharing options...
Cory94bailly Posted July 20, 2009 Author Share Posted July 20, 2009 Actually.. It seems to work with a longer key.. Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878714 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 Actually.. It seems to work with a longer key.. Yeah, as I said, it only seems to happen if the key is "key". Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878717 Share on other sites More sharing options...
Cory94bailly Posted July 20, 2009 Author Share Posted July 20, 2009 Actually.. It seems to work with a longer key.. Yeah, as I said, it only seems to happen if the key is "key". Ok, thanks I marked it solved.. Jeez.. phpfreaks is always helpful, I love this community Quote Link to comment https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/#findComment-878721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.