PHP Mcrypt - the decryption function does not work for part of the lines

I know there are already a million messages in PHP mcrypt_decrypt, but I could not find one that had the same results as mine. I have a couple of simple encryption / decryption functions that I would like to use to perform two-way data encryption. It is strange that about 4% of any random string that I pass in a function will not successfully decrypt. For example, if I create a "loop cycle" from 0 to 9999 and also encrypt and decrypt a string version of these numbers, the same values ​​will fail every time, and these values ​​depend on the key that I pass to the function. I can pass any key, and although certain values ​​that do not work will change, the percentage of failed values ​​will remain approximately constant.

I tried ECB mode without parameter IV, and I tried CBC mode with parameter IV, and it gives the same results.

Here is my ECB mode encryption function:

function mc_encrypt($string, $mc_key) {
    $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($string), MCRYPT_MODE_ECB));
    $encode = base64_encode($passcrypt);

    return $encode;
}

And here is my decryption in ECB mode:

function mc_decrypt($string, $mc_key) {
    $decoded = base64_decode($string);
    $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB));

    return $decrypted;
}

The CBC mode version was the same, except that it used the mcrypt_create_iv () function to create an IV in encryption and passed it as a parameter in the decryption.

Using these functions on my server with "abc" as the test encryption key, if I run from 0 to 300, the following values ​​will not be decrypted correctly:

4, 6, 70, 145, 151, 176, 237, 254, 275

If I change my encryption key to something else, it will shift which values ​​will return correctly, but it will not change the frequency with which the values ​​are returned.

Any suggestions???

Thanks in advance!

+3
source share
1 answer

trim() , . , mcrypt_encrypt (\ 0), . , $decoded mcrypt_decrypt . mcrypt_decrypt rtrim.

function mc_encrypt($string, $mc_key) {
    $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($string), MCRYPT_MODE_ECB);
    $encode = base64_encode($passcrypt);

    return $encode;
}

function mc_decrypt($string, $mc_key) {
    $decoded = base64_decode($string);
    $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, $decoded, MCRYPT_MODE_ECB));

    return $decrypted;
}
+5

All Articles