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中实现进程间通讯
Oct 09 PHP
php将会员数据导入到ucenter的代码
Jul 18 PHP
PHP 简易输出CSV表格文件的方法详解
Jun 20 PHP
php后台如何避免用户直接进入方法实例
Oct 15 PHP
ThinkPHP验证码使用简明教程
Mar 05 PHP
php调用google接口生成二维码示例
Apr 28 PHP
ThinkPHP模板自定义标签使用方法
Jun 26 PHP
php中switch与ifelse的效率区别及适用情况分析
Feb 12 PHP
完美利用Yii2微信后台开发的系列总结
Jul 18 PHP
PHP调用API接口实现天气查询功能的示例
Sep 21 PHP
PHP实现的数据对象映射模式详解
Mar 20 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
Feb 27 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数据类型之整数类型、浮点数的介绍
2013/04/28 PHP
PHP中$_FILES的使用方法及注意事项说明
2014/02/14 PHP
PHP+shell脚本操作Memcached和Apache Status的实例分享
2016/03/11 PHP
关于PHP虚拟主机概念及如何选择稳定的PHP虚拟主机
2018/11/20 PHP
不安全的常用的js写法
2009/09/15 Javascript
Ajax搜索结果页面下方的分页按钮的生成
2012/04/05 Javascript
上传图片预览JS脚本 Input file图片预览的实现示例
2014/10/23 Javascript
jquery中animate的stop()方法作用实例分析
2015/01/30 Javascript
JavaScript实现图片滑动切换的代码示例分享
2016/03/06 Javascript
jQuery事件处理的特征(事件命名机制)
2016/08/23 Javascript
BootStrap Tooltip插件源码解析
2016/12/27 Javascript
JS实现动态修改table及合并单元格的方法示例
2017/02/20 Javascript
vue.js指令v-model使用方法
2017/03/20 Javascript
PHP 实现一种多文件上传的方法
2017/09/20 Javascript
使用cookie绕过验证码登录的实现代码
2017/10/12 Javascript
Node.js的Koa实现JWT用户认证方法
2018/05/05 Javascript
JS实现斐波那契数列的五种方式(小结)
2020/09/09 Javascript
design vue 表格开启列排序的操作
2020/10/28 Javascript
python获得图片base64编码示例
2014/01/16 Python
python如何通过protobuf实现rpc
2016/03/06 Python
Python 实现文件的全备份和差异备份详解
2016/12/27 Python
python中的set实现不重复的排序原理
2018/01/24 Python
python正则实现提取电话功能
2018/02/24 Python
python利用Opencv实现人脸识别功能
2019/04/25 Python
python实践项目之监控当前联网状态详情
2019/05/23 Python
python 发送json数据操作实例分析
2019/10/15 Python
HTML5给汉字加拼音收起展开组件的实现代码
2020/04/08 HTML / CSS
AmazeUI导航的示例代码
2020/08/14 HTML / CSS
家用个人磨皮机:Trophy Skin
2017/03/30 全球购物
来自世界各地的饮料:Flavourly
2019/05/06 全球购物
英国马莎百货印度官网:Marks & Spencer印度
2020/10/08 全球购物
法制报告会主持词
2014/04/02 职场文书
新员工入职欢迎词
2015/01/23 职场文书
2015年医德医风工作总结
2015/04/02 职场文书
小王子读书笔记
2015/06/29 职场文书
SQL注入篇学习之盲注/宽字节注入
2022/03/03 MySQL