Algorithme de cryptage simple

Contenu du snippet

Une classe de cryptage basique ...
L'abondence des boucles rend celui-ci assez lent et doit pouvoir etre facilement optimisable.

Source / Exemple :


<?

class Cypher_dKe
{
	var $data = array();
	var $K = array(
		0x493e0,	0x61a80,	0x7a120,	0x927c0,	0xaae60,	
		0xc3500,	0xdbba0,	0xf4240,	0x10c8e0,	0x124f80,	
		0x13d620,	0x155cc0,	0x16e360,	0x186a00,	0x19f0a0,	
		0x1b7740,	0x1cfde0,	0x1e8480,	0x200b20,	0x2191c0,	
		0x231860,	0x249f00,	0x2625a0,	0x27ac40,	0x2932e0,	
		0x2ab980,	0x2c4020,	0x2dc6c0,	0x2f4d60,	0x30d400,	
		0x325aa0,	0x33e140,	0x3567e0,	0x36ee80,	0x387520,	
		0x39fbc0,	0x3b8260,	0x3d0900,	0x3e8fa0,	0x401640,	
		0x419ce0,	0x432380,	0x44aa20,	0x4630c0,	0x47b760,	
		0x493e00,	0x4ac4a0,	0x4c4b40,	0x4dd1e0,	0x4f5880,	
		0x50df20,	0x5265c0,	0x53ec60,	0x557300,	0x56f9a0,	
		0x588040,	0x5a06e0,	0x5b8d80,	0x5d1420,	0x5e9ac0,	
		0x602160,	0x61a800,	0x632ea0,	0x64b540,	0x663be0,	
		0x67c280,	0x694920,	0x6acfc0,	0x6c5660,	0x6ddd00,	
		0x6f63a0,	0x70ea40,	0x7270e0,	0x73f780,	0x757e20,	
		0x7704c0,	0x788b60,	0x7a1200,	0x7b98a0,	0x7d1f40,	
		0x7ea5e0,	0x802c80,	0x81b320,	0x8339c0,	0x84c060,	
		0x864700,	0x87cda0,	0x895440,	0x8adae0,	0x8c6180,	
		0x8de820,	0x8f6ec0,	0x90f560,	0x927c00,	0x9402a0,	
		0x958940,	0x970fe0,	0x989680,	0x9a1d20,	0x9ba3c0,	
		0x9d2a60,	0x9eb100,	0xa037a0,	0xa1be40,	0xa344e0,	
		0xa4cb80,	0xa65220,	0xa7d8c0,	0xa95f60,	0xaae600,	
		0xac6ca0,	0xadf340,	0xaf79e0,	0xb10080,	0xb28720,	
		0xb40dc0,	0xb59460,	0xb71b00,	0xb8a1a0,	0xba2840,	
		0xbbaee0,	0xbd3580,	0xbebc20,	0xc042c0,	0xc1c960,	
		0xc35000,	0xc4d6a0,	0xc65d40,	0xc7e3e0,	0xc96a80,	
		0xcaf120,	0xcc77c0,	0xcdfe60,	0xcf8500,	0xd10ba0,	
		0xd29240,	0xd418e0,	0xd59f80,	0xd72620,	0xd8acc0,	
		0xda3360,	0xdbba00,	0xdd40a0,	0xdec740,	0xe04de0,	
		0xe1d480,	0xe35b20,	0xe4e1c0,	0xe66860,	0xe7ef00,	
		0xe975a0,	0xeafc40,	0xec82e0,	0xee0980,	0xef9020,	
		0xf116c0,	0xf29d60,	0xf42400,	0xf5aaa0,	0xf73140,	
		0xf8b7e0,	0xfa3e80,	0xfbc520,	0xfd4bc0,	0xfed260,	
		0x1005900,	0x101dfa0,	0x1036640,	0x104ece0,	0x1067380,	
		0x107fa20,	0x10980c0,	0x10b0760,	0x10c8e00,	0x10e14a0,	
		0x10f9b40,	0x11121e0,	0x112a880,	0x1142f20,	0x115b5c0,	
		0x1173c60,	0x118c300,	0x11a49a0,	0x11bd040,	0x11d56e0,	
		0x11edd80,	0x1206420,	0x121eac0,	0x1237160,	0x124f800,	
		0x1267ea0,	0x1280540,	0x1298be0,	0x12b1280,	0x12c9920,	
		0x12e1fc0,	0x12fa660,	0x1312d00,	0x132b3a0,	0x1343a40,	
		0x135c0e0,	0x1374780,	0x138ce20,	0x13a54c0,	0x13bdb60,	
		0x13d6200,	0x13ee8a0,	0x1406f40,	0x141f5e0,	0x1437c80,	
		0x1450320,	0x14689c0,	0x1481060,	0x1499700,	0x14b1da0,	
		0x14ca440,	0x14e2ae0,	0x14fb180,	0x1513820,	0x152bec0,	
		0x1544560,	0x155cc00,	0x15752a0,	0x158d940,	0x15a5fe0,	
		0x15be680,	0x15d6d20,	0x15ef3c0,	0x1607a60,	0x1620100,	
		0x16387a0,	0x1650e40,	0x16694e0,	0x1681b80,	0x169a220,	
		0x16b28c0,	0x16caf60,	0x16e3600,	0x16fbca0,	0x1714340,	
		0x172c9e0,	0x1745080,	0x175d720,	0x1775dc0,	0x178e460,	
		0x17a6b00,	0x17bf1a0,	0x17d7840,	0x17efee0,	0x1808580,	
		0x1820c20,	0x18392c0,	0x1851960,	0x186a000,	0x18826a0,	
		0x189ad40,	0x18b33e0,	0x18cba80,	0x18e4120,	0x18fc7c0,	
		0x1914e60,	0x192d500,	0x1945ba0,	0x195e240,	0x19768e0,	
		0x198ef80,	0x19a7620,	0x19bfcc0,	0x19d8360,	0x19f0a00,	
		0x1a090a0,	0x1a21740,	0x1a39de0,	0x1a52480,	0x1a6ab20,	
		0x1a831c0,	0x1a9b860,	0x1ab3f00,	0x1acc5a0,	0x1ae4c40,	
		0x1afd2e0,	0x1b15980,	0x1b2e020,	0x1b466c0,	0x1b5ed60,	
		0x1b77400,	0x1b8faa0,	0x1ba8140,	0x1bc07e0,	0x1bd8e80,	
		0x1bf1520,	0x1c09bc0,	0x1c22260,	0x1c3a900,	0x1c52fa0,	
		0x1c6b640,	0x1c83ce0,	0x1c9c380,	0x1cb4a20,	0x1ccd0c0	
	);
	var $ret = false;
	
	
	function dkcypher($param = false)
	{
		return true;
	}

