Home  >  Article  >  php教程  >  PHP中的 Mcrypt 可逆加密算法

PHP中的 Mcrypt 可逆加密算法

WBOY
WBOYOriginal
2016-06-13 10:11:57793browse

PHP中的 Mcrypt 可逆加密算法

    $td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); //使用MCRYPT_DES算法,ecb模式  

     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);  
    $ks = mcrypt_enc_get_key_size($td);  
  
    $key = "ery secret key";//密钥  
     $key = substr(md5($key), 0, $ks);  
  
    mcrypt_generic_init($td, $key, $iv); //初始处理  
  
    //加密  
    $encrypted = mcrypt_generic($td, 'This is very important data');  
  
    //结束处理  
    mcrypt_generic_deinit($td);  
  
    //初始解密处理  
    mcrypt_generic_init($td, $key, $iv);  
  
    //解密  
    $decrypted = mdecrypt_generic($td, $encrypted);  
  
    //结束  
    mcrypt_generic_deinit($td);  
  
    mcrypt_module_close($td);  
  
    //解密后,可能会有后续的,需去掉  
    echo trim($decrypted) . "n";  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn