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自动判断字符集并转码的详解
Jun 26 PHP
thinkphp判断访客为手机端或PC端的方法
Nov 24 PHP
php中session与cookie的比较
Jan 27 PHP
PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库
Nov 25 PHP
php打乱数组二维数组多维数组的简单实例
Jun 17 PHP
适合PHP初学者阅读的4本经典书籍
Sep 23 PHP
php mysql_list_dbs()函数用法示例
Mar 29 PHP
PHP以json或xml格式返回请求数据的方法
May 31 PHP
PHP常用正则表达式精选(推荐)
May 28 PHP
PHP创建XML接口示例
Jul 04 PHP
PHP代码加密的方法总结
Mar 13 PHP
ThinkPHP6.0如何利用自定义验证规则规范的实现登陆
Dec 16 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自定义函数收代码
2010/08/01 PHP
PHP 数组遍历foreach语法结构及实例
2016/06/13 PHP
推荐11款jQuery开发的复选框和单选框美化插件
2011/08/02 Javascript
jQuery异步上传文件插件ajaxFileUpload详细介绍
2015/05/19 Javascript
jQuery中的ajax async同步和异步详解
2015/09/29 Javascript
基于MVC4+EasyUI的Web开发框架形成之旅之界面控件的使用
2015/12/16 Javascript
Vue2.0实现将页面中表格数据导出excel的实例
2017/08/09 Javascript
JS实现页面打印(整体、局部)
2017/08/18 Javascript
浅谈angular.js跨域post解决方案
2017/08/30 Javascript
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
[52:27]2018DOTA2亚洲邀请赛 3.31 小组赛B组 paiN vs Secret
2018/04/01 DOTA
[01:13:08]2018DOTA2亚洲邀请赛4.6 淘汰赛 mineski vs LGD 第二场
2018/04/10 DOTA
python进阶教程之模块(module)介绍
2014/08/30 Python
python3实现ftp服务功能(服务端 For Linux)
2017/03/24 Python
python获取交互式ssh shell的方法
2019/02/14 Python
python爬虫中多线程的使用详解
2019/09/23 Python
详解centos7+django+python3+mysql+阿里云部署项目全流程
2019/11/15 Python
Python3自动生成MySQL数据字典的markdown文本的实现
2020/05/07 Python
html5图片上传预览示例分享
2014/04/14 HTML / CSS
Artist Guitars新西兰:乐器在线商店
2017/09/17 全球购物
法国票务网站:Ticketmaster法国
2018/07/09 全球购物
印度在线杂货店:bigbasket
2018/08/23 全球购物
美体小铺奥地利官方网站:The Body Shop奥地利
2019/04/11 全球购物
final, finally, finalize的区别
2012/03/01 面试题
一套Delphi的笔试题一
2016/02/14 面试题
初中学生期末评语
2014/04/24 职场文书
公共艺术专业自荐信
2014/09/01 职场文书
国际贸易本科毕业生求职信
2014/09/26 职场文书
投资入股合作协议书
2014/10/28 职场文书
暑期社会实践个人总结
2015/03/06 职场文书
学生会副主席竞选稿
2015/11/19 职场文书
《我要的是葫芦》教学反思
2016/02/18 职场文书
一文搞清楚MySQL count(*)、count(1)、count(col)区别
2022/03/03 MySQL
Vue.js中v-for指令的用法介绍
2022/03/13 Vue.js
vue router 动态路由清除方式
2022/05/25 Vue.js