php blowfish加密解密算法
PHPBlowfish算法的加密解密,供大家参考,具体内容如下
<?php
/**
*phpblowfish算法
*Classblowfish
*/
classblowfish{
/**
*blowfish+cbc模式+pkcs5补码加密
*@paramstring$str需要加密的数据
*@returnstring加密后base64加密的数据
*/
publicfunctionblowfish_cbc_pkcs5_encrypt($str)
{
$cipher=mcrypt_module_open(MCRYPT_BLOWFISH,'',MCRYPT_MODE_CBC,'');
//pkcs5补码
$size=mcrypt_get_block_size(MCRYPT_BLOWFISH,MCRYPT_MODE_CBC);
$str=$this->pkcs5_pad($str,$size);
if(mcrypt_generic_init($cipher,$this->key,$this->iv)!=-1)
{
$cipherText=mcrypt_generic($cipher,$str);
mcrypt_generic_deinit($cipher);
returnbase64_encode($cipherText);
}
mcrypt_module_close($cipher);
}
/**
*blowfish+cbc模式+pkcs5解密去补码
*@paramstring$str加密的数据
*@returnstring解密的数据
*/
publicfunctionblowfish_cbc_pkcs5_decrypt($str)
{
$cipher=mcrypt_module_open(MCRYPT_BLOWFISH,'',MCRYPT_MODE_CBC,'');
if(mcrypt_generic_init($cipher,$this->key,$this->iv)!=-1)
{
$cipherText=mdecrypt_generic($cipher,base64_decode($str));
mcrypt_generic_deinit($cipher);
return$this->pkcs5_unpad($cipherText);
}
mcrypt_module_close($cipher);
}
privatefunctionpkcs5_pad($text,$blocksize){
$pad=$blocksize-(strlen($text)%$blocksize);
return$text.str_repeat(chr($pad),$pad);
}
privatefunctionpkcs5_unpad($str){
$pad=ord($str[($len=strlen($str))-1]);
returnsubstr($str,0,strlen($str)-$pad);
}
}
BlowFish加密算法在php的使用第二例
<?php
$cipher=mcrypt_module_open(MCRYPT_BLOWFISH,'',MCRYPT_MODE_CBC,'');
//Theblock-sizeoftheBlowfishalgorithmis64-bits,thereforeourIV
//isalways8bytes:
$iv='12345678';
$key256='1234567890123456ABCDEFGHIJKLMNOP';
$key128='1234567890123456';
printf("iv:%s\n",bin2hex($iv));
printf("key256:%s\n",bin2hex($key256));
printf("key128:%s\n",bin2hex($key128));
$cleartext='Thequickbrownfoxjumpedoverthelazydog';
printf("clearText:%s\n\n",$cleartext);
//Do256-bitblowfishencryption:
//Thestrenghoftheencryptionisdeterminedbythelengthofthekey
//passedtomcrypt_generic_init
if(mcrypt_generic_init($cipher,$key256,$iv)!=-1)
{
//PHPpadswithNULLbytesif$cleartextisnotamultipleoftheblocksize..
$cipherText=mcrypt_generic($cipher,$cleartext);
mcrypt_generic_deinit($cipher);
//Displaytheresultinhex.
printf("256-bitblowfishencrypted:\n%s\n\n",bin2hex($cipherText));
}
//128-bitblowfishencryption:
if(mcrypt_generic_init($cipher,$key128,$iv)!=-1)
{
//PHPpadswithNULLbytesif$cleartextisnotamultipleoftheblocksize..
$cipherText=mcrypt_generic($cipher,$cleartext);
mcrypt_generic_deinit($cipher);
//Displaytheresultinhex.
printf("128-bitblowfishencrypted:\n%s\n\n",bin2hex($cipherText));
}
//-------
//Results
//-------
//YoumayusetheseastestvectorsfortestingyourBlowfishimplementations...
//
//iv:3132333435363738
//key256:313233343536373839303132333435364142434445464748494a4b4c4d4e4f50
//key128:31323334353637383930313233343536
//clearText:Thequickbrownfoxjumpedoverthelazydog
//
//256-bitblowfishencrypted:
//276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e
//
//128-bitblowfishencrypted:
//d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0
?>
以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。