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 相关文章推荐
php5中date()得出的时间为什么不是当前时间的解决方法
Jun 30 PHP
php url路由入门实例
Apr 23 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十四)
Jun 26 PHP
关于URL最大长度限制的相关资料查证
Dec 23 PHP
php+mysqli使用预处理技术进行数据库查询的方法
Jan 28 PHP
PHP中创建和验证哈希的简单方法实探
Jul 06 PHP
PHP输入流php://input实例讲解
Dec 22 PHP
thinkPHP3.x常量整理(预定义常量/路径常量/系统常量)
May 20 PHP
值得分享的php+ajax实时聊天室
Jul 20 PHP
php中namespace及use用法分析
Dec 06 PHP
PHP使用CURL实现下载文件功能示例
Jun 03 PHP
php7 图形用户界面GUI 开发示例
Feb 22 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
phpmyadmin里面导入sql语句格式的大量数据的方法
2010/06/05 PHP
THINKPHP内容分页代码分享
2015/01/14 PHP
PHP SESSION的增加、删除、修改、查看操作
2015/03/20 PHP
CI框架源码解读之利用Hook.php文件完成功能扩展的方法
2016/05/18 PHP
dojo 之基础篇(二)之从服务器读取数据
2007/03/24 Javascript
基于jQuery的表格操作插件
2010/04/22 Javascript
解析js中获得父窗口链接getParent方法以及各种打开窗口的方法
2013/06/19 Javascript
JavaScrip实现PHP print_r的数功能(三种方法)
2013/11/12 Javascript
自己使用jquery写的一个无缝滚动的插件
2014/04/30 Javascript
javascript正则表达式基础知识入门
2015/04/20 Javascript
JS实现自动切换文字的导航效果代码
2015/08/27 Javascript
理解javascript模块化
2016/03/28 Javascript
jQuery侧边栏实现代码
2016/05/06 Javascript
关于验证码在IE中不刷新的快速解决方法
2016/09/23 Javascript
JS实现物体带缓冲的间歇运动效果示例
2016/12/22 Javascript
深入理解Webpack 中路径的配置
2017/06/17 Javascript
JS失效 提示HTML1114: (UNICODE 字节顺序标记)的代码页 utf-8 覆盖(META 标记)的冲突的代码页 utf-8
2017/06/23 Javascript
webpack中如何使用雪碧图的示例代码
2018/11/11 Javascript
[02:02]2018DOTA2亚洲邀请赛Mineski赛前采访
2018/04/04 DOTA
python实现封装得到virustotal扫描结果
2014/10/05 Python
Python Nose框架编写测试用例方法
2017/10/26 Python
Python实现基于二叉树存储结构的堆排序算法示例
2017/12/08 Python
Pycharm 操作Django Model的简单运用方法
2018/05/23 Python
对Python3.6 IDLE常用快捷键介绍
2018/07/16 Python
python后端接收前端回传的文件方法
2019/01/02 Python
Python GUI库PyQt5图形和特效样式QSS介绍
2020/02/25 Python
中专生毕业自我鉴定
2013/11/01 职场文书
运动会入场解说词300字
2014/01/25 职场文书
研究生考核个人自我鉴定
2014/03/27 职场文书
毕业设计说明书
2014/05/07 职场文书
2015年个人实习工作总结
2014/12/12 职场文书
教师求职信怎么写
2015/03/20 职场文书
2015少先队大队辅导员工作总结
2015/07/24 职场文书
《倍数和因数》教学反思
2016/02/23 职场文书
简单且有用的Python数据分析和机器学习代码
2021/07/02 Python
Spring Boot配合PageHelper优化大表查询数据分页
2022/04/20 Java/Android