This article and we share the main is PHP, using the mcript extension to implement symmetric encryption method steps, take a look at it, hope to everyoneLearn PHPhelpful.
1. Symmetric encryption is primarily implemented using the same key, while asymmetric encryption uses public and private keys to encrypt, while symmetric encryption is faster, but less secure
why use Base64_encode (), is mainly used for the transfer of parameters, the direct encryption is not we can understand the string, which is what everyone said garbled
2. Encryption (using Mcript extension, des type), the key point is $key= "key:1111", the same key string
<?php
header ("Content-type:text/html;charset=utf-8");
$str = "I am the content before encryption";//Encrypt content
$key = "key:1111";//Key
$cipher = mcrypt_des;//password Type
$modes = MCRYPT_MODE_ECB;//password mode
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand);//initialization vector
echo "Encrypted plaintext:" $str. "
";
$str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV);//Cryptographic Functions
echo "Cipher cipher:" $str _encrypt. "
";
echo $str _encrypt=base64_encode ($str _encrypt);
3. Decryption, $key = "key:1111", the same key string
<?php
header ("Content-type:text/html;charset=utf-8");
$key = "key:1111";//Key
$cipher = mcrypt_des;//password Type
$modes = MCRYPT_MODE_ECB;//password mode
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand);//initialization vector
$str _encrypt= "TRN1DUQ6VT4I8V66EA9JO7QZ2X7JWMKF";//The value here is the 2nd step in the $str_encrypt=base64_encode ($str _encrypt);
$str _encrypt=base64_decode ($str _encrypt);
echo "Cipher cipher:" $str _encrypt. "
";
$str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV);//decryption function
echo "restore:". $str _decrypt;
Source: Linux commune
PHP for symmetric encryption with Mcript extensions