【问题标题】:Replace Mcrypt Encription with OpenSSL Encription for OpenCart CMS将 Mcrypt 加密替换为 OpenCart CMS 的 OpenSSL 加密
【发布时间】:2018-12-12 02:22:52
【问题描述】:

我在system library folder 中有OpenCart 1.5.6.4encryption.php 文件。
encryption.php中的代码是:

<?php
final class Encryption {
    private $key;
    private $iv;

    public function __construct($key) {
        $this->key = hash('sha256', $key, true);
        $this->iv = mcrypt_create_iv(32, MCRYPT_RAND);
    }

    public function encrypt($value) {
        return strtr(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $value, MCRYPT_MODE_ECB, $this->iv)), '+/=', '-_,');
    }

    public function decrypt($value) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->key, base64_decode(strtr($value, '-_,', '+/=')), MCRYPT_MODE_ECB, $this->iv));
    }
}
?>

对于从 php 5.6 迁移到 php 7.2 ,我需要将 Mcrypt Encription 替换为 OpenSSL Encription
我已将 mcrypt_create_iv(32, MCRYPT_RAND) 替换为 openssl_random_pseudo_bytes(32, true) ,但对于 encrypt functiondecrypt function ,我不知道 parameters 用于这些功能。
encription.php 代码需要进行哪些更改?

【问题讨论】:

  • @jiri-hrazdil ,如果你仔细阅读你会发现这两个问题有很大的不同。
  • openssl_encrypt($data=$value, $method="AES-256-CBC", $key=$this-&gt;key, $options=OPENSSL_RAW_DATA, $iv=$this-&gt;iv)。我假设您想使用"AES-256-CBC",因为您的密钥是 256 位,并且您使用的是 IV。如果您坚持使用 ECB(不推荐),只需将“CBC”替换为“ECB”即可。无论哪种方式,您都无法解密您的数据,因为 AES 不支持 256 块大小。
  • @t.m.adam 它完美的工作。
  • 太棒了!由于您使用的是 PHP 7.2 并且兼容性显然不是问题,因此请考虑使用 AEAD 密码

标签: php encryption openssl opencart mcrypt


【解决方案1】:

我最初 wrote this 是为了解决 current encryption class for OC3 出现的空 iv 警告:

警告:openssl_encrypt():使用空的初始化向量 (iv) 可能不安全,不推荐

由于您发布此问题的确切原因,最近将其向后移植到 OC1.5。下面是 system/library/encryption.php 的完全替代品,适用于 OC1.5.6.4 和 PHP7.2:

final class Encryption {

    private $cipher = 'aes-256-ctr';
    private $digest = 'sha256';
    private $key;

    public function __construct($key) {
        $this->key = $key;
    }

    public function encrypt($value) {
        $key       = openssl_digest($this->key, $this->digest, true);
        $iv_length = openssl_cipher_iv_length($this->cipher);
        $iv        = openssl_random_pseudo_bytes($iv_length);
        return base64_encode($iv . openssl_encrypt($value, $this->cipher, $key, OPENSSL_RAW_DATA, $iv));
    }

    public function decrypt($value) {
        $result    = NULL;
        $key       = openssl_digest($this->key, $this->digest, true);
        $iv_length = openssl_cipher_iv_length($this->cipher);
        $value     = base64_decode($value);
        $iv        = substr($value, 0, $iv_length);
        $value     = substr($value, $iv_length);
        if (strlen($iv) == $iv_length) {
            $result = openssl_decrypt($value, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
        }
        return $result;
    }
}

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 2020-04-12
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2019-04-13
    • 2011-05-06
    相关资源
    最近更新 更多