1. Introduction to 3DES
_3DES (Triple DES) is an encryption algorithm for the transition from DES to AES (in 1999, NIST designated 3-DES as the transition encryption standard). Its implementation is as follows: Let Ek() and Dk() represent the encryption and decryption process of DES algorithm, K represents the key used by DES algorithm, M represents plaintext, and C represents ciphertext.
The encryption process of 3DES is: C = Ek3 (Dk2 (Ek1 (M))
The decryption process of 3DES is: M = Dk1 (EK2 (Dk3 (C))
_Common "symmetric key" encryption algorithms mainly include DES, 3DES (Triple DES), AES, RC2, RC4, RC5 and Blowfish, etc. I will not mention the implementation of each algorithm here, interested in their own to find.
2. Crypt3Des for Encryption and Decryption
class Crypt3Des { public $key = ""; function __construct($key) { $this->key = $key; } /** * data encryption * @param string $input * @return void */ function encrypt($input) { $size = mcrypt_get_block_size(MCRYPT_3DES, 'ecb'); $input = $this->pkcs5_pad($input, $size); // $key = str_pad($this->key, 24, '0'); $key = $this->key; $td = mcrypt_module_open(MCRYPT_3DES, '', 'ecb', ''); $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); @mcrypt_generic_init($td, $key, $iv); $data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $data = base64_encode($data); return $data; } /** * data decryption * @param string $encrypted * @return void */ function decrypt($encrypted) { // $encrypted = base64_decode($encrypted); // $key = str_pad($this->key, 24, '0'); $key = $this->key; $td = mcrypt_module_open(MCRYPT_3DES, '', 'ecb', ''); $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); @mcrypt_generic_init($td, $key, $iv); $decrypted = mdecrypt_generic($td, $encrypted); mcrypt_generic_deinit($td); mcrypt_module_close($td); $y = $this->pkcs5_unpad($decrypted); // return $decrypted; return $y; } /** * Encrypted Data Filling * @param string $text * @param int $blocksize * @return void */ function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } /** * Interception of decrypted data * @param string $text * @return void */ function pkcs5_unpad($text) { $pad = ord($text{ strlen($text) - 1}); if ($pad > strlen($text)) { return false; } if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) { return false; } return substr($text, 0, -1 * $pad); } }
3. Data Encryption and Decryption
require "Crypt3Des.php"; $key = '1oneSign!@#^*'; // Encryption key, depending on your situation // Instantiate encryption classes $rep = new Crypt3Des($key); $input = "ILoveMe"; $encrypt_card = $rep->encrypt($input); echo '<br>'; echo "Original text:" . $input . "<br/>"; echo "Encryption:" . $encrypt_card . "<br/>"; echo "Decrypt:" . $rep->decrypt($encrypt_card) . "<br/>";
Friendship Tips:
_Recommend encryption classes written by others: php_mcrypt (This class applies only to php version 7.1 or below).