mcrypt is deprecated, what is the alternative?

前端 未结 10 2321
青春惊慌失措
青春惊慌失措 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:32

    I was able to translate my Crypto object

    • Get a copy of php with mcrypt to decrypt the old data. I went to http://php.net/get/php-7.1.12.tar.gz/from/a/mirror, compiled it, then added the ext/mcrypt extension (configure;make;make install). I think I had to add the extenstion=mcrypt.so line to the php.ini as well. A series of scripts to build intermediate versions of the data with all data unencrypted.

    • Build a public and private key for openssl

      openssl genrsa -des3 -out pkey.pem 2048
      (set a password)
      openssl rsa -in pkey.pem -out pkey-pub.pem -outform PEM -pubout
      
    • To Encrypt (using public key) use openssl_seal. From what I've read, openssl_encrypt using an RSA key is limited to 11 bytes less than the key length (See http://php.net/manual/en/function.openssl-public-encrypt.php comment by Thomas Horsten)

      $pubKey = openssl_get_publickey(file_get_contents('./pkey-pub.pem'));
      openssl_seal($pwd, $sealed, $ekeys, [ $pubKey ]);
      $encryptedPassword = base64_encode($sealed);
      $key = base64_encode($ekeys[0]);
      

    You could probably store the raw binary.

    • To Decrypt (using private key)

      $passphrase="passphrase here";
      $privKey = openssl_get_privatekey(file_get_contents('./pkey.pem'), $passphrase);
      // I base64_decode() from my db columns
      openssl_open($encryptedPassword, $plain, $key, $privKey);
      echo "

      Password=$plain

      ";

    P.S. You can't encrypt the empty string ("")

    P.P.S. This is for a password database not for user validation.

提交回复
热议问题