How do you Encrypt and Decrypt a PHP String?

后端 未结 10 1548
梦毁少年i
梦毁少年i 2020-11-22 01:21

What I mean is:

Original String + Salt or Key --> Encrypted String
Encrypted String + Salt or Key --> Decrypted (Original String)

May

10条回答
  •  既然无缘
    2020-11-22 01:48

    Below code work in php for all string with special character

       // Encrypt text --
    
        $token = "9611222007552";
    
          $cipher_method = 'aes-128-ctr';
          $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);  
          $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));  
          $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
        echo    $crypted_token;
        //unset($token, $cipher_method, $enc_key, $enc_iv);
    
        // Decrypt text  -- 
    
        list($crypted_token, $enc_iv) = explode("::", $crypted_token);  
          $cipher_method = 'aes-128-ctr';
          $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
          $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
        echo   $token;
        //unset($crypted_token, $cipher_method, $enc_key, $enc_iv);
    

提交回复
热议问题