php blowfish加密解密算法

 更新时间:2016年07月02日 13:47:30   投稿:lijiao  
这篇文章主要为大家详细介绍了php blowfish加密解密算法的相关资料,感兴趣的朋友可以参考一下

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实现数据库的增删改查

    本文给大家介绍的是PHP连接数据库以及实现数据库的增删改查功能的方法及示例代码,希望对大家学习php能够有所帮助
    2017-02-02
  • goto语法在PHP中的使用教程

    goto语法在PHP中的使用教程

    这篇文章主要给大家介绍了关于goto语法在PHP中的使用教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Laravel 5.3 学习笔记之 安装

    Laravel 5.3 学习笔记之 安装

    本文是Laravel 5.3 学习笔记系列文章的第一篇,我们来加单讲解下Laravel 5.3 的安装,有需要的小伙伴可以参考下
    2016-08-08
  • PHP面向对象精要总结

    PHP面向对象精要总结

    这篇文章主要介绍了PHP面向对象精要,较为详细的介绍了PHP面向对象程序设计中的各种概念与常见技巧,非常具有实用价值,需要的朋友可以参考下
    2014-11-11
  • php使用iconv中文截断问题的解决方法

    php使用iconv中文截断问题的解决方法

    这篇文章主要介绍了php使用iconv中文截断问题的解决方法,以实例形式较为详细的分析了出现中文截断问题的原因与具体解决方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • php 中奖概率算法实现代码

    php 中奖概率算法实现代码

    这篇文章主要介绍了php 中奖概率算法,需要的朋友可以参考下
    2017-01-01
  • PHP数字格式化

    PHP数字格式化

    PHP数字格式化...
    2006-12-12
  • php实现的中文分词类完整实例

    php实现的中文分词类完整实例

    这篇文章主要介绍了php实现的中文分词类,结合完整实例形式分析了php基于字符串的遍历、转换、运算等技巧实现中文分词功能的具体方法,需要的朋友可以参考下
    2017-02-02
  • PHP基于imap获取邮件实例

    PHP基于imap获取邮件实例

    这篇文章主要介绍了PHP基于imap获取邮件的方法,实例形式详细讲述了imap获取邮件的具体步骤,并详细讲述了Windows下和Linux下安装imap的方法,需要的朋友可以参考下
    2014-11-11
  • php轻松实现中英文混排字符串截取

    php轻松实现中英文混排字符串截取

    提到中英文混排计数、截取,大家首先想到的是自己写个截取函数,这在之前的文章中我也提到过,今天我给大家分享的是php的mb扩展,教你如何使用原生态的php函数轻松处理字符串。
    2014-05-05

最新评论