当前位置:Gxlcms > PHP教程 > php blowfish加密解密算法

php blowfish加密解密算法

时间:2021-07-01 10:21:17 帮助过:12人阅读

PHP Blowfish 算法的加密解密,供大家参考,具体内容如下

  1. <?php
  2. /**
  3. * php blowfish 算法
  4. * Class blowfish
  5. */
  6. class blowfish{
  7. /**
  8. * blowfish + cbc模式 + pkcs5补码 加密
  9. * @param string $str 需要加密的数据
  10. * @return string 加密后base64加密的数据
  11. */
  12. public function blowfish_cbc_pkcs5_encrypt($str)
  13. {
  14. $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
  15. //pkcs5补码
  16. $size = mcrypt_get_block_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
  17. $str = $this->pkcs5_pad($str, $size);
  18. if (mcrypt_generic_init($cipher, $this->key, $this->iv) != -1)
  19. {
  20. $cipherText = mcrypt_generic($cipher, $str);
  21. mcrypt_generic_deinit($cipher);
  22. return base64_encode($cipherText);
  23. }
  24. mcrypt_module_close($cipher);
  25. }
  26. /**
  27. * blowfish + cbc模式 + pkcs5 解密 去补码
  28. * @param string $str 加密的数据
  29. * @return string 解密的数据
  30. */
  31. public function blowfish_cbc_pkcs5_decrypt($str)
  32. {
  33. $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
  34. if (mcrypt_generic_init($cipher, $this->key, $this->iv) != -1)
  35. {
  36. $cipherText = mdecrypt_generic($cipher, base64_decode($str));
  37. mcrypt_generic_deinit($cipher);
  38. return $this->pkcs5_unpad($cipherText);
  39. }
  40. mcrypt_module_close($cipher);
  41. }
  42. private function pkcs5_pad($text, $blocksize){
  43. $pad = $blocksize - (strlen ( $text ) % $blocksize);
  44. return $text . str_repeat ( chr ( $pad ), $pad );
  45. }
  46. private function pkcs5_unpad($str){
  47. $pad = ord($str[($len = strlen($str)) - 1]);
  48. return substr($str, 0, strlen($str) - $pad);
  49. }
  50. }

BlowFish加密算法在php的使用第二例

  1. <?php
  2. $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
  3. // The block-size of the Blowfish algorithm is 64-bits, therefore our IV
  4. // is always 8 bytes:
  5. $iv = '12345678';
  6. $key256 = '1234567890123456ABCDEFGHIJKLMNOP';
  7. $key128 = '1234567890123456';
  8. printf("iv: %s\n",bin2hex($iv));
  9. printf("key256: %s\n",bin2hex($key256));
  10. printf("key128: %s\n",bin2hex($key128));
  11. $cleartext = 'The quick brown fox jumped over the lazy dog';
  12. printf("clearText: %s\n\n",$cleartext);
  13. // Do 256-bit blowfish encryption:
  14. // The strengh of the encryption is determined by the length of the key
  15. // passed to mcrypt_generic_init
  16. if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
  17. {
  18. // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
  19. $cipherText = mcrypt_generic($cipher,$cleartext );
  20. mcrypt_generic_deinit($cipher);
  21. // Display the result in hex.
  22. printf("256-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
  23. }
  24. // 128-bit blowfish encryption:
  25. if (mcrypt_generic_init($cipher, $key128, $iv) != -1)
  26. {
  27. // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
  28. $cipherText = mcrypt_generic($cipher,$cleartext );
  29. mcrypt_generic_deinit($cipher);
  30. // Display the result in hex.
  31. printf("128-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
  32. }
  33. // -------
  34. // Results
  35. // -------
  36. // You may use these as test vectors for testing your Blowfish implementations...
  37. //
  38. // iv: 3132333435363738
  39. // key256: 313233343536373839303132333435364142434445464748494a4b4c4d4e4f50
  40. // key128: 31323334353637383930313233343536
  41. // clearText: The quick brown fox jumped over the lazy dog
  42. //
  43. // 256-bit blowfish encrypted:
  44. // 276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e
  45. //
  46. // 128-bit blowfish encrypted:
  47. // d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0
  48. ?>

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

人气教程排行