• PHP使用DES進行加密和解密

    3
    PHP HTML C/C++ Go 12455 次瀏覽

    DES是一種標準的數據加密算法,php中有一個擴展可以支持DES的加密算法,是:extension=php_mcrypt.dll

    在配置文件中將這個擴展打開還不能夠在windows環境下使用

    需要將PHP文件夾下的 libmcrypt.dll 拷貝到系統的 system32 目錄下,這是通過phpinfo可以查看到mcrypt表示這個模塊可以正常試用了。

    下面是PHP中使用DES加密解密的一個例子:

    //$input - stuff to decrypt
        //$key - the secret key to use
    
        function do_mencrypt($input, $key)
        {
            $input = str_replace(""n", "", $input);
            $input = str_replace(""t", "", $input);
            $input = str_replace(""r", "", $input);
            $key = substr(md5($key), 0, 24);
            $td = mcrypt_module_open('tripledes', '', 'ecb', '');
            $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            mcrypt_generic_init($td, $key, $iv);
            $encrypted_data = mcrypt_generic($td, $input);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return trim(chop(base64_encode($encrypted_data)));
        }
        
        //$input - stuff to decrypt
        //$key - the secret key to use
        
        function do_mdecrypt($input, $key)
        {
            $input = str_replace(""n", "", $input);
            $input = str_replace(""t", "", $input);
            $input = str_replace(""r", "", $input);
            $input = trim(chop(base64_decode($input)));
            $td = mcrypt_module_open('tripledes', '', 'ecb', '');
            $key = substr(md5($key), 0, 24);
            $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            mcrypt_generic_init($td, $key, $iv);
            $decrypted_data = mdecrypt_generic($td, $input);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return trim(chop($decrypted_data));
    
        }    

    原文:http://www.cnblogs.com/cocowool/archive/2009/01/07/1371309.html

    相似問題

    相關經驗

    相關資訊

    相關文檔

  • sesese色