A des encryption algorithm recently written in PHP 1. example. php
Encode ($ str); echo"
Des encryption result:
"; Echo $ encode; $ decode = $ Des-> decode ($ encode); echo"
Des decryption result:
"; Echo $ decode; $ TripleDes = new TripleDes ('000000', 'abcdefgh'); $ encode = $ TripleDes-> encode ($ str); echo"
3Des encryption result:
"; Echo $ encode; $ decode = $ TripleDes-> decode ($ encode); echo"
3Des decryption result:
"; Echo $ decode;?>
2. TripleDes. class. php
DesArr [] = new Des ($ key1); $ this-> DesArr [] = new Des ($ key2);} public function encode ($ content) {return $ this-> DesArr [0]-> encode ($ this-> DesArr [1]-> decode ($ this-> DesArr [0]-> encode ($ content )));} public function decode ($ content) {return $ this-> DesArr [0]-> decode ($ this-> DesArr [1]-> encode ($ this-> DesArr [0]-> decode ($ content) ;}}?>
3. Des. class. php
Incluey = new incluey ($ key);} public function encode ($ content) {return $ this-> authCode ($ content, 'encoding ');} public function decode ($ content) {return $ this-> authCode ($ content, 'decode ');} /*** encrypted start function * @ param string $ type encryption type * @ param type $ content encrypted content * @ return type encryption result */public function authCode ($ content, $ type = 'encoding') {if ($ type! = 'Encoding') {$ type = 'decode';} $ contentEncodeArr = array (); $ contentArr = str_split ($ content, 8); $ encodeContent = ''; for ($ index = 0; $ index <count ($ contentArr); $ index ++) {$ content = $ contentArr [$ index]; if (strlen ($ content) <8) {$ content. = str_repeat ($ this-> contentAdd, (8-strlen ($ content) ;}$ contentBitArr = bytesToBitArr ($ content); list ($ L, $ R) = array_chunk ($ contentBitArr, 32); $ ContentEncodeArr = $ this-> _ run ($ L, $ R, $ type); $ byteArr = array_chunk ($ contentEncodeArr, 8); for ($ index1 = 0; $ index1 <count ($ byteArr); $ index1 ++) {$ byte = 0; for ($ I = 0; $ I <count ($ byteArr [$ index1]); $ I ++) {$ byte + = $ byteArr [$ index1] [$ I] * pow (2, 7-$ I);} $ encodeContent. = chr ($ byte) ;}return $ encodeContent ;} /*** iteration function in Feistel structure encryption algorithm * @ param type $ L 32-bit input in the left half * @ param type $ R 32-bit Enter * @ param type $ method encode (encrypted) or decode (decrypted) in the right half) * @ param type $ number of round iterations * @ return type */private function _ run ($ L, $ R, $ method = "encode", $ round = 0) {$ nextL = ''; // enter $ nextR ='' in the left half of the next round ''; // input $ subKey48Bit in the right half of the next round = $ this-> your ey-> getSubKeyAt ($ round, $ method ); // sub-key $ FResult32Bit = $ this-> _ F ($ subKey48Bit, $ R); // round function result // XOR or for ($ index = 0; $ index <count ($ FResult32Bit); $ index ++) {$ FResult 32Bit [$ index] = $ FResult32Bit [$ index] ===$ L [$ index]? False: true;} $ nextL = $ R; $ nextR = $ FResult32Bit; // The number of rounds stops at 15. a total of 16 rounds of if ($ round> = 15) are encrypted) {return array_merge ($ nextR, $ nextL);} else {return $ this-> _ run ($ nextL, $ nextR, $ method, ++ $ round );}} /*** wheel function in Feitel architecture * @ param type $ subKey48Bit 48-bit sub-Key * @ param type $ R enter the right part of the current wheel * @ return 32-bit result */ public function _ F ($ subKey48Bit, $ R) {$ tmp48Bit = array (); // E table replacement for ($ index = 0; $ index <count ($ this -> PermutationETable); $ index ++) {$ tmp48Bit [] = $ R [$ this-> permutationETable [$ index]-1];} // for ($ index = 0; $ index <count ($ tmp48Bit); $ index ++) {$ tmp48Bit [$ index] = $ tmp48Bit [$ index] ===$ subKey48Bit [$ index]? False: true;} // replace/Select (s box) $ tem32Bit = array (); $ tem6BitArr = array_chunk ($ tmp48Bit, 6); for ($ index = 0; $ index <count ($ tem6BitArr); $ index ++) {$ tem6Bit = $ tem6BitArr [$ index]; $ line = $ tem6Bit [0] * 2 + $ tem6Bit [5] * 1; $ field = $ tem6Bit [1] * 2*2*2 + $ tem6Bit [2] * 2*2 + $ tem6Bit [3] * 2 + $ tem6Bit [4]; $ selectPos = $ line * 6 + $ field; $ select = $ this-> sBox [$ index] [$ selectPos]; if ($ select> = 8) {$ tem32Bit [] = true;} else {$ tem32Bit [] = false;} $ select % = 8; if ($ select> = 4) {$ tem32Bit [] = true;} else {$ tem32Bit [] = false;} $ select % = 4; if ($ select> = 2) {$ tem32Bit [] = true;} else {$ tem32Bit [] = false;} $ select % = 2; if ($ select> = 1) {$ tem32Bit [] = true;} else {$ tem32Bit [] = false ;}// replace P $ FResult32Bit = array (); for ($ index = 0; $ index <count ($ this-> permutationPTable); $ index ++) {$ FResult32Bit [] = $ tem32Bit [$ this-> permutationPTable [$ index]-1];} return $ FResult32Bit ;}}
4. policey. class. php
_ InitKey ($ key); $ this-> _ generateSubKey ();} /*** obtain the sub-key at the specified position * @ param type $ index position * @ param type $ method determines whether it is a forward or backward subkey * @ return type 48bit */public function getSubKeyAt ($ index, $ method = 'encoding') {if ($ method = 'encoding') {return $ this-> subKeyArr [$ index];} else {return $ this-> subKeyArr [15-$ index] ;}} /*** initialize a 64-bit key * @ param type $ key character key */private function _ initKey ($ key) {$ key = substr ($ key, 0, 8); $ keyLen = strlen ($ key); // complete 64-bit if ($ keyLen <8) {$ key. = str_repeat ($ this-> keyAdd, 8-$ keyLen) ;}$ this-> key = $ key; $ bitArr = bytesToBitArr ($ this-> key ); // initialize C0, with 28 bits on the left for ($ index = 0; $ index <32; $ index ++) {if ($ index % 8 = 7) {continue;} $ this-> CkeyArr [] = $ bitArr [$ index];} // initialize D0, with 28 bits on the right ($ index = 32; $ index <64; $ index ++) {if ($ index % 8 === 7) {continue ;} $ this-> DkeyArr [] = $ bitArr [$ index] ;}} /*** generate 16 sub-keys in 16 rounds * @ param type $ current number of round rounds */private function _ generateSubKey ($ round = 0) {// shift left $ tmp28BitC = array_shift ($ this-> CkeyArr); $ tmp28BitD = array_shift ($ this-> DkeyArr); $ this-> CkeyArr [] = $ tmp28BitC; $ this-> DkeyArr [] = $ tmp28BitD; // whether to move left if ($ this-> leftShiftArr [$ round] = 2) {$ tmp28BitC = array_shift ($ this-> CkeyArr); $ tmp28BitD = array_shift ($ this-> DkeyArr); $ this-> CkeyArr [] = $ tmp28BitC; $ this-> DkeyArr [] = $ tmp28BitD;} $ tem56BitCDkey = array_merge ($ this-> CkeyArr, $ this-> DkeyArr); $ tem48BitSubkey = array (); // replace & compress for ($ index = 0; $ index <count ($ this-> permutationTable); $ index ++) {$ tem48BitSubkey [] = $ tem56BitCDkey [$ this-> permutationTable [$ index]-1];} $ this-> subKeyArr [] = $ tem48BitSubkey; if ($ round <15) {$ this-> _ generateSubKey (++ $ round );}}}
5. toolFunction. php
= 128) {$ boolArr [] = true;} else {$ boolArr [] = false ;}} return $ boolArr ;} /*** test with * @ param type $ bitArr */function dumpBit ($ bitArr) {for ($ index = 0; $ index <count ($ bitArr ); $ index ++) {if ($ bitArr [$ index]) {echo '1';} else {echo '0';} echo'
';}/*** Test with * @ param type $ num * @ param type $ str */function testDesTime ($ num = 100, $ str = 'Test test ') {$ startPoint = microtimeFloat (); for ($ index = 0; $ index <$ num; $ index ++) {$ Des = new Des ('20140901 '); $ encode = $ Des-> encode ($ str) ;}$ endPoint = microtimeFloat (); $ allTime = $ endPoint-$ startPoint; return $ allTime ;} /*** test with * @ param type $ num * @ param type $ str */function testMd5Time ($ num = 100, $ str = 'Test test ') {$ startPoint = microtimeFloat (); for ($ index = 0; $ index <$ num; $ index ++) {$ encode = md5 ($ str );} $ endPoint = microtimeFloat (); $ allTime = $ endPoint-$ startPoint; return $ allTime ;} /*** test with * @ param type $ num * @ param type $ str */function microtimeFloat () {list ($ usec, $ sec) = explode ("", microtime (); return (float) $ usec + (float) $ sec );}