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