PHP 3des encryption and decryption old and new methods can be connected to encryption

1. Old 3des encryption and decryption method

<?php
class Encrypt_3DES
{
    //加密秘钥,
    private $_key;
    private $_iv;
    public function __construct($key, $iv)
    {
        $this->_key = $key;
        $this->_iv = $iv;
    }
    /**
     * 对字符串进行3DES加密
     * @param string 要加密的字符串
     * @return mixed 加密成功返回加密后的字符串,否则返回false
     */
    public function encrypt3DES($str)
    {
        $td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
        if ($td === false) {
            return false;
        }
        //检查加密key,iv的长度是否符合算法要求
        $key = $this->fixLen($this->_key, mcrypt_enc_get_key_size($td));
        $iv = $this->fixLen($this->_iv, mcrypt_enc_get_iv_size($td));
        if (mcrypt_generic_init($td, $key, $iv) !== 0) {
            return false;
        }
        $result = mcrypt_generic($td, $str);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return base64_encode($result); // 对加密后的结果进行Base64编码
    }
    /**
     * 对加密的字符串进行3DES解密
     * @param string 要解密的字符串
     * @return mixed 解密成功返回解密后的字符串,否则返回false
     */
    public function decrypt3DES($str)
    {
        $td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
        if ($td === false) {
            return false;
        }
        //检查加密key,iv的长度是否符合算法要求
        $key = $this->fixLen($this->_key, mcrypt_enc_get_key_size($td));
        $iv = $this->fixLen($this->_iv, mcrypt_enc_get_iv_size($td));
        if (mcrypt_generic_init($td, $key, $iv) !== 0) {
            return false;
        }
        $str = base64_decode($str); // 对加密字符串进行Base64解码
        $result = mdecrypt_generic($td, $str);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $this->strUnPad($result);
    }
    /**
     * 返回适合算法长度的key,iv字符串
     * @param string $str key或iv的值
     * @param int $td_len 符合条件的key或iv长度
     * @return string 返回处理后的key或iv值
     */
    private function fixLen($str, $td_len)
    {
        $str_len = strlen($str);
        if ($str_len > $td_len) {
            return substr($str, 0, $td_len);
        } else if ($str_len < $td_len) {
            return str_pad($str, $td_len, "\0");
        }
        return $str;
    }
    /**
     * 返回适合算法的分组大小的字符串长度,末尾使用\0补齐
     * @param string $str 要加密的字符串
     * @param int $td_group_len 符合算法的分组长度
     * @return string 返回处理后字符串
     */
    private function strPad($str, $td_group_len)
    {
        $padding_len = $td_group_len - (strlen($str) % $td_group_len);
        return str_pad($str, strlen($str) + $padding_len, "\0");
    }
    /**
     * 返回适合算法的分组大小的字符串长度,去除末尾的\0
     * @param string $str 要解密的字符串
     * @return string 返回处理后字符串
     */
    private function strUnPad($str)
    {
        return rtrim($str, "\0");
    }
}
$key = '1a2bc@';
$iv = '12345678'; // 将iv改为字符串形式
$str = "abcd123";
$encrypt = new Encrypt_3DES($key, $iv);
$jiaData = $encrypt->encrypt3DES($str);
$jieData = $encrypt->decrypt3DES($jiaData);
echo "未加密字符串:".$str;
echo "\n";
echo "加密字符串:".$jiaData;
echo "\n";
echo "解密密字符串:".$jieData;

 2. The new 3des encryption and decryption method

<?php
class TripleDes {
    private $key;
    private $iv;

    public function __construct($key, $iv) {
        $this->key = $key;
        $this->iv = $iv;
    }

    public function encrypt($data) {
        $encrypted = openssl_encrypt($data, 'des-ede3-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
        return base64_encode($encrypted);
    }

    public function decrypt($encryptedData) {
        $decrypted = openssl_decrypt(base64_decode($encryptedData), 'des-ede3-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
        return $decrypted;
    }
}
$key = "2312342132"; 
$iv = "12345678"; // 初始化向量长度必须为8位
$des = new TripleDes($key, $iv);
$data = "adsadb123";
echo "要加密的数据:{$data}\n";
$encrypted = $des->encrypt($data);
echo "加密后的数据:" . $encrypted . "\n";
$decrypted = $des->decrypt($encrypted);
echo "解密后的数据:" . $decrypted . "\n";

 In the above code, we have created a TripleDesclass called , which has a constructor for initializing the key and initialization vector. The class defines encrypt()and decrypt()methods for encryption and decryption operations. When used, first create an TripleDesinstance of , and pass the key and initialization vector as parameters to the constructor. Then, encrypt()encrypt the data by calling the instance's method, and call decrypt()the method to decrypt the data. Please note that the 3DES algorithm is no longer considered a secure encryption algorithm, and it is recommended to use a more secure algorithm such as AES instead.

Guess you like

Origin blog.csdn.net/weixin_39934453/article/details/131845834