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 xml实例 留言本
Mar 20 PHP
使用bcompiler对PHP文件进行加密的代码
Aug 29 PHP
比较时间段一与时间段二是否有交集的php函数
May 31 PHP
页面乱码问题的根源及其分析
Aug 09 PHP
php统计时间和内存使用情况示例分享
Mar 13 PHP
PHP获取浏览器信息类和客户端地理位置的2个方法
Apr 24 PHP
使用php的HTTP请求的库Requests实现美女图片墙
Feb 22 PHP
PHP检测链接是否存在的代码实例分享
May 06 PHP
php将服务端的文件读出来显示在web页面实例
Oct 31 PHP
PHP中Notice错误常见解决方法
Apr 28 PHP
PHP的PDO预处理语句与存储过程
Jan 27 PHP
PHP实现微信退款的方法示例
Mar 26 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入门学习的几个不错的实例代码
2008/07/13 PHP
php下实现在指定目录搜索指定类型文件的函数
2008/10/03 PHP
PHP __autoload函数(自动载入类文件)的使用方法
2012/02/04 PHP
PHP5常用函数列表(分享)
2013/06/07 PHP
分享50个提高PHP执行效率的技巧
2015/12/26 PHP
PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
2016/11/25 PHP
PHP简单实现图片格式转换(jpg转png,gif转png等)
2019/10/30 PHP
js 动态选中下拉框
2009/11/26 Javascript
javascript中的undefined 与 null 的区别  补充篇
2010/03/17 Javascript
基于jquery的图片懒加载js
2010/06/30 Javascript
Js日期选择器并自动加入到输入框中示例代码
2013/08/02 Javascript
node.js调用C++开发的模块实例
2015/07/03 Javascript
jQuery CSS3相结合实现时钟插件
2016/01/08 Javascript
vue,angular,avalon这三种MVVM框架优缺点
2016/04/27 Javascript
jQuery 限制输入字符串长度
2016/06/20 Javascript
js手动播放图片实现图片轮播效果
2016/09/17 Javascript
AngularJS实现表单验证功能详解
2017/10/12 Javascript
nodejs实现超简单生成二维码的方法
2018/03/17 NodeJs
vue中的适配px2rem示例代码
2018/11/19 Javascript
微信小程序实现的一键拨号功能示例
2019/04/24 Javascript
linux 下以二进制的方式安装 nodejs
2020/02/12 NodeJs
vue element el-transfer增加拖拽功能
2021/01/15 Vue.js
python里对list中的整数求平均并排序
2014/09/12 Python
python正则表达式之对号入座篇
2018/07/24 Python
详解Python绘图Turtle库
2019/10/12 Python
Python TCPServer 多线程多客户端通信的实现
2019/12/31 Python
Python常用断言函数实例汇总
2020/11/30 Python
使用CSS3制作倾斜导航条和毛玻璃效果
2017/09/12 HTML / CSS
CSS3 :default伪类选择器使用简介
2018/03/15 HTML / CSS
经济管理专业自荐信
2013/12/30 职场文书
2014信息技术专业毕业生自我评价
2014/01/17 职场文书
八一建军节感言
2014/02/28 职场文书
财政专业大学生职业生涯规划书
2014/09/17 职场文书
2015元旦晚会主持词(开场白+结束语)
2014/12/14 职场文书
Python包管理工具pip的15 个使用小技巧
2021/05/17 Python
Java面试题冲刺第十九天--数据库(4)
2021/08/07 Java/Android