PHP Version des encryption solution (corresponding to. NET and Java editions)

Source: Internet
Author: User
Tags decrypt

/** *  * des for . NET version  *  @author  administrator * */class des_net{    var   $key;    var  $iv;  //offset           function des_net (  $key,  $iv =0 )  {    //key Length 8 Example: 1234ABCD          $this->key =  $key;         if (  $iv  == 0 )  {              $this->iv =  $key,  //default to $key  as  iv         } else {              $this->iv =  $iv; //mcrypt_create_iv  ( mcrypt_get_block_size  (mcrypt_des , &NBSP;MCRYPT_MODE_CBC), mcrypt_dev_random );         }    }         function  Encrypt ($STR)  {    //encryption, returns an uppercase hexadecimal string         $ size = mcrypt_get_block_size  ( MCRYPT_DES, MCRYPT_MODE_CBC );          $str  =  $this->pkcs5pad  (  $str,  $size  );         return strtoupper ( bin2hex (&NBSP;MCRYPT_CBC (MCRYPT_DES,   $this->key,  $str, mcrypt_encrypt,  $this->iv )  )  );     }         function decrypt ($STR)  {     //decryption          $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, &NBSP;CHR   (  $pad  ), strlen  (  $text  )  -  $pad  )  !=  $pad)             return false;        return  substr  (  $text, 0, - 1 *  $pad  );    }      }/** *  * des java version  *  @author  administrator  * */class DES_JAVA{var  $key Function des_java (  $key  ) {$this->key =   $key;} Function encrypt ($encrypt)  {$encrypt  =  $this->pkcs5_pad ($encrypt), $iv  =  Mcrypt_create_iv (Mcrypt_get_iv_size (MCRYPT_DES,&NBSP;MCRYPT_MODE_ECB),  mcrypt_rand); $passcrypt  =  mcrypt_encrypt (mcrypt_des,  $this->key,  $encrypt, mcrypt_mode_ecb,  $iv); return  strtoupper ( bin2hex ($passcrypt)  );} Function decrypt ($decrypt)  {//  $decoded  = base64_decode ($decrypt) $decoded  =  pack ("h*",  $decrypt); $iv  = mcrypt_create_iv (mcrypt_get_iv_Size (MCRYPT_DES,&NBSP;MCRYPT_MODE_ECB),  mcrypt_rand); $decrypted  = mcrypt_decrypt (MCRYPT_DES,   $this->key,  $decoded, mcrypt_mode_ecb,  $iv);return  $this->pkcs5_unpad ($ decrypted);} Function pkcs5_unpad ($text) {$pad  = ord ($text {strlen ($text)-1});if  ($pad  >  Strlen ($text))  return  $text;if  (strspn ($text, &NBSP;CHR ($pad),  strlen ($text)  - $ PAD)  !=  $pad)  return  $text return substr ($text, 0, -1 *  $pad);} Function pkcs5_pad ($text) {$len  = strlen ($text), $mod  =  $len  % 8; $pad  =  8 -  $mod;return  $text str_repeat (Chr ($pad), $pad);}}


PHP Version des encryption solution (corresponding to. NET and Java editions)

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.