I have been making heavy use of mcrypt in my php app for years, both on win/IIS and on linux. Although I'm running PHP 5.4.28 on my linux server, I just upgraded to PHP 5.6.11 on my windows 8.1 IIS box. And mcrypt no longer works. It doesn't throw any errors that I can see; it just doesn't work. Here is my encryption function:
function Encrypt($text){
global $salt;
if($text != "")
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
else
return "";
}
This works fine on my linux server, but returns blank on my local windows box. From what I have read, mcrypt is built into php 5.6 for windows, so there should be no fiddling with extensions or ini file.
What am I missing?
As SweatCoder stated before, your key for MCRYPT_RIJNDAEL_256 has to have a length of 32. To continue working with your old key being smaller than 32 (here called $oldkey) use
($key is what you called $salt)
Let's look at your code piece by piece. (With mostly cosmetic/whitespace changes.)
I would strongly recommend that you don't use this function for anything. It's not secure. Don't use ECB mode.
Furthermore, unauthenticated encryption is dangerous and libmcrypt is abandonware.
I don't have an answer, but this is rather long for a comment.
Have you tested your configuration to verify that you can you see errors when they occur?
If it's returning then it's not causing a fatal error. Hence the mcrypt functions are defined. Have you checked that the constants are defined? Have you checked that the version of libmcrypt matches the PHP extension requirement?
Have you checked that the inputs to the mcrypt_*() functions look sensible?
Even if the above worked, its a horrible bit of code. The reason for writing code and using high level languages is not so your computer can understand them but so human beings can understand the code:
(laying your code out like this also makes it simpler to inject checks, breakpoints and other debugging measures).
PHP 5.6 has stronger encryption requirements than 5.4. In 5.6 you'll get this warning, which is really an error because it actually causes encryptions and decryptions to fail:
...where "xx" is the length of your salt value. So the salt value has to be exactly 16, 24, or 32 characters in length.