Tumul Kumar
Tumul Kumar

Reputation: 11

Convert encryption algorithm mcrypt to openssl

As php has stopped support for Mcrypt from 7.2 and onwards. I do not know enough to convert Mcrypt to Openssl. I was wondering if someone could provide the OpenSSL equivalent for this? For the record, I am not looking to support Mcrypt so I have to decrypt my mcrypt encrypted strings(passwords) via openSSL.

To Encode via mcrypt->

static function encode($value= NULL, $key= NULL){
    if(!$value){
        return false;
    }
    $text = $value;
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
    return trim(self::safe_b64encode($crypttext));
}
private function safe_b64encode($string= NULL) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
}

Upvotes: 1

Views: 1229

Answers (2)

Joseph_J
Joseph_J

Reputation: 3669

Updated

Unfortunately it looks like there is not a way your going to be able to do what you want. Had you used the MCRYPT_RIJNDAEL_128 mode with a 256 byte key there may have been hope.

AES-256 and MCRYPT_RIJNDAEL_256 encryption are not the same thing, even though AES is basically Rijndael. It all has to do with the block size. What you want to do just is not compatible.

Your options are this:

  • Use a version of PHP that still has the MCRYPT libraries available and decrypt the passwords to a file and then encrypt them with your new encryption method.
  • A PECL option looks available that would allow you to install a pseudo version of MCRYPT with your latest version of PHP.

For just encrypting and decrypting your passwords OpenSSL should be fine, but OpenSSL has limitation especially when you want to encrypt large amounts of data. It requires you to write additional code to break apart your data into smaller chunks before you encrypt and then put it back together after you decrypt.

I highly recommend that you skip OpenSSL and learn the LibSodium library which is now supported on the latest PHP versions.

http://php.net/manual/en/book.sodium.php

Here is a good page to read to get you started.

https://paragonie.com/blog/2015/05/using-encryption-and-authentication-correctly

Some more Libsodium resources.

https://github.com/paragonie/pecl-libsodium-doc/blob/v1/chapters/01-quick-start.md

Good Luck~

Upvotes: 1

Adrian Baginski
Adrian Baginski

Reputation: 336

You have so many choices when it comes to encrypting data, so that's why you should implement it yourself. I recommend you the library diffuse/php-encryption, see this link for a detailed tutorial.

Upvotes: 0

Related Questions