婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > php實現的rc4加密解密類定義與用法示例

php實現的rc4加密解密類定義與用法示例

熱門標簽:代理打電話機器人 太原400電話申請流程 企業400電話辦理多少費用 桂陽公司如何做地圖標注 電信外呼系統多少錢一個月 神龍斗士電話機器人 宿州正規外呼系統軟件 合肥企業外呼系統線路 萍鄉商鋪地圖標注

本文實例講述了php實現的rc4加密解密類。分享給大家供大家參考,具體如下:

class.rc4crypt.php文件:

?php
/* 
 * By julying.com
 */
define('CRYPT_RC4_MODE_INTERNAL', 1);
define('CRYPT_RC4_MODE_MCRYPT', 2);
define('CRYPT_RC4_ENCRYPT', 0);
define('CRYPT_RC4_DECRYPT', 1);
class Crypt_RC4 {
 /**
  * The Key
  *
  * @see Crypt_RC4::setKey()
  * @var String
  * @access private
  */
 var $key = "\0";
 /**
  * The Key Stream for encryption
  *
  * If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
  *
  * @see Crypt_RC4::setKey()
  * @var Array
  * @access private
  */
 var $encryptStream = false;
 /**
  * The Key Stream for decryption
  *
  * If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
  *
  * @see Crypt_RC4::setKey()
  * @var Array
  * @access private
  */
 var $decryptStream = false;
 /**
  * The $i and $j indexes for encryption
  *
  * @see Crypt_RC4::_crypt()
  * @var Integer
  * @access private
  */
 var $encryptIndex = 0;
 /**
  * The $i and $j indexes for decryption
  *
  * @see Crypt_RC4::_crypt()
  * @var Integer
  * @access private
  */
 var $decryptIndex = 0;
 /**
  * MCrypt parameters
  *
  * @see Crypt_RC4::setMCrypt()
  * @var Array
  * @access private
  */
 var $mcrypt = array('', '');
 /**
  * The Encryption Algorithm
  *
  * Only used if CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT. Only possible values are MCRYPT_RC4 or MCRYPT_ARCFOUR.
  *
  * @see Crypt_RC4::Crypt_RC4()
  * @var Integer
  * @access private
  */
 var $mode;
 /**
  * Default Constructor.
  *
  * Determines whether or not the mcrypt extension should be used.
  *
  * @param optional Integer $mode
  * @return Crypt_RC4
  * @access public
  */
 var $continuousBuffer ;
 function Crypt_RC4()
 {
  if ( !defined('CRYPT_RC4_MODE') ) {
   switch (true) {
    case extension_loaded('mcrypt')  (defined('MCRYPT_ARCFOUR') || defined('MCRYPT_RC4')):
     // i'd check to see if rc4 was supported, by doing in_array('arcfour', mcrypt_list_algorithms('')),
     // but since that can be changed after the object has been created, there doesn't seem to be
     // a lot of point...
     define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_MCRYPT);
     break;
    default:
     define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_INTERNAL);
   }
  }
  switch ( CRYPT_RC4_MODE ) {
   case CRYPT_RC4_MODE_MCRYPT:
    switch (true) {
     case defined('MCRYPT_ARCFOUR'):
      $this->mode = MCRYPT_ARCFOUR;
      break;
     case defined('MCRYPT_RC4');
      $this->mode = MCRYPT_RC4;
    }
  }
 }
 /**
  * Sets the key.
  *
  * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
  * be used. If no key is explicitly set, it'll be assumed to be a single null byte.
  *
  * @access public
  * @param String $key
  */
 function setKey($key)
 {
  $this->key = $key;
  if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
   return;
  }
  $keyLength = strlen($key);
  $keyStream = array();
  for ($i = 0; $i  256; $i++) {
   $keyStream[$i] = $i;
  }
  $j = 0;
  for ($i = 0; $i  256; $i++) {
   $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength]))  255;
   $temp = $keyStream[$i];
   $keyStream[$i] = $keyStream[$j];
   $keyStream[$j] = $temp;
  }
  $this->encryptIndex = $this->decryptIndex = array(0, 0);
  $this->encryptStream = $this->decryptStream = $keyStream;
 }
 /**
  * Dummy function.
  *
  * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
  * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
  * calling setKey().
  *
  * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
  * the IV's are relatively easy to predict, an attack described by
  * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
  * can be used to quickly guess at the rest of the key. The following links elaborate:
  *
  * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
  * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
  *
  * @param String $iv
  * @see Crypt_RC4::setKey()
  * @access public
  */
 function setIV($iv)
 {
 }
 /**
  * Sets MCrypt parameters. (optional)
  *
  * If MCrypt is being used, empty strings will be used, unless otherwise specified.
  *
  * @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
  * @access public
  * @param optional Integer $algorithm_directory
  * @param optional Integer $mode_directory
  */
 function setMCrypt($algorithm_directory = '', $mode_directory = '')
 {
  if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
   $this->mcrypt = array($algorithm_directory, $mode_directory);
   $this->_closeMCrypt();
  }
 }
 /**
  * Encrypts a message.
  *
  * @see Crypt_RC4::_crypt()
  * @access public
  * @param String $plaintext
  */
 function encrypt($plaintext)
 {
  return self::toHex($this->_crypt($plaintext, CRYPT_RC4_ENCRYPT));
 }
 /**
  * Decrypts a message.
  *
  * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
  * Atleast if the continuous buffer is disabled.
  *
  * @see Crypt_RC4::_crypt()
  * @access public
  * @param String $ciphertext
  */
 function decrypt($ciphertext)
 {
  $ciphertext = self::fromHex($ciphertext);
  return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
 }
 /**
  * Encrypts or decrypts a message.
  *
  * @see Crypt_RC4::encrypt()
  * @see Crypt_RC4::decrypt()
  * @access private
  * @param String $text
  * @param Integer $mode
  */
 function _crypt($text, $mode)
 {
  if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
   $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
   if ($this->$keyStream === false) {
    $this->$keyStream = mcrypt_module_open($this->mode, $this->mcrypt[0], MCRYPT_MODE_STREAM, $this->mcrypt[1]);
    mcrypt_generic_init($this->$keyStream, $this->key, '');
   } else if (!$this->continuousBuffer) {
    mcrypt_generic_init($this->$keyStream, $this->key, '');
   }
   $newText = mcrypt_generic($this->$keyStream, $text);
   if (!$this->continuousBuffer) {
    mcrypt_generic_deinit($this->$keyStream);
   }
   return $newText;
  }
  if ($this->encryptStream === false) {
   $this->setKey($this->key);
  }
  switch ($mode) {
   case CRYPT_RC4_ENCRYPT:
    $keyStream = $this->encryptStream;
    list($i, $j) = $this->encryptIndex;
    break;
   case CRYPT_RC4_DECRYPT:
    $keyStream = $this->decryptStream;
    list($i, $j) = $this->decryptIndex;
  }
  $newText = '';
  for ($k = 0; $k  strlen($text); $k++) {
   $i = ($i + 1)  255;
   $j = ($j + $keyStream[$i])  255;
   $temp = $keyStream[$i];
   $keyStream[$i] = $keyStream[$j];
   $keyStream[$j] = $temp;
   $temp = $keyStream[($keyStream[$i] + $keyStream[$j])  255];
   $newText.= chr(ord($text[$k]) ^ $temp);
  }
  if ($this->continuousBuffer) {
   switch ($mode) {
    case CRYPT_RC4_ENCRYPT:
     $this->encryptStream = $keyStream;
     $this->encryptIndex = array($i, $j);
     break;
    case CRYPT_RC4_DECRYPT:
     $this->decryptStream = $keyStream;
     $this->decryptIndex = array($i, $j);
   }
  }
  return $newText;
 }
 /**
  * Treat consecutive "packets" as if they are a continuous buffer.
  *
  * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  * will yield different outputs:
  *
  * code>
  * echo $rc4->encrypt(substr($plaintext, 0, 8));
  * echo $rc4->encrypt(substr($plaintext, 8, 8));
  * /code>
  * code>
  * echo $rc4->encrypt($plaintext);
  * /code>
  *
  * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  * another, as demonstrated with the following:
  *
  * code>
  * $rc4->encrypt(substr($plaintext, 0, 8));
  * echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  * /code>
  * code>
  * echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  * /code>
  *
  * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  *
  * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
  * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  * however, they are also less intuitive and more likely to cause you problems.
  *
  * @see Crypt_RC4::disableContinuousBuffer()
  * @access public
  */
 function enableContinuousBuffer()
 {
  $this->continuousBuffer = true;
 }
 /**
  * Treat consecutive packets as if they are a discontinuous buffer.
  *
  * The default behavior.
  *
  * @see Crypt_RC4::enableContinuousBuffer()
  * @access public
  */
 function disableContinuousBuffer()
 {
  if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) {
   $this->encryptIndex = $this->decryptIndex = array(0, 0);
   $this->setKey($this->key);
  }
  $this->continuousBuffer = false;
 }
 /**
  * Dummy function.
  *
  * Since RC4 is a stream cipher and not a block cipher, no padding is necessary. The only reason this function is
  * included is so that you can switch between a block cipher and a stream cipher transparently.
  *
  * @see Crypt_RC4::disablePadding()
  * @access public
  */
 function enablePadding()
 {
 }
 /**
  * Dummy function.
  *
  * @see Crypt_RC4::enablePadding()
  * @access public
  */
 function disablePadding()
 {
 }
 /**
  * Class destructor.
  *
  * Will be called, automatically, if you're using PHP5. If you're using PHP4, call it yourself. Only really
  * needs to be called if mcrypt is being used.
  *
  * @access public
  */
 function __destruct()
 {
  if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
   $this->_closeMCrypt();
  }
 }
 /**
  * Properly close the MCrypt objects.
  *
  * @access prviate
  */
 function _closeMCrypt()
 {
  if ( $this->encryptStream !== false ) {
   if ( $this->continuousBuffer ) {
    mcrypt_generic_deinit($this->encryptStream);
   }
   mcrypt_module_close($this->encryptStream);
   $this->encryptStream = false;
  }
  if ( $this->decryptStream !== false ) {
   if ( $this->continuousBuffer ) {
    mcrypt_generic_deinit($this->decryptStream);
   }
   mcrypt_module_close($this->decryptStream);
   $this->decryptStream = false;
  }
 }
 // @function fromHex 把十六進制數轉換成字符串
 function toHex($sa , $len = 0){
  $buf = "";
  if( $len == 0 )
   $len = strlen($sa) ;
  for ($i = 0; $i  $len; $i++)
  {
   $val = dechex(ord($sa{$i}));  
   if(strlen($val) 2) 
    $val = "0".$val;
   $buf .= $val;
  }
  return $buf;
 }
 // @function fromHex 把十六進制數轉換成字符串 
 function fromHex($sa){
  $buf = "";
  $len = strlen($sa) ;
  for($i = 0; $i  $len; $i += 2){
   $val = chr(hexdec(substr($sa, $i, 2)));
   $buf .= $val;
  }
  return $buf;
 }
}

