天天看點

10.PHP加密相關

PHP加密函數

10.PHP加密相關
<?php
    $str = 'This is an example!';
    echo '1:'.$str.'<br>';
    $crypttostr = crypt($str);
    echo '2:'.$crypttostr.'<br>';
?>1:Thisisanexample!           .

2:$1$WQ1.nK..$sYGDz2NVVKAPeQT3J1OKw/      

F5重新整理每次都不一樣,因為密碼随機,下面是固定密碼

<?php
    $str = 'This is an example!';
    echo '1:'.$str.'<br>';
    $crypttostr = crypt($str ,'ma');
    echo '2:'.$crypttostr.'<br>';
?>      
<?php
    $str = 'Thisisanexample!';
    echo '1:'.$str.'<br>';
    $crypttostr = md5($str);
    echo '2:'.$crypttostr.'<br>';
?>1:Thisisanexample!

2:ef7e5805358cf0c1e8b5beab5b025d0e      
10.PHP加密相關
<?php
$str = 'Thisisanexample!';
echo '1:'.$str.'<br>';
$crypttostr = sha1($str);
echo '2:'.$crypttostr.'<br>';
?>1:Thisisanexample!

2:ee7b7019ceef7b4122aa861450548f47e4aa591e      

Mcrypt雙向加密

擷取算法和模式

<?php
    $en_dir = mcrypt_list_algorithms();
    echo 'Mcrypt Algorithm:';
    foreach($en_dir as $en_value){
        echo $en_value.",";
    }
    echo '<br>';
    $en_mo = mcrypt_list_modes();
    echo 'Mcrypt Modes:';
    foreach($en_mo as $en_value){
        echo $en_value.",";
    }
?>      

執行結果

McryptAlgorithm:
cast-128,gost,rijndael-128,twofish,cast-256,loki97,rijndael-192,saferplus,wake,blowfish-compat,des,rijndael-256,serpent,xtea,blowfish,enigma,rc2,tripledes,arcfour,

Mcrypt Modes:cbc,cfb,ctr,ecb,ncfb,nofb,ofb,stream,      

加解密(Mcrypt):

<?php
    $str = "JiaMiZhiQian";
    $key = "111";
    $cipher = MCRYPT_DES;
    $modes = MCRYPT_MODE_ECB;
    $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher ,$modes) ,MCRYPT_RAND);
    echo 'JMQ:'.$str.'<br>';
    $str_encrytp = mcrypt_encrypt($cipher,$key,$str ,$modes ,$iv);
    echo 'JMH:'.$str_encrytp.'<br>';
    $str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrytp ,$modes ,$iv);
    echo 'HY:'.$str_decrypt.'<br>';
?>      

Mhash擴充庫

<?php
    $num = mhash_count();
    echo 'Mhash Algorithm:<br>';
    for($i = 0 ;$i < $num ;$i ++){
        echo $i."=>".mhash_get_hash_name($i)." ";
    }
?>Mhash Algorithm:

0=>CRC32 1=>MD5 2=>SHA1 3=>HAVAL256 4=> 5=>RIPEMD160 6=> 7=>TIGER 8=>GOST 9=>CRC32B 10=>HAVAL224 11=>HAVAL192 12=>HAVAL160 13=>HAVAL128 14=>TIGER128 15=>TIGER160 16=>MD4 17=>SHA256 18=>ADLER32 19=>SHA224 20=>SHA512 21=>SHA384 22=>WHIRLPOOL 23=>RIPEMD128 24=>RIPEMD256 25=>RIPEMD320 26=> 27=>SNEFRU256 28=>MD2 29=>FNV132 30=>FNV1A32 31=>FNV164 32=>FNV1A64
<?php
    $filename = 'a.txt';
    $str = file_get_contents($filename);
    $hash = 2;
    $password = '111';
    $salt = '1234';
    $key = mhash_keygen_s2k(1 ,$password ,$salt ,10);
    $str_mhash = bin2hex(mhash($hash ,$str ,$key));
    echo $str_mhash;
?>