版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第php實(shí)現(xiàn)的rc4加密解密類定義與用法示例本文實(shí)例講述了php實(shí)現(xiàn)的rc4加密解密類。分享給大家供大家參考,具體如下:
class.rc4crypt.php文件:
*By
define('CRYPT_RC4_MODE_INTERNAL',1);
define('CRYPT_RC4_MODE_MCRYPT',2);
define('CRYPT_RC4_ENCRYPT',0);
define('CRYPT_RC4_DECRYPT',1);
classCrypt_RC4{
*TheKey
*@seeCrypt_RC4::setKey()
*@varString
*@accessprivate
var$key="\0";
*TheKeyStreamforencryption
*IfCRYPT_RC4_MODE==CRYPT_RC4_MODE_MCRYPT,thiswillbeequaltothemcryptobject
*@seeCrypt_RC4::setKey()
*@varArray
*@accessprivate
var$encryptStream=false;
*TheKeyStreamfordecryption
*IfCRYPT_RC4_MODE==CRYPT_RC4_MODE_MCRYPT,thiswillbeequaltothemcryptobject
*@seeCrypt_RC4::setKey()
*@varArray
*@accessprivate
var$decryptStream=false;
*The$iand$jindexesforencryption
*@seeCrypt_RC4::_crypt()
*@varInteger
*@accessprivate
var$encryptIndex=0;
*The$iand$jindexesfordecryption
*@seeCrypt_RC4::_crypt()
*@varInteger
*@accessprivate
var$decryptIndex=0;
*MCryptparameters
*@seeCrypt_RC4::setMCrypt()
*@varArray
*@accessprivate
var$mcrypt=array('','');
*TheEncryptionAlgorithm
*OnlyusedifCRYPT_RC4_MODE==CRYPT_RC4_MODE_MCRYPT.OnlypossiblevaluesareMCRYPT_RC4orMCRYPT_ARCFOUR.
*@seeCrypt_RC4::Crypt_RC4()
*@varInteger
*@accessprivate
var$mode;
*DefaultConstructor.
*Determineswhetherornotthemcryptextensionshouldbeused.
*@paramoptionalInteger$mode
*@returnCrypt_RC4
*@accesspublic
var$continuousBuffer;
functionCrypt_RC4()
if(!defined('CRYPT_RC4_MODE')){
switch(true){
caseextension_loaded('mcrypt')(defined('MCRYPT_ARCFOUR')||defined('MCRYPT_RC4')):
//i'dchecktoseeifrc4wassupported,bydoingin_array('arcfour',mcrypt_list_algorithms('')),
//butsincethatcanbechangedaftertheobjecthasbeencreated,theredoesn'tseemtobe
//alotofpoint...
define('CRYPT_RC4_MODE',CRYPT_RC4_MODE_MCRYPT);
break;
default:
define('CRYPT_RC4_MODE',CRYPT_RC4_MODE_INTERNAL);
switch(CRYPT_RC4_MODE){
caseCRYPT_RC4_MODE_MCRYPT:
switch(true){
casedefined('MCRYPT_ARCFOUR'):
$this-mode=MCRYPT_ARCFOUR;
break;
casedefined('MCRYPT_RC4');
$this-mode=MCRYPT_RC4;
*Setsthekey.
*Keyscanbebetween1and256byteslong.Iftheyarelongerthen256bytes,thefirst256byteswill
*beused.Ifnokeyisexplicitlyset,it'llbeassumedtobeasinglenullbyte.
*@accesspublic
*@paramString$key
functionsetKey($key)
$this-key=$key;
if(CRYPT_RC4_MODE==CRYPT_RC4_MODE_MCRYPT){
return;
$keyLength=strlen($key);
$keyStream=array();
for($i=0;$i256;$i++){
$keyStream[$i]=$i;
$j=0;
for($i=0;$i256;$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;
*Dummyfunction.
*Someprotocols,suchasWEP,prependan"initializationvector"tothekey,effectivelycreatinganewkey[1].
*Ifyouneedtouseaninitializationvectorinthismanner,feelfreetoprependittothekey,yourself,before
*callingsetKey().
*[1]WEP'sinitializationvectors(IV's)areusedinasomewhatinsecureway.Since,inthatprotocol,
*theIV'sarerelativelyeasytopredict,anattackdescribedby
*{@link/~aboba/IEEE/rc4_ksaproc.pdfScottFluhrer,ItsikMantin,andAdiShamir}
*canbeusedtoquicklyguessattherestofthekey.Thefollowinglinkselaborate:
*{@link/rsalabs/node.aspid=2009/rsalabs/node.aspid=2009}
*{@link/wiki/Related_key_attack/wiki/Related_key_attack}
*@paramString$iv
*@seeCrypt_RC4::setKey()
*@accesspublic
functionsetIV($iv)
*SetsMCryptparameters.(optional)
*IfMCryptisbeingused,emptystringswillbeused,unlessotherwisespecified.
*@link/function.mcrypt-module-open#function.mcrypt-module-open
*@accesspublic
*@paramoptionalInteger$algorithm_directory
*@paramoptionalInteger$mode_directory
functionsetMCrypt($algorithm_directory='',$mode_directory='')
if(CRYPT_RC4_MODE==CRYPT_RC4_MODE_MCRYPT){
$this-mcrypt=array($algorithm_directory,$mode_directory);
$this-_closeMCrypt();
*Encryptsamessage.
*@seeCrypt_RC4::_crypt()
*@accesspublic
*@paramString$plaintext
functionencrypt($plaintext)
returnself::toHex($this-_crypt($plaintext,CRYPT_RC4_ENCRYPT));
*Decryptsamessage.
*$this-decrypt($this-encrypt($plaintext))==$this-encrypt($this-encrypt($plaintext)).
*Atleastifthecontinuousbufferisdisabled.
*@seeCrypt_RC4::_crypt()
*@accesspublic
*@paramString$ciphertext
functiondecrypt($ciphertext)
$ciphertext=self::fromHex($ciphertext);
return$this-_crypt($ciphertext,CRYPT_RC4_DECRYPT);
*Encryptsordecryptsamessage.
*@seeCrypt_RC4::encrypt()
*@seeCrypt_RC4::decrypt()
*@accessprivate
*@paramString$text
*@paramInteger$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,'');
}elseif(!$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){
caseCRYPT_RC4_ENCRYPT:
$keyStream=$this-encryptStream;
list($i,$j)=$this-encryptIndex;
break;
caseCRYPT_RC4_DECRYPT:
$keyStream=$this-decryptStream;
list($i,$j)=$this-decryptIndex;
$newText='';
for($k=0;$kstrlen($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){
caseCRYPT_RC4_ENCRYPT:
$this-encryptStream=$keyStream;
$this-encryptIndex=array($i,$j);
break;
caseCRYPT_RC4_DECRYPT:
$this-decryptStream=$keyStream;
$this-decryptIndex=array($i,$j);
return$newText;
*Treatconsecutive"packets"asiftheyareacontinuousbuffer.
*Sayyouhavea16-byteplaintext$plaintext.Usingthedefaultbehavior,thetwofollowingcodesnippets
*willyielddifferentoutputs:
*code
*echo$rc4-encrypt(substr($plaintext,0,8));
*echo$rc4-encrypt(substr($plaintext,8,8));
*/code
*code
*echo$rc4-encrypt($plaintext);
*/code
*Thesolutionistoenablethecontinuousbuffer.Althoughthiswillresolvetheabovediscrepancy,itcreates
*another,asdemonstratedwiththefollowing:
*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
*Withthecontinuousbufferdisabled,thesewouldyieldthesameoutput.Withitenabled,theyyielddifferent
*outputs.Thereasonisduetothefactthattheinitializationvector'schangeaftereveryencryption/
*decryptionroundwhenthecontinuousbufferisenabled.Whenit'sdisabled,theyremainconstant.
*Putanotherway,whenthecontinuousbufferisenabled,thestateoftheCrypt_DES()objectchangesaftereach
*encryption/decryptionround,whereasotherwise,it'dremainconstant.Forthisreason,it'srecommendedthat
*continuousbuffersnotbeused.Theydoofferbettersecurityandare,infact,sometimesrequired(SSHusesthem),
*however,theyarealsolessintuitiveandmorelikelytocauseyouproblems.
*@seeCrypt_RC4::disableContinuousBuffer()
*@accesspublic
functionenableContinuousBuffer()
$this-continuousBuffer=true;
*Treatconsecutivepacketsasiftheyareadiscontinuousbuffer.
*Thedefaultbehavior.
*@seeCrypt_RC4::enableContinuousBuffer()
*@accesspublic
functiondisableContinuousBuffer()
if(CRYPT_RC4_MODE==CRYPT_RC4_MODE_INTERNAL){
$this-encryptIndex=$this-decryptIndex=array(0,0);
$this-setKey($this-key);
$this-continuousBuffer=false;
*Dummyfunction.
*SinceRC4isastreamcipherandnotablockcipher,nopaddingisnecessary.Theonlyreasonthisfunctionis
*includedissothatyoucanswitchbetweenablockcipherandastreamciphertransparently.
*@seeCrypt_RC4::disablePadding()
*@accesspublic
functionenablePadding()
*Dummyfunction.
*@seeCrypt_RC4::enablePadding()
*@accesspublic
functiondisablePadding()
*Classdestructor.
*Willbecalled,automatically,ifyou'reusingPHP5.Ifyou'reusingPHP4,callityourself.Onlyreally
*needstobecalledifmcryptisbeingused.
*@accesspublic
function__destruct()
if(CRYPT_RC4_MODE==CRYPT_RC4_MODE_MCRYPT){
$this-_closeMCrypt();
*ProperlyclosetheMCryptobjects.
*@accessprviate
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;
//@functionfromHex把十六進(jìn)制數(shù)轉(zhuǎn)換成字符串
functiontoHex($sa,$len=0){
$buf="";
if($len==0)
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 安全員A證考試考前沖刺練習(xí)試題及完整答案詳解(奪冠)
- 燃?xì)夤艿澜](méi)區(qū)設(shè)計(jì)方案
- 安全員A證考試通關(guān)考試題庫(kù)含答案詳解(研優(yōu)卷)
- 安全員A證考試全真模擬模擬題及參考答案詳解(培優(yōu))
- 安全員A證考試真題匯編含完整答案詳解【各地真題】
- 安全員A證考試練習(xí)題(一)附參考答案詳解(完整版)
- 安全員A證考試通關(guān)模擬題庫(kù)及參考答案詳解(綜合卷)
- 安全員A證考試復(fù)習(xí)提分資料含答案詳解【綜合卷】
- 安全員A證考試模擬卷包及答案詳解一套
- 安全員A證考試考前沖刺分析【網(wǎng)校專用】附答案詳解
- 南京醫(yī)科大學(xué)2026年招聘人事代理人員備考題庫(kù)及1套參考答案詳解
- 2026年教育平臺(tái)資源輸出協(xié)議
- 【《四旋翼飛行器坐標(biāo)系及相互轉(zhuǎn)換關(guān)系分析綜述》1000字】
- 2026浙江金華市婺城區(qū)城市發(fā)展控股集團(tuán)有限公司招聘59人筆試參考題庫(kù)及答案解析
- 靜脈補(bǔ)液課件
- 廣東深圳市鹽田高級(jí)中學(xué)2024~2025學(xué)年高一上冊(cè)1月期末考試化學(xué)試題 附答案
- 2026年輔警招聘考試試題庫(kù)附答案【完整版】
- 建筑施工風(fēng)險(xiǎn)辨識(shí)與防范措施
- 浙江省杭州地區(qū)六校2026屆化學(xué)高一第一學(xué)期期末學(xué)業(yè)水平測(cè)試試題含解析
- 2025年CFA二級(jí)估值與財(cái)務(wù)報(bào)表分析試卷(含答案)
- 2025年宜昌化學(xué)真題試卷及答案
評(píng)論
0/150
提交評(píng)論