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 Image Resize图片大小调整的函数代码
Jan 17 PHP
php cookies中删除的一般赋值方法
May 07 PHP
修改php.ini不生效问题解决方法(上传大于8M的文件)
Jun 14 PHP
多个PHP中文字符串截取函数
Nov 12 PHP
实例讲解PHP面向对象之多态
Aug 20 PHP
php更新mysql后获取改变行数的方法
Dec 25 PHP
CentOS下PHP安装Oracle扩展
Feb 15 PHP
PHP抓取及分析网页的方法详解
Apr 26 PHP
php mysql 封装类实例代码
Sep 18 PHP
thinkphp5框架API token身份验证功能示例
May 21 PHP
Yii框架的路由配置方法分析
Sep 09 PHP
PHP之header函数详解
Mar 02 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
ThinkPHP通过AJAX返回JSON的两种实现方法
2014/12/18 PHP
AES加解密在php接口请求过程中的应用示例
2016/10/26 PHP
php安装php_rar扩展实现rar文件读取和解压的方法
2016/11/17 PHP
php简单计算年龄的方法(周岁与虚岁)
2016/12/06 PHP
Laravel 已登陆用户再次查看登陆页面的自动跳转设置方法
2019/09/30 PHP
网上应用的一个不错common.js脚本
2007/08/08 Javascript
js模仿hover的具体实现代码
2013/12/30 Javascript
防止登录页面出现在frame中js代码
2014/07/22 Javascript
JavaScript操作DOM元素的childNodes和children区别
2015/04/01 Javascript
浅析JavaScript 调试方法和技巧
2015/10/22 Javascript
jQuery+PHP+MySQL二级联动下拉菜单实例讲解
2015/10/27 Javascript
javascript实现简单的ajax封装示例
2016/12/28 Javascript
angularjs ui-router中路由的二级嵌套
2017/03/10 Javascript
微信小程序 支付功能(前端)的实现
2017/05/24 Javascript
vue-cli脚手架config目录下index.js配置文件的方法
2018/03/13 Javascript
官方推荐react-navigation的具体使用详解
2018/05/08 Javascript
Vue组件中prop属性使用说明实例代码详解
2018/05/31 Javascript
vue项目打包部署到服务器的方法示例
2018/08/27 Javascript
PWA介绍及快速上手搭建一个PWA应用的方法
2019/01/27 Javascript
JavaScript中的回调函数实例讲解
2019/01/27 Javascript
从零搭一个自用的前端脚手架的方法步骤
2019/09/23 Javascript
举例介绍Python中的25个隐藏特性
2015/03/30 Python
python实现简易云音乐播放器
2018/01/04 Python
python求最大值,不使用内置函数的实现方法
2019/07/09 Python
在PyTorch中使用标签平滑正则化的问题
2020/04/03 Python
pycharm永久激活超详细教程
2020/10/29 Python
CSS3的常见transformation图形变化用法小结
2016/05/13 HTML / CSS
HTML5 3D衣服摇摆动画特效
2016/03/17 HTML / CSS
mysql有关权限的表都有哪几个
2015/04/22 面试题
追悼会上的答谢词
2014/01/10 职场文书
公司管理建议书范文
2014/03/12 职场文书
2014市府办领导班子“四风问题”对照检查材料思想汇报
2014/09/24 职场文书
2015年前台个人工作总结
2015/04/03 职场文书
医院中层管理人员培训心得体会
2016/01/11 职场文书
MySQL8.0无法启动3534的解决方法
2021/06/03 MySQL
一篇文章搞懂python混乱的切换操作与优雅的推导式
2021/08/23 Python