php blowfish加密解密算法

 更新时间:2016年07月02日 13:47:30   投稿:lijiao  
这篇文章主要为大家详细介绍了php blowfish加密解密算法的相关资料,感兴趣的朋友可以参考一下

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

<?php
 
/**
 * php blowfish 算法
 * Class blowfish
 */
class blowfish{
 /**
  * blowfish + cbc模式 + pkcs5补码 加密
  * @param string $str 需要加密的数据
  * @return string 加密后base64加密的数据
  */
 public function blowfish_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);
 
   return base64_encode($cipherText);
  }
 
  mcrypt_module_close($cipher);
 }
 
 /**
  * blowfish + cbc模式 + pkcs5 解密 去补码
  * @param string $str 加密的数据
  * @return string 解密的数据
  */
 public function blowfish_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);
 }
 
 private function pkcs5_pad($text, $blocksize){
  $pad = $blocksize - (strlen ( $text ) % $blocksize);
  return $text . str_repeat ( chr ( $pad ), $pad );
 }
 
 private function pkcs5_unpad($str){
  $pad = ord($str[($len = strlen($str)) - 1]);
  return substr($str, 0, strlen($str) - $pad);
 }
}

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

<?php
 
 $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
  
 // The block-size of the Blowfish algorithm is 64-bits, therefore our IV
 // is always 8 bytes:
 $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 = 'The quick brown fox jumped over the lazy dog';
 printf("clearText: %s\n\n",$cleartext);
  
 // Do 256-bit blowfish encryption:
 // The strengh of the encryption is determined by the length of the key
 // passed to mcrypt_generic_init
 if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
 {
  // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
  $cipherText = mcrypt_generic($cipher,$cleartext );
  mcrypt_generic_deinit($cipher);
  
  // Display the result in hex.
  printf("256-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
 }
 
 // 128-bit blowfish encryption:
 if (mcrypt_generic_init($cipher, $key128, $iv) != -1)
 {
  // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
  $cipherText = mcrypt_generic($cipher,$cleartext );
  mcrypt_generic_deinit($cipher);
  
  // Display the result in hex.
  printf("128-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
 }
 
 // -------
 // Results
 // -------
 // You may use these as test vectors for testing your Blowfish implementations...
 // 
 // iv: 3132333435363738
 // key256: 313233343536373839303132333435364142434445464748494a4b4c4d4e4f50
 // key128: 31323334353637383930313233343536
 // clearText: The quick brown fox jumped over the lazy dog
 // 
 // 256-bit blowfish encrypted:
 // 276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e
 // 
 // 128-bit blowfish encrypted:
 // d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0 
 
?>

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

相关文章

  • php 用checkbox一次性删除多条记录的方法

    php 用checkbox一次性删除多条记录的方法

    一次性删除多条记录,对于比较多的信息,如果没有批量删除功能是非常麻烦的。这样就方便多了。
    2010-02-02
  • 详解php的socket通信

    详解php的socket通信

    这篇文章通过案例演示详细介绍了php的socket通信,对每一段代码进行了分析,每一步骤都配有详细说明,感兴趣的的朋友可以参考下
    2015-08-08
  • PHP计算日期相差天数实例分析

    PHP计算日期相差天数实例分析

    这篇文章主要介绍了PHP计算日期相差天数的方法,结合实例形式分析了PHP日期与时间操作及换算的相关技巧,需要的朋友可以参考下
    2016-02-02
  • php中文语义分析实现方法示例

    php中文语义分析实现方法示例

    这篇文章主要介绍了php中文语义分析实现方法,结合实例形式分析了PHP基于BosonNLP扩展实现中文语义分析的具体操作步骤与相关实现技巧,需要的朋友可以参考下
    2019-09-09
  • PHP中header和session_start前不能有输出原因分析

    PHP中header和session_start前不能有输出原因分析

    在http传输文本中,规定必须 header和content顺序必须是:header在前content在后,并且header的格式必须满足“keyword: value\n”这种格式,大家知道这是为什么吗?接下来为您详细解答
    2013-01-01
  • php5与php7的区别点总结

    php5与php7的区别点总结

    在本篇文章里小编给大家整理的是关于php5与php7的区别是什么的相关知识点内容,有需要的朋友们学习下。
    2019-10-10
  • 浅谈PHP中单引号和双引号到底有啥区别呢?

    浅谈PHP中单引号和双引号到底有啥区别呢?

    本文向我们简单介绍了php中单双引号的区别,以及对比了下javascript中单引号和双引号的区别,方便我们更好的理解,非常实用,推荐给大家
    2015-03-03
  • PHP+MySQL实现无极限分类栏目的方法

    PHP+MySQL实现无极限分类栏目的方法

    这篇文章主要介绍了PHP+MySQL实现无极限分类栏目的方法,涉及php操作数据库查询及结果集递归遍历的技巧,需要的朋友可以参考下
    2015-12-12
  • 安装apache2.2.22配置php5.4(具体操作步骤)

    安装apache2.2.22配置php5.4(具体操作步骤)

    本篇文章是对安装apache2.2.22配置php5.4的具体操作步骤进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • kindeditor 加入七牛云上传的实例讲解

    kindeditor 加入七牛云上传的实例讲解

    下面小编就为大家带来一篇kindeditor 加入七牛云上传的实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11

最新评论