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网页后退不再出现过期
Mar 08 PHP
强制PHP命令行脚本单进程运行的方法
Apr 15 PHP
php中文字符串截取方法实例总结
Sep 30 PHP
Yii框架中memcache用法实例
Dec 03 PHP
Twig模板引擎用法入门教程
Jan 20 PHP
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
May 19 PHP
zend framework重定向方法小结
May 28 PHP
php实时倒计时功能实现方法详解
Feb 27 PHP
PHP编程实现csv文件导入mysql数据库的方法
Apr 29 PHP
Laravel中日期时间处理包Carbon的简单使用
Sep 21 PHP
ThinkPHP 3.2.3实现加减乘除图片验证码
Dec 05 PHP
Laravel validate error处理,ajax,json示例
Oct 25 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
php使用parse_url和parse_str解析URL
2015/02/22 PHP
PHP实现转盘抽奖算法分享
2020/04/15 PHP
PHP使Laravel为JSON REST API返回自定义错误的问题
2018/10/16 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
2018/10/18 PHP
php写入mysql中文乱码的实例解决方法
2019/09/17 PHP
Javascript 对象的解释
2008/11/24 Javascript
js textarea自动增高并隐藏滚动条
2009/12/16 Javascript
Extjs407 getValue()和getRawValue()区别介绍
2013/05/21 Javascript
jquery快捷动态绑定键盘事件的操作函数代码
2013/10/17 Javascript
JQuery插件iScroll实现下拉刷新,滚动翻页特效
2014/06/22 Javascript
教你使用javascript简单写一个页面模板引擎
2015/05/05 Javascript
TypeScript 学习笔记之基本类型
2015/06/19 Javascript
举例详解JavaScript中Promise的使用
2015/06/24 Javascript
javascript编写贪吃蛇游戏
2015/07/07 Javascript
jQuery基础知识点总结(DOM操作)
2016/06/01 Javascript
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
2016/07/11 Javascript
微信小程序 弹窗自定义实例代码
2017/03/08 Javascript
微信小程序自定义模态对话框实例详解
2017/08/16 Javascript
Vue 获取数组键名的方法
2018/06/21 Javascript
详解js访问对象的属性和方法
2018/10/25 Javascript
jQuery事件委托代码实践详解
2019/06/21 jQuery
Python多线程编程(四):使用Lock互斥锁
2015/04/05 Python
Python virtualenv虚拟环境实现过程解析
2020/04/18 Python
详解Anaconda 的安装教程
2020/09/23 Python
Canvas制作的下雨动画的示例
2018/03/06 HTML / CSS
一套SQL笔试题
2016/08/14 面试题
介绍一下linux的文件系统
2015/10/06 面试题
介绍一下如何优化MySql
2016/12/20 面试题
市场营销专业个人求职信范文
2013/12/14 职场文书
关于赌博的检讨书
2014/01/08 职场文书
2014年节能减排工作总结
2014/12/06 职场文书
教师考核表个人总结
2015/02/12 职场文书
产品质量保证书范本
2015/02/27 职场文书
2015年求职自荐信范文
2015/03/04 职场文书
2015年党员创先争优公开承诺书
2015/04/27 职场文书
Java Spring Boot 正确读取配置文件中的属性的值
2022/04/20 Java/Android