使用方法:

include('class.rc4crypt.php');
$rc4 = new Crypt_RC4();
$rc4 -> setKey('21sd54a1w5q');
$text = 'www.jb51.net';
echo $x = $rc4->encrypt($text);//加密
echo 'br />';
echo $rc4->decrypt( $x) ;//解密

運行結果:

7907bb7c6694f179e9642ebd
www.jb51.net

PS:關于加密解密感興趣的朋友還可以參考本站在線工具:

在線RC4加密/解密工具:
http://tools.jb51.net/password/rc4_encode

文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode

在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php加密方法總結》、《PHP編碼與轉碼操作技巧匯總》、《PHP數學運算技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP數據結構與算法教程》、《php程序設計算法總結》及《php正則表達式用法總結》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHP rsa加密解密算法原理解析
  • 基于PHP實現解密或加密Cloudflar郵箱保護
  • PHP實現的AES加密、解密封裝類與用法示例
  • PHP實現的DES加密解密類定義與用法示例
  • 基于PHP RSA密文過長加密解密 越過1024的解決方法
  • PHP的RSA加密解密方法以及開發接口使用
  • 六種php加密解密方法實例講解

標簽:白銀 太原 辛集 鄂州 廊坊 綏化 崇左 衡陽

巨人網絡通訊聲明:本文標題《php實現的rc4加密解密類定義與用法示例》,本文關鍵詞  php,實現,的,rc4,加密解密,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php實現的rc4加密解密類定義與用法示例》相關的同類信息!
  • 本頁收集關于php實現的rc4加密解密類定義與用法示例的相關信息資訊供網民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    日韩精品久久久久久| 国产精品丝袜黑色高跟| 日韩欧美一区在线观看| 国产精品福利影院| 国产成人三级在线观看| 欧美一级日韩免费不卡| 蜜桃久久av一区| 色婷婷av一区二区三区大白胸| 中文字幕免费不卡在线| 国内外精品视频| 精品剧情v国产在线观看在线| 亚洲成人三级小说| 在线免费精品视频| 日本一道高清亚洲日美韩| 欧美日韩一区二区三区高清| 洋洋成人永久网站入口| 在线免费观看日本一区| 日本美女视频一区二区| 91麻豆精品国产91久久久更新时间 | 色婷婷亚洲精品| 一二三四社区欧美黄| 欧美日韩精品三区| 免费成人在线播放| 久久久精品影视| 99久久精品国产观看| 亚洲一区二区三区视频在线| 7777精品伊人久久久大香线蕉的 | 久久精品免费看| 欧美精品一区二区三区很污很色的| 久草精品在线观看| 亚洲人妖av一区二区| 91福利国产精品| 日韩中文字幕亚洲一区二区va在线| 欧美一区二区女人| 午夜精品一区二区三区三上悠亚| 欧美电影一区二区| 国产宾馆实践打屁股91| 亚洲动漫第一页| 久久蜜桃香蕉精品一区二区三区| 成人av网址在线| 丝袜脚交一区二区| 日本一区二区成人| 91精品国产综合久久精品麻豆 | 国产丝袜在线精品| 在线看日韩精品电影| 美女免费视频一区| 亚洲人快播电影网| 精品国内片67194| 欧美在线播放高清精品| 三级亚洲高清视频| 自拍偷拍亚洲激情| 欧美精品一区二区在线观看| 欧美色电影在线| 成人在线视频一区二区| 亚洲国产精品一区二区www在线 | 亚洲福利电影网| 国产精品久久久久一区二区三区 | 色综合天天狠狠| 国产成人午夜99999| 老色鬼精品视频在线观看播放| 亚洲男同性视频| 国产亚洲精品bt天堂精选| 欧美精品黑人性xxxx| 99久久综合狠狠综合久久| 九九九久久久精品| 蜜桃av一区二区| 日韩avvvv在线播放| 亚洲另类在线制服丝袜| 男人操女人的视频在线观看欧美| 亚洲天堂免费看| 国产精品天干天干在线综合| 精品久久一区二区| 日韩亚洲欧美成人一区| 欧美精品自拍偷拍动漫精品| 色94色欧美sute亚洲线路一ni| 不卡免费追剧大全电视剧网站| 国产一区二区三区电影在线观看 | 日日夜夜精品视频免费| 亚洲在线中文字幕| 亚洲图片欧美色图| 亚洲一级片在线观看| 亚洲最新在线观看| 亚洲一区二区视频在线观看| 亚洲综合成人在线视频| 亚洲自拍偷拍av| 亚洲国产日日夜夜| 亚洲大片一区二区三区| 日韩精品一级中文字幕精品视频免费观看 | 懂色av一区二区夜夜嗨| 国产成人av影院| 成人激情文学综合网| 99视频一区二区三区| 在线欧美一区二区| 欧美日韩一区不卡| 欧美电影免费观看完整版| ww亚洲ww在线观看国产| 欧美韩国日本不卡| 亚洲人成亚洲人成在线观看图片| 香蕉久久夜色精品国产使用方法| 人禽交欧美网站| 国产盗摄一区二区三区| a4yy欧美一区二区三区| 欧美日韩中文字幕一区二区| 日韩亚洲欧美一区二区三区| 国产偷国产偷亚洲高清人白洁 | 久久国产人妖系列| 高清国产一区二区| 一本大道久久a久久综合婷婷 | 国产午夜精品一区二区| 一区二区三区鲁丝不卡| 日本中文字幕一区二区视频| 国产精品一级片在线观看| 91在线观看视频| 日韩一区二区麻豆国产| 国产精品美女一区二区| 日韩**一区毛片| 91麻豆福利精品推荐| 精品国产一区二区三区av性色| 亚洲男人天堂一区| 奇米色一区二区三区四区| 92精品国产成人观看免费| 日韩欧美一级二级三级久久久| 国产精品毛片无遮挡高清| 日韩精品亚洲一区二区三区免费| 成人一区二区三区| 久久综合九色综合97婷婷女人 | 国产福利一区二区三区视频在线| 欧美在线观看18| 欧美韩国日本不卡| 捆绑紧缚一区二区三区视频 | 亚洲电影一级片| av日韩在线网站| 久久久亚洲高清| 奇米色一区二区| 欧美精品免费视频| 一区二区三区四区激情| 懂色av中文字幕一区二区三区| 91精品国产综合久久福利| 亚洲男人的天堂av| 丁香桃色午夜亚洲一区二区三区| 欧美年轻男男videosbes| 国产精品剧情在线亚洲| 国产成人鲁色资源国产91色综| 欧美一级专区免费大片| 亚洲一区av在线| 欧美性极品少妇| 亚洲国产精品自拍| 91无套直看片红桃| 国产精品国产三级国产专播品爱网| 人人爽香蕉精品| 欧美日韩在线播放一区| 亚洲精品欧美专区| 色av成人天堂桃色av| 亚洲夂夂婷婷色拍ww47| 99综合电影在线视频| 亚洲日本一区二区| 热久久久久久久| 91精品国产aⅴ一区二区| 日韩va欧美va亚洲va久久| 69p69国产精品| 日本视频一区二区| 精品久久久久久久久久久久久久久| 美女视频黄a大片欧美| 精品卡一卡二卡三卡四在线| 国产精品一区免费在线观看| 久久久99免费| 99久久久久久| 亚洲免费观看高清完整版在线| 色丁香久综合在线久综合在线观看| 亚洲精品v日韩精品| 欧美高清视频不卡网| 韩国一区二区在线观看| 国产精品久久毛片a| 色视频一区二区| 久久99精品久久久久久| 中文字幕在线不卡| 色天天综合色天天久久| 久久精品国产精品亚洲红杏| 中文字幕日本乱码精品影院| 日韩欧美一区二区三区在线| 91亚洲男人天堂| 麻豆国产一区二区| 综合久久久久久久| 欧美精品一区男女天堂| 欧美日产在线观看| 色久优优欧美色久优优| 国产99久久久国产精品潘金网站| 一区二区三区.www| 欧美国产精品久久| 欧美一二三四区在线| 成人激情免费视频| 久久99蜜桃精品| 午夜一区二区三区视频| 欧美激情一二三区| 日韩久久久久久| 欧美老女人第四色| 国产精品欧美经典| 日韩三级高清在线| 欧美亚洲国产一区二区三区| 国产成人av在线影院|