php blowfish加密解密算法


Posted in PHP onJuly 02, 2016

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 相关文章推荐
如何对PHP程序中的常见漏洞进行攻击(下)
Oct 09 PHP
PHP图片上传类带图片显示
Nov 25 PHP
php Sql Server连接失败问题及解决办法
Aug 07 PHP
开启CURL扩展,让服务器支持PHP curl函数(远程采集)
Mar 19 PHP
php array_filter除去数组中的空字符元素
Jun 21 PHP
深入php self与$this的详解
Jun 08 PHP
php类常量的使用详解
Jun 08 PHP
解析zend Framework如何自动加载类
Jun 28 PHP
PHP中数组的分组排序实例
Jun 01 PHP
PHP中echo,print_r与var_dump区别分析
Sep 29 PHP
php提交post数组参数实例分析
Dec 17 PHP
解决出现SoapFault (looks like we got no XML document)的问题
Jun 24 PHP
对比PHP对MySQL的缓冲查询和无缓冲查询
Jul 01 #PHP
PHP处理CSV表格文件的常用操作方法总结
Jul 01 #PHP
PHP读书笔记整理_结构语句详解
Jul 01 #PHP
PHP安装GeoIP扩展根据IP获取地理位置及计算距离的方法
Jul 01 #PHP
php投票系统之增加与删除投票(管理员篇)
Jul 01 #PHP
PHP读书笔记_运算符详解
Jul 01 #PHP
php+MySql实现登录系统与输出浏览者信息功能
Jul 01 #PHP
You might like
基于mysql的bbs设计(五)
2006/10/09 PHP
PHP5.2下chunk_split()函数整数溢出漏洞 分析
2007/06/06 PHP
PHP中返回引用类型的方法
2015/04/03 PHP
PHP魔术方法之__call与__callStatic使用方法
2017/07/23 PHP
详解php中生成标准uuid(guid)的方法
2019/04/28 PHP
javascript attachEvent和addEventListener使用方法
2009/03/19 Javascript
js GridView 实现自动计算操作代码
2009/03/25 Javascript
JavaScript 对象链式操作测试代码
2010/04/25 Javascript
window.ActiveXObject使用说明
2010/11/08 Javascript
javascript阻止scroll事件多次执行的思路及实现
2013/11/08 Javascript
JavaScript中的this关键字使用详解
2015/08/14 Javascript
JS+CSS实现带有碰撞缓冲效果的竖向导航条代码
2015/09/15 Javascript
jQuery通过deferred对象管理ajax异步
2016/05/20 Javascript
深入理解jquery中的事件与动画
2016/05/24 Javascript
JavaScript实现图片懒加载(Lazyload)
2016/11/28 Javascript
用headjs来管理和加载js 提高网站加载速度
2016/11/29 Javascript
原生js实现商品放大镜效果
2017/01/12 Javascript
d3.js入门教程之数据绑定详解
2017/04/28 Javascript
JavaScript中数组常见操作技巧
2017/09/01 Javascript
使用vue重构资讯页面的实例代码解析
2019/11/26 Javascript
[01:03:36]DOTA2-DPC中国联赛 正赛 VG vs Magma BO3 第二场 1月26日
2021/03/11 DOTA
python3调用R的示例代码
2018/02/23 Python
对python numpy.array插入一行或一列的方法详解
2019/01/29 Python
Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解
2019/04/26 Python
Django之choices选项和富文本编辑器的使用详解
2020/04/01 Python
什么是Python变量作用域
2020/06/03 Python
Django Model层F,Q对象和聚合函数原理解析
2020/11/12 Python
python抢购软件/插件/脚本附完整源码
2021/03/04 Python
使用jTopo给Html5 Canva中绘制的元素添加鼠标事件
2014/05/15 HTML / CSS
俄罗斯和世界各地的酒店预订:Hotels.com俄罗斯
2016/08/19 全球购物
屈臣氏泰国官网:Watsons TH
2021/02/23 全球购物
社会学专业学生职业规划书
2014/02/07 职场文书
合作协议书怎么写
2014/04/18 职场文书
代理协议书范本
2014/04/22 职场文书
感恩父母主题班会
2015/08/12 职场文书
2016年员工年度考核评语
2015/12/02 职场文书