将支付宝SDK中的AES加密替换为openssl方式

骑驴找蚂蚁 · 2018-11-12
   /**
     * @param string $content
     * @return string
     */
    public function encrypt(string $content)
    {
        $secret = base64_decode($this->getEncryptKey());
        $method = $this->getEncryptMethod($secret);
        $encrypt = @openssl_encrypt($content, $method, $secret, OPENSSL_RAW_DATA);
        return base64_encode($encrypt);
    }

    /**
     * @param string $encrypt
     * @return string
     */
    public function decrypt(string $encrypt)
    {
        $secret = base64_decode($this->getEncryptKey());
        $method = $this->getEncryptMethod($secret);
        $content = base64_decode($encrypt);
        return openssl_decrypt($content, $method, $secret, 1);
    }

    /**
     * @param string $secret
     * @return string
     */
    public function getEncryptMethod(string $secret)
    {
        $len = strlen($secret);
        if ($len <= 16) {
            $method = 'AES-128-CBC';
        } elseif ($len > 16 && $len <= 24) {
            $method = 'AES-192-CBC';
        } else {
            $method = 'AES-256-CBC';
        }
        return $method;
    }

全栈工程师

994 浏览
1