dainifeng
V2EX  ›  问与答

请问在 php7.1 里, mcrypt_encrypt()这个算法 MCRYPT_RIJNDAEL_256 ,如何用 OpenSSL 替换呢?

  •  
  •   dainifeng · Aug 17, 2018 · 2626 views
    This topic created in 2919 days ago, the information mentioned may be changed or developed.

    项目中使用了 mcrypt 的 MCRYPT_RIJNDAEL_256 这个算法,找了好久也没找到好的解决方案! 用 OpenSSL 的 AES-256-CBC 或者 ARS-128-CBC 里面的偏移量只能使用 16 位,而不能使用 32 位,瞬间崩溃陷入死循环了。 下面是加密解密算法,敢问改如何用 openssl 进行处理呢?希望万能网友能给予支持!

    /**
     * 加密数据
     *
     * @param null $data
     * @return string
     */
    
    function safeEncrypt($data = null,$key=null) {
        $ivSize     = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); // 向量大小
        $iv         = mcrypt_create_iv($ivSize, MCRYPT_RAND);// 向量值
        $cryptText  = rtrim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv), "\0"); // 加密
    
        return base64_encode($iv . '$$' . $cryptText);
    }
    
    /**
     * 解密数据
     *
     * @param null $data
     * @return string
     */
    function safeDecrypt($data = null,$key=null) {
    
        $data   = base64_decode($data);
        $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
        $iv     = substr($data, 0, $ivSize);
        $text   = substr($data, $ivSize + 2);
        $cryptText = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv), "\0");
    
        return $cryptText;
    }
    
    1 replies    2018-08-17 23:33:29 +08:00
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1039 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:28 · PVG 07:28 · LAX 16:28 · JFK 19:28
    ♥ Do have faith in what you're doing.