欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php blowfish加密解密算法

 更新時間:2016年07月02日 13:47:30   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了php blowfish加密解密算法的相關(guān)資料,感興趣的朋友可以參考一下

PHP Blowfish 算法的加密解密,供大家參考,具體內(nèi)容如下

<?php
 
/**
 * php blowfish 算法
 * Class blowfish
 */
class blowfish{
 /**
  * blowfish + cbc模式 + pkcs5補碼 加密
  * @param string $str 需要加密的數(shù)據(jù)
  * @return string 加密后base64加密的數(shù)據(jù)
  */
 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 加密的數(shù)據(jù)
  * @return string 解密的數(shù)據(jù)
  */
 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 
 
?>

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)php程序設(shè)計有所幫助。

相關(guān)文章

  • php 用checkbox一次性刪除多條記錄的方法

    php 用checkbox一次性刪除多條記錄的方法

    一次性刪除多條記錄,對于比較多的信息,如果沒有批量刪除功能是非常麻煩的。這樣就方便多了。
    2010-02-02
  • 詳解php的socket通信

    詳解php的socket通信

    這篇文章通過案例演示詳細(xì)介紹了php的socket通信,對每一段代碼進(jìn)行了分析,每一步驟都配有詳細(xì)說明,感興趣的的朋友可以參考下
    2015-08-08
  • PHP計算日期相差天數(shù)實例分析

    PHP計算日期相差天數(shù)實例分析

    這篇文章主要介紹了PHP計算日期相差天數(shù)的方法,結(jié)合實例形式分析了PHP日期與時間操作及換算的相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • php中文語義分析實現(xiàn)方法示例

    php中文語義分析實現(xiàn)方法示例

    這篇文章主要介紹了php中文語義分析實現(xiàn)方法,結(jié)合實例形式分析了PHP基于BosonNLP擴(kuò)展實現(xiàn)中文語義分析的具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-09-09
  • PHP中header和session_start前不能有輸出原因分析

    PHP中header和session_start前不能有輸出原因分析

    在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來為您詳細(xì)解答
    2013-01-01
  • php5與php7的區(qū)別點總結(jié)

    php5與php7的區(qū)別點總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于php5與php7的區(qū)別是什么的相關(guān)知識點內(nèi)容,有需要的朋友們學(xué)習(xí)下。
    2019-10-10
  • 淺談PHP中單引號和雙引號到底有啥區(qū)別呢?

    淺談PHP中單引號和雙引號到底有啥區(qū)別呢?

    本文向我們簡單介紹了php中單雙引號的區(qū)別,以及對比了下javascript中單引號和雙引號的區(qū)別,方便我們更好的理解,非常實用,推薦給大家
    2015-03-03
  • PHP+MySQL實現(xiàn)無極限分類欄目的方法

    PHP+MySQL實現(xiàn)無極限分類欄目的方法

    這篇文章主要介紹了PHP+MySQL實現(xiàn)無極限分類欄目的方法,涉及php操作數(shù)據(jù)庫查詢及結(jié)果集遞歸遍歷的技巧,需要的朋友可以參考下
    2015-12-12
  • 安裝apache2.2.22配置php5.4(具體操作步驟)

    安裝apache2.2.22配置php5.4(具體操作步驟)

    本篇文章是對安裝apache2.2.22配置php5.4的具體操作步驟進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • kindeditor 加入七牛云上傳的實例講解

    kindeditor 加入七牛云上傳的實例講解

    下面小編就為大家?guī)硪黄猭indeditor 加入七牛云上傳的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11

最新評論