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中使用Oracle数据库(6)
Oct 09 PHP
php读取数据库信息的几种方法
May 24 PHP
Notice: Undefined index: page in E:\PHP\test.php on line 14
Nov 02 PHP
php中用foreach来操作数组的代码
Jul 17 PHP
PHP递归算法的详细示例分析
Feb 19 PHP
PHP实现根据浏览器跳转不同语言页面代码
Aug 02 PHP
php实现的双向队列类实例
Sep 24 PHP
PHP实现文件下载详解
Nov 27 PHP
php实现的简单中文验证码功能示例
Jan 03 PHP
php 删除一维数组中某一个值元素的操作方法
Feb 01 PHP
不常用但很实用的PHP预定义变量分析
Jun 25 PHP
Laravel定时任务的每秒执行代码
Oct 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
收音机术语解释
2021/03/01 无线电
php变量作用域的深入解析
2013/06/03 PHP
PHP微信模板消息操作示例
2017/06/29 PHP
用Div仿showModalDialog模式菜单的效果的代码
2007/03/05 Javascript
使用jQuery判断浏览器滚动条位置的方法
2016/05/30 Javascript
浅谈JavaScript变量的自动转换和语句
2016/06/12 Javascript
Bootstrap基本插件学习笔记之Tooltip提示工具(18)
2016/12/08 Javascript
微信小程序 石头剪刀布实例代码
2017/01/04 Javascript
详解react如何在组件中获取路由参数
2017/06/15 Javascript
总结JavaScript在IE9之前版本中内存泄露问题
2018/04/28 Javascript
[01:06:39]DOTA2上海特级锦标赛主赛事日 - 1 胜者组第一轮#1Liquid VS Alliance第三局
2016/03/02 DOTA
一个检测OpenSSL心脏出血漏洞的Python脚本分享
2014/04/10 Python
python在windows命令行下输出彩色文字的方法
2015/03/19 Python
Python实现计算文件夹下.h和.cpp文件的总行数
2015/04/23 Python
Python爬虫实例_利用百度地图API批量获取城市所有的POI点
2018/01/10 Python
python如何统计序列中元素
2020/07/31 Python
详谈Pandas中iloc和loc以及ix的区别
2018/06/08 Python
Python设计模式之适配器模式原理与用法详解
2019/01/15 Python
Keras实现DenseNet结构操作
2020/07/06 Python
海淘零差价,宝贝全球购: 宝贝格子
2016/08/24 全球购物
GEOX鞋美国官方网站:意大利会呼吸的鞋
2017/07/12 全球购物
物理专业本科生自荐信
2014/01/30 职场文书
表彰会主持词
2014/03/26 职场文书
村容村貌整治方案
2014/05/21 职场文书
大学生见习期满自我鉴定
2014/09/13 职场文书
医院见习报告范文
2014/11/03 职场文书
蓬莱阁导游词
2015/02/04 职场文书
党员干部廉洁自律承诺书
2015/04/28 职场文书
新年祝酒词大全
2015/08/11 职场文书
2015年物业公司保洁工作总结
2015/10/22 职场文书
分布式锁为什么要选择Zookeeper而不是Redis?看完这篇你就明白了
2021/05/21 Redis
微软Win11什么功能最惊艳? Windows11新功能特性汇总
2021/11/21 数码科技
Python万能模板案例之matplotlib绘制直方图的基本配置
2022/04/13 Python
MySQL数据库Innodb 引擎实现mvcc锁
2022/05/06 MySQL
MySQL生成千万测试数据以及遇到的问题
2022/08/05 MySQL
Spring Boot实现文件上传下载
2022/08/14 Java/Android