How do you Encrypt and Decrypt a PHP String?

后端 未结 10 1498
梦毁少年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);
    
    0 讨论(0)
  • 2020-11-22 01:49

    These are compact methods to encrypt / decrypt strings with PHP using AES256 CBC:

    function encryptString($plaintext, $password, $encoding = null) {
        $iv = openssl_random_pseudo_bytes(16);
        $ciphertext = openssl_encrypt($plaintext, "AES-256-CBC", hash('sha256', $password, true), OPENSSL_RAW_DATA, $iv);
        $hmac = hash_hmac('sha256', $ciphertext.$iv, hash('sha256', $password, true), true);
        return $encoding == "hex" ? bin2hex($iv.$hmac.$ciphertext) : ($encoding == "base64" ? base64_encode($iv.$hmac.$ciphertext) : $iv.$hmac.$ciphertext);
    }
    
    function decryptString($ciphertext, $password, $encoding = null) {
        $ciphertext = $encoding == "hex" ? hex2bin($ciphertext) : ($encoding == "base64" ? base64_decode($ciphertext) : $ciphertext);
        if (!hash_equals(hash_hmac('sha256', substr($ciphertext, 48).substr($ciphertext, 0, 16), hash('sha256', $password, true), true), substr($ciphertext, 16, 32))) return null;
        return openssl_decrypt(substr($ciphertext, 48), "AES-256-CBC", hash('sha256', $password, true), OPENSSL_RAW_DATA, substr($ciphertext, 0, 16));
    }
    

    Usage:

    $enc = encryptString("mysecretText", "myPassword");
    $dec = decryptString($enc, "myPassword");
    
    0 讨论(0)
  • 2020-11-22 01:50

    I'm late to the party, but searching for the correct way to do it I came across this page it was one of the top Google search returns, so I will like to share my view on the problem, which I consider it to be up to date at the time of writing this post (beginning of 2017). From PHP 7.1.0 the mcrypt_decrypt and mcrypt_encrypt is going to be deprecated, so building future proof code should use openssl_encrypt and openssl_decrypt

    You can do something like:

    $string_to_encrypt="Test";
    $password="password";
    $encrypted_string=openssl_encrypt($string_to_encrypt,"AES-128-ECB",$password);
    $decrypted_string=openssl_decrypt($encrypted_string,"AES-128-ECB",$password);
    

    Important: This uses ECB mode, which isn't secure. If you want a simple solution without taking a crash course in cryptography engineering, don't write it yourself, just use a library.

    You can use any other chipper methods as well, depending on your security need. To find out the available chipper methods please see the openssl_get_cipher_methods function.

    0 讨论(0)
  • 2020-11-22 01:50

    If you don't want to use library (which you should) then use something like this (PHP 7):

    function sign($message, $key) {
        return hash_hmac('sha256', $message, $key) . $message;
    }
    
    function verify($bundle, $key) {
        return hash_equals(
          hash_hmac('sha256', mb_substr($bundle, 64, null, '8bit'), $key),
          mb_substr($bundle, 0, 64, '8bit')
        );
    }
    
    function getKey($password, $keysize = 16) {
        return hash_pbkdf2('sha256',$password,'some_token',100000,$keysize,true);
    }
    
    function encrypt($message, $password) {
        $iv = random_bytes(16);
        $key = getKey($password);
        $result = sign(openssl_encrypt($message,'aes-256-ctr',$key,OPENSSL_RAW_DATA,$iv), $key);
        return bin2hex($iv).bin2hex($result);
    }
    
    function decrypt($hash, $password) {
        $iv = hex2bin(substr($hash, 0, 32));
        $data = hex2bin(substr($hash, 32));
        $key = getKey($password);
        if (!verify($data, $key)) {
          return null;
        }
        return openssl_decrypt(mb_substr($data, 64, null, '8bit'),'aes-256-ctr',$key,OPENSSL_RAW_DATA,$iv);
    }
    
    $string_to_encrypt='John Smith';
    $password='password';
    $encrypted_string=encrypt($string_to_encrypt, $password);
    $decrypted_string=decrypt($encrypted_string, $password);
    
    0 讨论(0)
提交回复
热议问题