	function encrypt($string)
	{
		$this->data['char'] = array();
		$this->data['cypher'] = array();
		$this->data['length'] = strlen($string);
		
		if(($this->data['length'] % 2) == 1)
		{
			$string .= chr( 0x00 );
			$this->data['length'] += 1;
		}
		
		for($i = 0; $i < $this->data['length']; $i++)
			$this->data['char'][] = base_convert( ord( $string{$i} ), 10, 8);

		for($i = 0; $i < $this->data['length']; $i++)
		{
			$L = $this->data['char'][$i];
			$R = $this->data['char'][$this->data['length'] - $i - 1];

			for($j = 0; $j < 300; $j++)
			{
				$L ^= $this->K[$j];
				$R ^= $this->K[$j];
			}
			
			$this->data['cypher'][$i] = $L;
			$this->data['cypher'][$this->data['length'] - $i - 1] = $R;
		}

		$this->ret = false;
		for($i = 0; $i < $this->data['length']; $i++)
		{
			$this->ret .= ( chr( substr( $this->data['cypher'][$i], 0, 2) ) ^ '&' );
			$this->ret .= ( chr( substr( $this->data['cypher'][$i], 2, 2) ) ^ '%' );
			$this->ret .= ( chr( substr( $this->data['cypher'][$i], 4, 2) ) ^ '$' );
			$this->ret .= ( chr( substr( $this->data['cypher'][$i], 6, 2) ) ^ '?' );
		}
		
		return $this->ret;
	}

	function decrypt($string)
	{
		$this->data['char'] = array();
		$this->data['length'] = strlen($string);
		$this->data['dlength'] = 0;
		
		$this->data['decypher'] = array();
		$this->data['decypher']['char'] = array();
		$this->data['decypher']['string'] = false;
		
		for($i = 0; $i < $this->data['length']; $i += 4)
		{
			$this->data['char'][] = $string{$i}   ^ '&';
			$this->data['char'][] = $string{$i+1} ^ '%';
			$this->data['char'][] = $string{$i+2} ^ '$';
			$this->data['char'][] = $string{$i+3} ^ '?';
		}
		
		for($i = 0; $i < $this->data['length']; $i += 4)
		{
			$s1 = ord( $this->data['char'][$i]   );
			$s2 = ord( $this->data['char'][$i+1] );
			$s3 = ord( $this->data['char'][$i+2] );
			$s4 = ord( $this->data['char'][$i+3] );
			
			$s1 = ( $s1 < 10 ) ? ( '0' . $s1 ) : $s1;
			$s2 = ( $s2 < 10 ) ? ( '0' . $s2 ) : $s2;
			$s3 = ( $s3 < 10 ) ? ( '0' . $s3 ) : $s3;
			$s4 = ( $s4 < 10 ) ? ( '0' . $s4 ) : $s4;			
			
			$this->data['decypher']['char'][] = $s1 . $s2 . $s3 . $s4 ;
		}
		
		$this->data['dlength'] = count($this->data['decypher']['char']);
		$this->data['char'] = array();
		
		for($i = 0; $i < $this->data['dlength']; $i++)
		{
			$L = $this->data['decypher']['char'][$i];
			$R = $this->data['decypher']['char'][$this->data['dlength'] - $i - 1];
			
			for($j = 0; $j < 300; $j++)
			{
				$L ^= $this->K[$j];
				$R ^= $this->K[$j];
			}
			
			$this->data['char'][$i] = $L;
			$this->data['char'][$this->data['dlength'] - $i - 1] = $R;
		}
		
		$this->ret = false;
		for($i = 0; $i < $this->data['dlength']; $i++)
			$this->ret .= chr ( base_convert( $this->data['char'][$i], 8, 10) );
		
		return $this->ret;
	}
}

$dKe = new Cypher_dKe();

?>

Conclusion :


Le code n'est pas commenté ... désolé pour celà

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.