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 相关文章推荐
第1次亲密接触PHP5(1)
Oct 09 PHP
PHP完整的日历类(CLASS)
Nov 27 PHP
PHP中通过ADO调用Access数据库的方法测试不通过
Dec 31 PHP
PHP 替换模板变量实现步骤
Aug 24 PHP
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
Jun 17 PHP
百度站点地图(百度sitemap)生成方法分享
Jan 09 PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
Jan 22 PHP
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
Jun 12 PHP
thinkPHP中create方法与令牌验证实例浅析
Dec 08 PHP
PHP常用设计模式之委托设计模式
Feb 13 PHP
php实现简单加入购物车功能
Mar 07 PHP
PHP截取发动短信内容的方法
Jul 04 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 csv操作类代码
2009/12/14 PHP
TMDPHP 模板引擎使用教程
2012/03/13 PHP
PHP简单实现“相关文章推荐”功能的方法
2014/07/19 PHP
php中stdClass的用法分析
2015/02/27 PHP
Laravel 5框架学习之Eloquent 关系
2015/04/09 PHP
摘自织梦CMS中的图片处理类
2015/08/08 PHP
IE中jscript/javascript的条件编译
2006/09/07 Javascript
js获取url参数的使用扩展实例
2007/12/29 Javascript
在IE浏览器中resize事件执行多次的解决方法
2011/07/12 Javascript
Jquery 自定义动画概述及示例
2013/03/29 Javascript
框架页面高度自动刷新的Javascript脚本
2013/11/01 Javascript
自己封装的一个简单的倒计时功能实例
2016/11/23 Javascript
vue中进入详情页记住滚动位置的方法(keep-alive)
2018/09/21 Javascript
vue和H5 draggable实现拖拽并替换效果
2020/07/29 Javascript
Node在Controller层进行数据校验的过程详解
2020/08/28 Javascript
Python自动重试HTTP连接装饰器
2015/04/28 Python
Python通过正则表达式选取callback的方法
2015/07/18 Python
python对json的相关操作实例详解
2017/01/04 Python
python使用pyqt写带界面工具的示例代码
2017/10/23 Python
Python实现聊天机器人的示例代码
2018/07/09 Python
详解Python中的各种转义符\n\r\t
2019/07/10 Python
获取Pytorch中间某一层权重或者特征的例子
2019/08/17 Python
python模拟点击网页按钮实现方法
2020/02/25 Python
python3注册全局热键的实现
2020/03/22 Python
pytorch 中的重要模块化接口nn.Module的使用
2020/04/02 Python
介绍一下grep命令的使用
2012/06/28 面试题
秋季运动会表扬稿
2014/01/16 职场文书
探亲邀请信范文
2014/01/30 职场文书
《明天,我们毕业》教学反思
2014/04/24 职场文书
动漫设计与制作专业推荐信
2014/07/07 职场文书
个性与发展自我评价
2015/03/06 职场文书
广播体操比赛主持词
2015/06/29 职场文书
趣味运动会通讯稿
2015/07/18 职场文书
详解Java分布式事务的 6 种解决方案
2021/06/26 Java/Android
详解Go语言Slice作为函数参数的使用
2021/07/02 Golang
python周期任务调度工具Schedule使用详解
2021/11/23 Python