简体   繁体   中英

PHP MyCrypt Encoding/Decoding doesn't work

i have a little problem: my decryption won't give me the same string i encoded, and i can't find the problem... looked in other posts, but nothing helpful there here are my functions:

public static function encryptData($data){
    if($key = self::getEncryptionKey()){
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB,$iv));
    } else {
        return false;
    }
}

public static function decryptData($data)
{
    if($key = self::getEncryptionKey()){
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($data), MCRYPT_MODE_ECB,$iv);
    } else {
        return false;
    }

}

where's the problem? i'm kinda desperate here...

To decrypt you need everything exactly the same at both sides. key, mode, IV and padding.

Looking at your code, you appear to be generating a new IV for decryption. Don't. Use the same IV as you used to encrypt.

You, correctly, specify a mode explicitly, but you pick the worst possible mode. Don't use ECB, it leaks information. Use CBC or CTR mode instead.

You don't specify a padding. Far better to specify it explicitly, use PKCS7 with Rijndael.

If none of that helps, then check your key, byte by byte, to make sure it is the same for both encryption and decryption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM