PHP7.1 mcrypt alternative

前端 未结 5 750
梦毁少年i
梦毁少年i 2020-11-27 21:57

Mcrypt function has been deprecated as of PHP 7.1.0.

My deprecated string encode / decode functions:

$key: secret key
$str: string


$encoded = base6         


        
相关标签:
5条回答
  • 2020-11-27 22:06

    You should use openssl_encrypt instead.

    0 讨论(0)
  • 2020-11-27 22:13

    For MCRYPT_RIJNDAEL_256 I posted a full answer for PHP7.3 here: https://stackoverflow.com/a/53937314/243782

    snippet:

    works like this with the phpseclib library

    $rijndael = new \phpseclib\Crypt\Rijndael(\phpseclib\Crypt\Rijndael::MODE_ECB);
    $rijndael->setKey(ENCRYPT_KEY);
    $rijndael->setKeyLength(256);
    $rijndael->disablePadding();
    $rijndael->setBlockLength(256);
    
    $decoded = $rijndael->decrypt($term);
    
    0 讨论(0)
  • 2020-11-27 22:24
    echo encrypt_openssl($str, $key);
    
    function encrypt_openssl($msg, $key, $iv = null) {
            $iv_size = openssl_cipher_iv_length('AES-256-CBC');
            if (!$iv) {
                $iv = openssl_random_pseudo_bytes($iv_size);
            }
            $encryptedMessage = openssl_encrypt($msg, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
            return base64_encode($iv . $encryptedMessage);
        }
    

    mcrypt may be removed in PHP 7.1 alternative openssl

    0 讨论(0)
  • 2020-11-27 22:25

    As mentioned above, open_ssl is a good alternative for mcrypt. The only problem I had with open_ssl, is that it cannot be used for large strings.

    I wrote a script (static class), which overcomes this problem (large strings are split up in chunks and encrypted/decrypted separately in the background).

    See public gist: https://gist.github.com/petermuller71/33616d55174d9725fc00a663d30194ba

    0 讨论(0)
  • 2020-11-27 22:26

    Consider using defuse or RNCryptor, they provide a complete solution, are being maintained and is correct.

    0 讨论(0)
提交回复
热议问题