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 变量定义和变量替换的方法
Jul 30 PHP
PHP获取用户的浏览器与操作系统信息的代码
Sep 04 PHP
使用php实现下载生成某链接快捷方式的解决方法
May 07 PHP
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
Jan 03 PHP
PHP实现微信公众平台音乐点播
Mar 20 PHP
smarty模板引擎中变量及变量修饰器用法实例
Jan 22 PHP
浅谈本地WAMP环境的搭建
May 13 PHP
thinkphp3.x中cookie方法的用法分析
May 19 PHP
PHP实现的贪婪算法实例
Oct 17 PHP
php 广告点击统计代码(php+mysql)
Feb 21 PHP
php成功操作redis cluster集群的实例教程
Jan 13 PHP
使用laravel指定日志文件记录任意日志
Oct 17 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下将XML转换为数组
2010/01/01 PHP
PHP中Session引起的脚本阻塞问题解决办法
2014/04/08 PHP
PHP扩展程序实现守护进程
2015/04/16 PHP
laravel 自定义常量的两种方案
2019/10/14 PHP
extjs 学习笔记(二) Ext.Element类
2009/10/13 Javascript
向大师们学习Javascript(视频与PPT)
2009/12/27 Javascript
jquery DOM操作 基于命令改变页面
2010/05/06 Javascript
jQuery 源码分析笔记(2) 变量列表
2011/05/28 Javascript
js当一个变量为函数时 应该注意的一点细节小结
2011/12/29 Javascript
JS分页控件 可用于无刷新分页
2013/07/23 Javascript
javascript 获取网页标题代码实例
2014/01/22 Javascript
jquery下拉select控件操作方法分享(jquery操作select)
2014/03/25 Javascript
jquery向上向下取整适合分页查询
2014/09/06 Javascript
浅谈jQuery中setInterval()方法
2015/07/07 Javascript
jquery点击缩略图切换视频播放特效代码分享
2015/09/15 Javascript
jQuery实现悬浮在右上角的网页客服效果代码
2015/10/24 Javascript
jQuery zclip插件实现跨浏览器复制功能
2015/11/02 Javascript
微信开发 js实现tabs选项卡效果
2016/10/28 Javascript
JavaScript实现全选取消效果
2017/12/14 Javascript
Vue.js 通过jQuery ajax获取数据实现更新后重新渲染页面的方法
2018/08/09 jQuery
JS 遍历 json 和 JQuery 遍历json操作完整示例
2019/11/11 jQuery
[05:13]TI4 中国战队 机场出征!!
2014/07/07 DOTA
Python基础教程之内置函数locals()和globals()用法分析
2018/03/16 Python
使用pytorch进行图像的顺序读取方法
2018/07/27 Python
python对视频画框标记后保存的方法
2018/12/07 Python
Python实现合并两个有序链表的方法示例
2019/01/31 Python
详解Python循环作用域与闭包
2019/03/21 Python
python使用Plotly绘图工具绘制柱状图
2019/04/01 Python
利用python绘制正态分布曲线
2021/01/04 Python
CSS3 实现footer 固定在底部(无论页面多高始终在底部)
2019/10/15 HTML / CSS
Canvas高级路径操作之拖拽对象的实现
2019/08/05 HTML / CSS
个人素质的自我评价分享
2013/12/16 职场文书
收银员岗位职责
2015/02/03 职场文书
详解Java实践之建造者模式
2021/06/18 Java/Android
JavaScript模拟实现网易云轮播效果
2022/04/04 Javascript
springboot创建的web项目整合Quartz框架的项目实践
2022/06/21 Java/Android