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

Source: Internet
Author: User
/** * * DES for.    NET version * @author Administrator * */class des_net{var $key; var $iv;        Offset function des_net ($key, $iv =0) {//key length 8 for example: 1234ABCD $this->key = $key; if ($iv = = 0) {$this->iv = $key;//default to $key as IV} else {$this->iv = $iv;//mcrypt_c        Reate_iv (Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC), mcrypt_dev_random); }} function Encrypt ($STR) {//encryption, return uppercase hexadecimal string $size = Mcrypt_get_block_size (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); }}/** * * 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, MCRYPT_MODE_ECB), mcrypt_rand); $passcrypt = Mcrypt_encrypt (mcrypt_des, $this-&gtKey, $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, 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, 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);}}
  • 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.