PHP for symmetric encryption with Mcript extensions

Source: Internet
Author: User
Tags key string learn php asymmetric encryption

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.