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 和 MySQL 基础教程(一)
Oct 09 PHP
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
May 07 PHP
php下载文件源代码(强制任意文件格式下载)
May 09 PHP
PHP获取windows登录用户名的方法
Jun 24 PHP
php用ini_get获取php.ini里变量值的方法
Mar 04 PHP
浅谈使用PHP开发微信支付的流程
Oct 04 PHP
详解PHP+AJAX无刷新分页实现方法
Nov 03 PHP
PHP爬虫之百万级别知乎用户数据爬取与分析
Jan 22 PHP
PHP数组操作实例分析【添加,删除,计算,反转,排序,查找等】
Dec 24 PHP
php利用fsockopen GET/POST提交表单及上传文件
May 22 PHP
Ajax中的JSON格式与php传输过程全面解析
Nov 14 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使用feof()函数读文件的方法
2014/11/07 PHP
PHP中使用break跳出多重循环代码实例
2015/01/21 PHP
php中stdClass的用法分析
2015/02/27 PHP
php判断是否连接上网络的方法实例详解
2016/12/14 PHP
简单的JS多重继承示例
2008/03/13 Javascript
javascript call和apply方法
2008/11/24 Javascript
JS实现超简单的鼠标拖动效果
2015/11/02 Javascript
AngularJS中实现显示或隐藏动画效果的方式总结
2015/12/31 Javascript
jQuery Mobile和HTML5开发App推广注册页
2016/11/07 Javascript
JS 插件dropload下拉刷新、上拉加载使用小结
2017/04/13 Javascript
ES6中Math对象新增的方法实例详解
2017/04/25 Javascript
用 Vue.js 递归组件实现可折叠的树形菜单(demo)
2017/12/25 Javascript
jquery实现点击a链接,跳转之后,该a链接处显示背景色的方法
2018/01/18 jQuery
ionic2中使用自动生成器的方法
2018/03/04 Javascript
jQuery基于闭包实现的显示与隐藏div功能示例
2018/06/09 jQuery
JavaScript实现单图片上传并预览功能
2019/09/30 Javascript
解决vue init webpack 下载依赖卡住不动的问题
2020/11/09 Javascript
Python 常用的安装Module方式汇总
2017/05/06 Python
查看django执行的sql语句及消耗时间的两种方法
2018/05/29 Python
Python使用APScheduler实现定时任务过程解析
2019/09/11 Python
jenkins配置python脚本定时任务过程图解
2019/10/29 Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
2020/02/25 Python
计算Python Numpy向量之间的欧氏距离实例
2020/05/22 Python
基于python实现百度语音识别和图灵对话
2020/11/02 Python
localstorage和sessionstorage使用记录(推荐)
2017/05/23 HTML / CSS
台湾演唱会订票网站:StubHub台湾
2019/06/11 全球购物
美国浴缸、水槽和水龙头购物网站:Vintage Tub & Bath
2019/11/05 全球购物
Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
2015/01/27 面试题
安全横幅标语
2014/06/09 职场文书
离职报告范文
2014/11/04 职场文书
房地产销售主管岗位职责
2015/02/13 职场文书
教师调动申请报告
2015/05/18 职场文书
健康证明
2015/06/19 职场文书
教师听课学习心得体会
2016/01/15 职场文书
聊一聊python常用的编程模块
2021/05/14 Python
html+css实现滚动到元素位置显示加载动画效果
2021/08/02 HTML / CSS