简体   繁体   English

PHP MyCrypt编码/解码不起作用

[英]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. 键,模式,IV和填充。

Looking at your code, you appear to be generating a new IV for decryption. 查看您的代码,您似乎正在生成用于解密的新IV。 Don't. 别。 Use the same IV as you used to encrypt. 使用与加密相同的IV。

You, correctly, specify a mode explicitly, but you pick the worst possible mode. 您正确地明确指定了一种模式,但是选择了最差的模式。 Don't use ECB, it leaks information. 不要使用ECB,它会泄漏信息。 Use CBC or CTR mode instead. 请改用CBC或CTR模式。

You don't specify a padding. 您不指定填充。 Far better to specify it explicitly, use PKCS7 with Rijndael. 要明确指定它更好,将PKCS7与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. 如果以上方法均无济于事,请逐字节检查密钥,以确保加密和解密都相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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