Convert framework from mcrypt to openssl

Viewed 3092

I'm working on developing encrypted session handling. The examples I have been provided use mcrypt, and while I know enough to modify the examples to work for my code, I do not know enough to convert Mcrypt to Openssl.

private function encrypt($data, $key) {
    $salt = 'cH!swe!retReGu7W6bEDRup7usuDUh9THeD2CHeGE*ewr4n39=E@rAsp7c-Ph@pH';
    $key = substr(hash('sha256', $salt.$key.$salt), 0, 32);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv));
    return $encrypted;
}
private function decrypt($data, $key) {
    $salt = 'cH!swe!retReGu7W6bEDRup7usuDUh9THeD2CHeGE*ewr4n39=E@rAsp7c-Ph@pH';
    $key = substr(hash('sha256', $salt.$key.$salt), 0, 32);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($data), MCRYPT_MODE_ECB, $iv);
    $decrypted = rtrim($decrypted, "\0");
    return $decrypted;
}

I was wondering if someone could provide the OpenSSL equivalent for this? For the record, I am not looking to support Mcrypt so no backwards compatibility is required.

1 Answers
Related