mcrypt is deprecated, what is the alternative?

前端 未结 10 2509
青春惊慌失措
青春惊慌失措 2020-11-22 08:05

The mcrypt-extension is deprecated will be removed in PHP 7.2 according to the comment posted here. So I am looking for an alternative way to encrypt passwords.

Righ

10条回答
  •  甜味超标
    2020-11-22 08:44

    You should use OpenSSL over mcrypt as it's actively developed and maintained. It provides better security, maintainability and portability. Secondly it performs AES encryption/decryption much faster. It uses PKCS7 padding by default, but you can specify OPENSSL_ZERO_PADDING if you need it. To use with a 32-byte binary key, you can specify aes-256-cbc which is much obvious than MCRYPT_RIJNDAEL_128.

    Here is the code example using Mcrypt:

    Unauthenticated AES-256-CBC encryption library written in Mcrypt with PKCS7 padding.

    /**
     * This library is unsafe because it does not MAC after encrypting
     */
    class UnsafeMcryptAES
    {
        const CIPHER = MCRYPT_RIJNDAEL_128;
    
        public static function encrypt($message, $key)
        {
            if (mb_strlen($key, '8bit') !== 32) {
                throw new Exception("Needs a 256-bit key!");
            }
            $ivsize = mcrypt_get_iv_size(self::CIPHER);
            $iv = mcrypt_create_iv($ivsize, MCRYPT_DEV_URANDOM);
    
            // Add PKCS7 Padding
            $block = mcrypt_get_block_size(self::CIPHER);
            $pad = $block - (mb_strlen($message, '8bit') % $block, '8bit');
            $message .= str_repeat(chr($pad), $pad);
    
            $ciphertext = mcrypt_encrypt(
                MCRYPT_RIJNDAEL_128,
                $key,
                $message,
                MCRYPT_MODE_CBC,
                $iv
            );
    
            return $iv . $ciphertext;
        }
    
        public static function decrypt($message, $key)
        {
            if (mb_strlen($key, '8bit') !== 32) {
                throw new Exception("Needs a 256-bit key!");
            }
            $ivsize = mcrypt_get_iv_size(self::CIPHER);
            $iv = mb_substr($message, 0, $ivsize, '8bit');
            $ciphertext = mb_substr($message, $ivsize, null, '8bit');
    
            $plaintext = mcrypt_decrypt(
                MCRYPT_RIJNDAEL_128,
                $key,
                $ciphertext,
                MCRYPT_MODE_CBC,
                $iv
            );
    
            $len = mb_strlen($plaintext, '8bit');
            $pad = ord($plaintext[$len - 1]);
            if ($pad <= 0 || $pad > $block) {
                // Padding error!
                return false;
            }
            return mb_substr($plaintext, 0, $len - $pad, '8bit');
        }
    }
    

    And here is the version written using OpenSSL:

    /**
     * This library is unsafe because it does not MAC after encrypting
     */
    class UnsafeOpensslAES
    {
        const METHOD = 'aes-256-cbc';
    
        public static function encrypt($message, $key)
        {
            if (mb_strlen($key, '8bit') !== 32) {
                throw new Exception("Needs a 256-bit key!");
            }
            $ivsize = openssl_cipher_iv_length(self::METHOD);
            $iv = openssl_random_pseudo_bytes($ivsize);
    
            $ciphertext = openssl_encrypt(
                $message,
                self::METHOD,
                $key,
                OPENSSL_RAW_DATA,
                $iv
            );
    
            return $iv . $ciphertext;
        }
    
        public static function decrypt($message, $key)
        {
            if (mb_strlen($key, '8bit') !== 32) {
                throw new Exception("Needs a 256-bit key!");
            }
            $ivsize = openssl_cipher_iv_length(self::METHOD);
            $iv = mb_substr($message, 0, $ivsize, '8bit');
            $ciphertext = mb_substr($message, $ivsize, null, '8bit');
    
            return openssl_decrypt(
                $ciphertext,
                self::METHOD,
                $key,
                OPENSSL_RAW_DATA,
                $iv
            );
        }
    }
    

    Source: If You're Typing the Word MCRYPT Into Your PHP Code, You're Doing It Wrong.

提交回复
热议问题