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安全配置详细说明
Sep 26 PHP
php实现扫描二维码根据浏览器类型访问不同下载地址
Oct 15 PHP
php使用ffmpeg向视频中添加文字字幕的实现方法
May 23 PHP
浅谈PHP eval()函数定义和用法
Jun 21 PHP
zen cart实现订单中增加paypal中预留电话的方法
Jul 12 PHP
PHP加密技术的简单实现
Sep 04 PHP
php json_encode与json_decode详解及实例
Dec 13 PHP
php处理多图上传压缩代码功能
Jun 13 PHP
laravel 解决强制跳转 https的问题
Oct 22 PHP
PHP设计模式之组合模式定义与应用示例
Feb 01 PHP
PHP开发api接口安全验证操作实例详解
Mar 26 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
DC游戏Steam周三特惠 《蝙蝠侠》阿卡姆系列平史低
2020/04/09 欧美动漫
SONY ICF-SW55的电路分析
2021/03/02 无线电
数据库的日期格式转换
2006/10/09 PHP
php下intval()和(int)转换使用与区别
2008/07/18 PHP
php下连接mssql2005的代码
2011/01/17 PHP
file模式访问网页时iframe高度自适应解决方案
2013/01/16 Javascript
现代 JavaScript 开发编程风格Idiomatic.js指南中文版
2014/05/28 Javascript
利用a标签自动解析URL分析网址实例
2014/10/20 Javascript
JavaScript插件化开发教程(六)
2015/02/01 Javascript
js针对ip地址、子网掩码、网关的逻辑性判断
2016/01/06 Javascript
require、backbone等重构手机图片查看器
2016/11/17 Javascript
javascript中的深复制详解及实例分析
2016/12/29 Javascript
微信小程序 出现错误:{&quot;baseresponse&quot;:{&quot;errcode&quot;:-80002,&quot;errmsg&quot;:&quot;&quot;}}解决办法
2017/02/23 Javascript
JS实现标签页切换效果
2017/05/04 Javascript
微信小程序request出现400的问题解决办法
2017/05/23 Javascript
AngularJS中ng-class用法实例分析
2017/07/06 Javascript
Vue.js分页组件实现:diVuePagination的使用详解
2018/01/10 Javascript
JS中常用的消息框总结
2018/02/24 Javascript
关于微信小程序登录的那些事
2019/01/08 Javascript
解决Vue打包后访问图片/图标不显示的问题
2019/07/25 Javascript
vue router 传参获取不到的解决方式
2019/11/13 Javascript
python安装Scrapy图文教程
2017/08/14 Python
分享一个简单的python读写文件脚本
2017/11/25 Python
浅谈python中requests模块导入的问题
2018/05/18 Python
python之Flask实现简单登录功能的示例代码
2018/12/24 Python
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2019/12/02 Python
tensorflow的计算图总结
2020/01/12 Python
详解用python -m http.server搭一个简易的本地局域网
2020/09/24 Python
英国Amara家居法国网站:家居装饰,现代装饰和豪华礼品
2016/12/15 全球购物
乌克兰设计师和品牌的服装:Love&Live
2020/04/14 全球购物
青年教师典范事迹材料
2014/01/31 职场文书
酒店总经理助理岗位职责
2014/02/01 职场文书
吴仁宝观后感
2015/06/09 职场文书
2015年汽车销售员工作总结
2015/07/24 职场文书
25句企业管理语录:助你迅速打开思路,句句经典!
2020/01/14 职场文书
详解gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)
2021/11/27 Vue.js