PHP uses 3des encryption and decryption, hmacsha256 encryption

1. 3des encryption and decryption

As of PHP 7.2 it will be removed from the core code and moved to PECL. The PHP manual gives an alternative on the 7.1 migration page, which is to replace MCrypt with OpenSSL.

Now sort out the replacement scheme of AES encryption and decryption method mcrypt_module_open() in PHP7.2, the following is the original encryption and decryption method:

function encrypt($str) { 
 
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); 
 
        mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv)); 
 
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); 
 
        $pad = $block - (strlen($str) % $block); 
 
        $str .= str_repeat(chr($pad), $pad); 
 
        $encrypted = mcrypt_generic($td, $str); 
 
        mcrypt_generic_deinit($td); 
 
        mcrypt_module_close($td); 
 
        return base64_encode($encrypted); 
 
    } 
 
    function decrypt($code) { 
 
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); 
 
        mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv)); 
 
        $str = mdecrypt_generic($td, base64_decode($code)); 
 
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); 
 
        mcrypt_generic_deinit($td); 
 
        mcrypt_module_close($td); 
 
        return $this->strippadding($str); 
 
    }

After replacement, the new method:

//加密
public function encrypt($data)
{
 
    if ($data== null || empty($data)) {
        return $data;
    }
    $secret_key = "your key";
    $iv = "your iv";
    $result= base64_encode(openssl_encrypt( $data, "aes-256-cbc", $secret_key, OPENSSL_RAW_DATA, $iv));
 
    return $result;
    
}
 
//解密
public function decode($data)
{
    if ($data== null || empty($data)) {
        return $data;
    }
    $secret_key = "your key";
    $iv = "your iv";
    $result= openssl_decrypt(base64_decode($data), "aes-256-cbc", $secret_key, OPENSSL_RAW_DATA, $iv);
 
    return $result;
   
}

Encryption method:

openssl_encrypt($data, $method, $password, $options, $iv)
decryption method:

openssl_decrypt($data, $method, $password, $options, $iv)
parameter description:

$data encrypted plaintext

$method encryption method

1. DES-ECB
2. DES-CBC
3. DES-CTR
4. DES-OFB
5. DES-CFB
$passwd encryption key [password]

$options data format options (optional) [options are:]

1. 0
2. OPENSSL_RAW_DATA=1
3. OPENSSL_ZERO_PADDING=2
4. OPENSSL_NO_PADDING=3
$iv encryption initialization vector (optional)

DES is a common type of symmetric encryption, which is a block algorithm encrypted with a key.

Two, hmacsha256 encryption

Use hash_hmac()

string hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = false ] )


parameter

something

    The hash algorithm name to use, eg: "md5", "sha256", "haval160,4", etc. See hash_algos() for a list of supported algorithms.
data

    The message to hash.
key

    The key to use when generating message digests using HMAC.
raw_output

    Set to TRUE to output raw binary data, set to FALSE to output lowercase hexadecimal string.

return value

If raw_output is set to TRUE, then return the information summary represented by raw binary data, otherwise return the information summary expressed in hexadecimal lowercase string format. Returns FALSE if the algo parameter specifies an algorithm that is not supported.

Guess you like

Origin blog.csdn.net/qq_36611673/article/details/125162556