Skip to content

Commit

Permalink
CO: update the encrypt and decrypt functions to support all PHP versions
Browse files Browse the repository at this point in the history
  • Loading branch information
hibatallah.aouadni committed Aug 18, 2017
1 parent ebf3643 commit 80ecf62
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions classes/Rijndael.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,47 @@ public function encrypt($plaintext)
if ($length >= 1048576) {
return false;
}
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->_key, $plaintext, MCRYPT_MODE_CBC, $this->_iv)).sprintf('%06d', $length);
$ciphertext = null;
if (function_exists('openssl_encrypt') && version_compare(phpversion(), '5.3.3', '>=')) {
$ciphertext = openssl_encrypt($plaintext, 'AES-128-CBC', $this->_key, OPENSSL_RAW_DATA, $this->_iv);
} elseif (function_exists('mcrypt_encrypt')) {
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->_key, $plaintext, MCRYPT_MODE_CBC, $this->_iv);
} else {
throw new RuntimeException('Either Mcrypt or OpenSSL extension is required to run Prestashop');
}
return base64_encode($ciphertext) . sprintf('%06d', $length);
}

public function decrypt($ciphertext)
{
$output = null;
if (ini_get('mbstring.func_overload') & 2) {
$length = intval(mb_substr($ciphertext, -6, 6, ini_get('default_charset')));
$ciphertext = mb_substr($ciphertext, 0, -6, ini_get('default_charset'));
if (function_exists('openssl_decrypt') && version_compare(phpversion(), '5.3.3', '>=')) {
$output = openssl_decrypt(base64_decode($ciphertext), 'AES-128-CBC', $this->_key, OPENSSL_RAW_DATA, $this->_iv);
} elseif (function_exists('mcrypt_decrypt')) {
$output = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->_key, base64_decode($ciphertext), MCRYPT_MODE_CBC, $this->_iv);
} else {
throw new RuntimeException('Either Mcrypt or OpenSSL extension is required to run Prestashop');
}
return mb_substr(
mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->_key, base64_decode($ciphertext), MCRYPT_MODE_CBC, $this->_iv),
$output,
0,
$length,
ini_get('default_charset')
);
} else {
$length = intval(substr($ciphertext, -6));
$ciphertext = substr($ciphertext, 0, -6);
return substr(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->_key, base64_decode($ciphertext), MCRYPT_MODE_CBC, $this->_iv), 0, $length);
if (function_exists('openssl_decrypt') && version_compare(phpversion(), '5.3.3', '>=')) {
$output = openssl_decrypt(base64_decode($ciphertext), 'AES-128-CBC', $this->_key, OPENSSL_RAW_DATA, $this->_iv);
} elseif (function_exists('mcrypt_decrypt')) {
$output = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->_key, base64_decode($ciphertext), MCRYPT_MODE_CBC, $this->_iv);
} else {
throw new RuntimeException('Either Mcrypt or OpenSSL extension is required to run Prestashop');
}
return substr($output, 0, $length);
}
}
}

1 comment on commit 80ecf62

@gadnis
Copy link

@gadnis gadnis commented on 80ecf62 Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I print_r($ciphertext), I see bunch of � characters. I googled that maybe it's because of UTF-8. My sites language is Lithuanian, with special characters.
Anyone could help with this?
The error thrown is: IV passed is 7 bytes long....

I figured it out:
Something was wrong with cookie.php Class, I gets wrong IV Constant. I've replaced the cookie PHP Class With the one from the new installation And it works.
Hope that will help for someone

$yourshop/modules/classes/Cookie.php

Please sign in to comment.