现在的态度决定你未来的高度

实用的PHP算法AES加密解密类之OpenSSL (复制可用)

代码部分

    <?php

    class Aes {
        CONST OPTIONS = OPENSSL_RAW_DATA;
    
        /**
         * @param $input
         * @param string $keys
         * @param string $cipher
         * @return string
         */
        public static function encrypt($input, $keys = '', $cipher = 'AES-128-ECB')
        {
            if(empty($keys)) $keys = self::$keys;
            $data = openssl_encrypt($input, $cipher, $keys, self::OPTIONS);
            return bin2hex($data);
        }
    
        /**
         * @param $output
         * @param string $keys
         * @param string $cipher
         * @return string
         */
        public static function decrypt($output, $keys = '', $cipher = 'AES-128-ECB')
        {
            if(empty($keys)) $keys = self::$keys;
    
            $data = hex2bin($output);
            return openssl_decrypt($data, $cipher, $keys, self::OPTIONS);
        }
    }

使用方法

    Aes::encrypt('需要加密的数据');       

    Aes::decrypt('需要解密的字符串');
发表新评论