Example of symmetric encryption function implemented by PHP based on mcript extension

  • 2021-11-29 06:16:32
  • OfStack

This paper describes the symmetric encryption function of PHP based on mcript extension. Share it for your reference, as follows:

1. Symmetric encryption mainly uses the same key to achieve, while asymmetric encryption uses public key and private key to encrypt. Comparatively speaking, symmetric encryption is faster, but its security is lower

Why use base64_encode() It is mainly used to pass parameters. After direct encryption, it is not a string that we can understand, that is, what everyone calls garbled code

2. Encryption (using mcript extension, DES type), the key points are $key="key:1111" , same key string


<?php
header("content-type:text/html;charset=utf-8");
$str = " I am the content before encryption "; // Encrypted content 
$key = "key:1111"; // Key 
$cipher = MCRYPT_DES; // Password type 
$modes = MCRYPT_MODE_ECB; // Cryptographic mode 
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);// Initialization vector 
echo " Encrypted plaintext: ".$str."<p>";
$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); // Encryption function 
echo " Encrypted ciphertext: ".$str_encrypt." <p>";
echo $str_encrypt=base64_encode($str_encrypt);

Run results:

Encrypted plaintext: I am the content before encryption
Encrypted ciphertext: v "c ~ Zi

trn1duq6vt4i8v66Ea9jo7qZ2X7JWmkf

3. Decrypt, $key= "key: 1111", 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; // Cryptographic mode 
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);// Initialization vector 
$str_encrypt="trn1duq6vt4i8v66Ea9jo7qZ2X7JWmkf";// The value here is the first 2 In step $str_encrypt=base64_encode($str_encrypt);
$str_encrypt=base64_decode($str_encrypt);
echo " Encrypted ciphertext: ".$str_encrypt." <p>";
$str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); // Decryption function 
echo " Restore: ".$str_decrypt;

Run results:

Encrypted ciphertext: v "c ~ Zi
Restore: I am the content before encryption

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Online DES Encryption/Decryption Tool
http://tools.ofstack.com/password/des_encode

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of php Encryption Methods", "Summary of PHP Encoding and Transcoding Operation Skills", "Summary of PHP Mathematical Operation Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial of PHP Data Structure and Algorithm", "Summary of php Programming Algorithm" and "Summary of php Regular Expression Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: