PHP des encryption + ECB way +PKCS5 padded

Source: Internet
Author: User
Tags pkcs5
PHP des encryption + ECB mode +PKCS5-padded
/** * * Cryptographic function * algorithm: des * Encryption mode: ECB * Complement method: PKCS5 * * @param unknown_type $input */function encryptDesEcbPKCS5 ($input, $key)     {$size = mcrypt_get_block_size (' des ', ' ECB ');         $input = Pkcs5_pad ($input, $size);     $TD = Mcrypt_module_open (' des ', ' ', ' ECB ', ');     $iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);    $iv = 0;     Mcrypt_generic_init ($TD, $key, $IV);     $data = Mdecrypt_generic ($TD, $input);     Mcrypt_generic_deinit ($TD);     Mcrypt_module_close ($TD);     $data = Base64_encode ($data); return $data;     }/** * Decryption function * algorithm: des * Encryption mode: ECB * Complement method: PKCS5 * @param unknown_type $input */function decryptDesEcbPKCS5 ($input, $key) {        $size = Mcrypt_get_block_size (' des ', ' ECB ');     $TD = Mcrypt_module_open (' des ', ' ', ' ECB ', ');    $iv = 0;     Mcrypt_generic_init ($TD, $key, $IV);     $data = Mcrypt_generic ($TD, $input);     Mcrypt_generic_deinit ($TD);     Mcrypt_module_close ($TD);     $data = Pkcs5_unpad ($data, $size); $data = Base64_encoDe ($DATA); return $data;     } function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text. Str_repeat (Chr ($pad), $pad);     } function Pkcs5_unpad ($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); } $str = ' Test crypt '; $key = ' jack11 '; $res = EncryptDesEcbPKCS5 ($str, $key); echo "$res \ n"; $deRes = DecryptDesEcbPKCS5 ($res , $key); echo "\n$deres\n";

?

PHP's DES encryption is very special, and can not be specified

Http://www.herongyang.com/Cryptography/DES-PHP-Block-Padding-in-mcrypt.html

  • 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.