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 相关文章推荐
ADODB类使用
Nov 25 PHP
收集的PHP中与数组相关的函数
Mar 22 PHP
php strtotime 函数UNIX时间戳
Jan 14 PHP
给初学者的30条PHP最佳实践(荒野无灯)
Aug 02 PHP
PHP安全性漫谈
Jun 28 PHP
PHP列出MySQL中所有数据库的方法
Mar 12 PHP
PHP中读取文件的几个方法总结(推荐)
Jun 03 PHP
php中strlen和mb_strlen用法实例分析
Nov 12 PHP
PHP利用Socket获取网站的SSL证书与公钥
Jun 18 PHP
php连接mysql数据库最简单的实现方法
Sep 24 PHP
php文件上传原理与实现方法详解
Dec 20 PHP
PHP字符串与数组处理函数用法小结
Jan 07 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
zend framework多模块多布局配置
2011/02/26 PHP
php以fastCGI的方式运行时文件系统权限问题及解决方法
2015/05/11 PHP
计算世界完全对称日的js代码,粗糙版
2011/11/04 Javascript
防止浏览器记住用户名及密码的简单实用方法
2013/04/22 Javascript
javascript检测浏览器的缩放状态实现代码
2014/09/28 Javascript
浅析Javascript中“==”与“===”的区别
2014/12/23 Javascript
jQuery自定义添加&quot;$&quot;与解决&quot;$&quot;冲突的方法
2015/01/19 Javascript
浅谈轻量级js模板引擎simplite
2015/02/13 Javascript
Nodejs学习笔记之测试驱动
2015/04/16 NodeJs
JavaScript实现倒计时代码段Item1(非常实用)
2015/11/03 Javascript
JavaScript通过使用onerror设置默认图像显示代替alt
2016/03/01 Javascript
jQuery实现带延时功能的水平多级菜单效果【附demo源码下载】
2016/09/21 Javascript
微信小程序开发之animation循环动画实现的让云朵飘效果
2017/07/14 Javascript
vue+axios 前端实现的常用拦截的代码示例
2018/08/23 Javascript
Vue多环境代理配置方法思路详解
2019/06/21 Javascript
微信小程序顶部导航栏可滑动并选中放大
2019/12/05 Javascript
JS变量提升及函数提升实例解析
2020/09/03 Javascript
[05:46]DOTA2英雄梦之声_第18期_陈
2014/06/20 DOTA
[47:52]完美世界DOTA2联赛PWL S2 PXG vs InkIce 第二场 11.26
2020/11/30 DOTA
Python的Django框架中的URL配置与松耦合
2015/07/15 Python
设计模式中的原型模式在Python程序中的应用示例
2016/03/02 Python
Python数据结构与算法之使用队列解决小猫钓鱼问题
2017/12/14 Python
python 字典有序并写入json文件过程解析
2019/09/30 Python
Django框架创建项目的方法入门教程
2019/11/04 Python
tensorflow 动态获取 BatchSzie 的大小实例
2020/06/30 Python
Python configparser模块封装及构造配置文件
2020/08/07 Python
利用Python实现学生信息管理系统的完整实例
2020/12/30 Python
HTML5本地存储和本地数据库实例详解
2017/09/05 HTML / CSS
标记环介质访问控制协议
2016/03/27 面试题
网络工程师个人的自我评价范文
2013/10/01 职场文书
工会主席岗位责任制
2014/02/11 职场文书
企业安全生产承诺书
2014/05/22 职场文书
励志演讲稿3分钟
2014/08/21 职场文书
2015年元旦晚会活动总结(学生会)
2014/11/28 职场文书
CSS3鼠标悬浮过渡缩放效果
2021/04/17 HTML / CSS
分析JVM源码之Thread.interrupt系统级别线程打断
2021/06/29 Java/Android