How to encrypt/decrypt data in php?

前端 未结 6 930
北恋
北恋 2020-11-22 07:17

I\'m currently a student and I\'m studying PHP, I\'m trying to make a simple encrypt/decrypt of data in PHP. I made some online research and some of them were quite confusin

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 07:44

         function my_simple_crypt( $string, $action = 'e' ) {
            // you may change these values to your own
            $secret_key = 'my_simple_secret_key';
            $secret_iv = 'my_simple_secret_iv';
    
            $output = false;
            $encrypt_method = "AES-256-CBC";
            $key = hash( 'sha256', $secret_key );
            $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
    
            if( $action == 'e' ) {
                $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
            }
            else if( $action == 'd' ){
                $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
            }
    
            return $output;
        }
    

提交回复
热议问题