PHP encryption and decryption functions and DES encryption and decryption instances

Source: Internet
Author: User
Tags crypt
This article mainly introduces the encryption and decryption functions in PHP and DES encryption and decryption, and analyzes the principles and application methods of common PHP encryption and decryption algorithms encrypt and DES encryption and decryption in the form of examples, it is very practical. if you need it, you can refer to the example in this article to describe the application of the encryption and decryption function and DES encryption and decryption in PHP. The details are as follows:

Example: example of php encryption and decryption

Encryption function:

/** Function: encrypt strings * parameter 1: content to be encrypted * parameter 2: key */function passport_encrypt ($ str, $ key) {// encryption function srand (double) microtime () * 1000000); $ encrypt_key = md5 (rand (0, 32000); $ ctr = 0; $ tmp = ''; for ($ I = 0; $ I
 
  

The decryption code is as follows:

/** Function: decrypts strings * parameter 1: ciphertext to be decrypted * parameter 2: key */function passport_decrypt ($ str, $ key) {// decryption function $ str = passport_key (base64_decode ($ str), $ key); $ tmp = ''; for ($ I = 0; $ I
   
    

Auxiliary functions:

/** Auxiliary function */function passport_key ($ str, $ encrypt_key) {$ encrypt_key = md5 ($ encrypt_key); $ ctr = 0; $ tmp = ''; for ($ I = 0; $ I
     
      

Use

$ Key = '000000'; $ encrypt = passport_encrypt ($ str, $ key); $ decrypt = passport_decrypt ($ encrypt, $ key); echo 'original: ', $ str."
"; Echo 'ciphertext: ', $ encrypt ."
"; Echo 'Translation: ', $ decrypt ."
";

The specific code of the DES encryption and decryption function is as follows:

       Key = $ key; if ($ iv = 0) {$ this-> iv = $ key;} else {$ this-> iv = $ iv; // mcrypt_create_iv (encrypt (MCRYPT_DES, decrypt), MCRYPT_DEV_RANDOM) ;}} function encrypt ($ str) {// encrypted, returns an uppercase hexadecimal string $ size = encrypt (MCRYPT_DES, MCRYPT_MODE_CBC); $ str = $ this-> pkcs5Pad ($ str, $ size); return strtoupper (bin2hex (mcrypt_cbc (MCRYPT_DES, $ this-> key, $ str, MCRYPT_ENCRYPT, $ This-> iv);} function decrypt ($ str) {// decrypt $ strBin = $ this-> hex2bin (strtolower ($ str )); $ str = mcrypt_cbc (MCRYPT_DES, $ this-> key, $ strBin, MCRYPT_DECRYPT, $ this-> iv); $ str = $ this-> pkcs5Unpad ($ str ); return $ str;} function hex2bin ($ hexData) {$ binData = ""; for ($ I = 0; $ I <strlen ($ hexData); $ I + = 2) {$ binData. = chr (hexdec (substr ($ hexData, $ I, 2);} return $ binData ;} Function pkcs5Pad ($ text, $ blocksize) {$ pad = $ blocksize-(strlen ($ text) % $ blocksize); return $ text. str_repeat (chr ($ pad), $ pad);} function pkcs5Unpad ($ 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) ;}}?>

The test results are as follows:

$str = '12345678';$key = '1234abcd';$crypt = new DES($key);$mstr = $crypt->encrypt($str);$str = $crypt->decrypt($mstr);echo  $str.' <=> '.$mstr;

Example 2

The code is as follows:

/*** Parameters to be passed in the url through get */function args_encode ($ data) {if (is_array ($ data )) {$ string = http_build_query ($ data); return base64_encode ($ string) ;}else {return false ;}} /*** get the parameters passed by the GET method in the url */function getargs () {$ string = base64_decode ($ _ get ['args ']); parse_str ($ string, $ g); return $ g ;}

I hope this article will help you with PHP programming.

For more articles about the encryption and decryption functions and DES encryption and decryption instances in PHP, please follow the PHP Chinese network!


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.