标签:des style c class blog code
不清楚DES算法的可以看维基百科
<?php class DESede{ /** * 加密 * @param $data 待加密明文 * @param $key DES私钥 * @param $use3des 是否启用3DES加密,默认不启用 */ function encrypt($data=‘‘, $key=‘‘, $use3des = False) { if (empty($data) || empty($key)) { return False; } $cipher = $use3des ? MCRYPT_TRIPLEDES : MCRYPT_DES; $modes = MCRYPT_MODE_ECB; # Add PKCS7 padding. $block = mcrypt_get_block_size($cipher, $modes); $pad = $block - (strlen($data) % $block); $data .= str_repeat(chr($pad), $pad); $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, $modes), MCRYPT_RAND); $encrypted = @mcrypt_encrypt($cipher, $key, $data, $modes, $iv); return $encrypted; } /** * 解密 * @param $data 待解密密文 * @param $key DES私钥 * @param $use3des 是否启用3DES加密,默认不启用 */ function decrypt($data=‘‘, $key=‘‘, $use3des = False) { if (empty($data) || empty($key)) { return False; } $cipher = $use3des ? MCRYPT_TRIPLEDES : MCRYPT_DES; $modes = MCRYPT_MODE_ECB; $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, $modes), MCRYPT_RAND); $data = @mcrypt_decrypt($cipher, $key, $data, $modes, $iv); # Strip padding out. $block = mcrypt_get_block_size($cipher, $modes); $pad = ord($data[($len = strlen($data)) - 1]); $decrypted = substr($data, 0, strlen($data) - $pad); return $decrypted; } }
php实现对称加密算法DESede,布布扣,bubuko.com
标签:des style c class blog code
原文地址:http://www.cnblogs.com/kennyhr/p/3746107.html