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 相关文章推荐
IIS+PHP+MySQL+Zend配置 (视频教程)
Dec 13 PHP
PHP读取MySQL数据代码
Jun 05 PHP
php替换超长文本中的特殊字符的函数代码
May 22 PHP
服务器变量 $_SERVER 的深入解析
Jul 02 PHP
使用gd库实现php服务端图片裁剪和生成缩略图功能分享
Dec 25 PHP
ThinkPHP基本的增删查改操作实例教程
Aug 22 PHP
PHP命令行执行整合pathinfo模拟定时任务实例
Aug 12 PHP
Laravel使用memcached缓存对文章增删改查进行优化的方法
Oct 08 PHP
thinkphp修改配置进入默认首页的方法
Feb 07 PHP
PHP实现留言板功能的详细代码
Mar 25 PHP
PHP中常用的魔术方法
Apr 28 PHP
TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例
May 29 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简单封装了一些常用JS操作
2007/02/25 PHP
PHP封装的XML简单操作类完整实例
2017/11/13 PHP
php实现快速对二维数组某一列进行组装的方法小结
2019/12/04 PHP
仿jQuery的siblings效果的js代码
2011/08/09 Javascript
Jquery命名冲突解决的五种方案分享
2012/03/16 Javascript
avascript中的自执行匿名函数应用示例
2014/09/15 Javascript
JavaScript设计模式之代理模式介绍
2014/12/28 Javascript
jQuery实现给页面换肤的方法
2015/05/30 Javascript
javascript实现手机震动API代码
2015/08/05 Javascript
jQuery版本升级踩坑大全
2016/01/12 Javascript
Bootstrap教程JS插件滚动监听学习笔记分享
2016/05/18 Javascript
基于Echarts 3.19 制作常用的图形(非静态)
2016/05/19 Javascript
JS 根据子网掩码,网关计算出所有IP地址范围示例
2020/04/23 Javascript
bootstrap选项卡使用方法解析
2017/01/11 Javascript
jQuery基于正则表达式的表单验证功能示例
2017/01/21 Javascript
Angularjs 动态添加指令并绑定事件的方法
2017/04/13 Javascript
Vue如何实现组件的源码解析
2017/06/08 Javascript
JavaScript监听手机物理返回键的两种解决方法
2017/08/14 Javascript
Angularjs实现数组随机排序的方法
2018/10/02 Javascript
Layui实现主窗口和Iframe层参数传递
2019/11/14 Javascript
Android应用开发中Action bar编写的入门教程
2016/02/26 Python
Ruby使用eventmachine为HTTP服务器添加文件下载功能
2016/04/20 Python
python对象及面向对象技术详解
2016/07/19 Python
简单谈谈Python中的反转字符串问题
2016/10/24 Python
Python实现堡垒机模式下远程命令执行操作示例
2019/05/09 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
2019/06/24 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
2020/02/17 Python
Tomcat中怎么使用log4j输出所有的log
2016/07/07 面试题
九年级数学教学反思
2014/02/02 职场文书
中学生清明节演讲稿
2015/03/18 职场文书
公司财务经理岗位职责
2015/04/08 职场文书
信仰纪录片观后感
2015/06/08 职场文书
2016年大学生就业指导课心得体会
2015/10/09 职场文书
数据结构课程设计心得体会
2016/01/15 职场文书
导游词之西递宏村
2019/12/10 职场文书
Pytorch可视化的几种实现方法
2021/06/10 Python