crypt.php 1.33 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
<?php
/**
 * Classe de cryptage réversible de données
 * 
 * Cette classe permet de coder ou décoder une chaïne
 * de caractères
 * 
 * @author CrazyCat <crazycat@c-p-f.org>
 * @copyright 2007 http://www.g33k-zone.org
 * @package Mephisto
 */
 
class RevCrypt {
 
	/**
	 * Clé utilisée pour générer le cryptage
	 * @var string
	 */
	public $key;
 
	/**
	 * Données à crypter
	 * @var string
	 */
	public $data;
 
	/**
	 * Constructeur de l'objet
	 * @param string $key Clé utilisée pour générer l'encodage
	 */
	public function __construct($key) {
		$this->key = sha1($key);
	}
 
	/**
	 * Encodeur de chaîne
	 * @param string $string Chaîne à coder
	 * @return string Chaîne codée
	 */
	public function code($string) {
		$this->data = '';
		for ($i = 0; $i<strlen($string); $i++) {
			$kc = substr($this->key, ($i%strlen($this->key)) - 1, 1);
			$this->data .= chr(ord($string{$i})+ord($kc));
		}
		$this->data = base64_encode($this->data);
		return $this->data;
	}
 
	/**
	 * Décodeur de Chaîne
	 * @param string $string Chaîne à décoder
	 * @return string
	 */
	public function decode($string) {
		$this->data = '';
		$string = base64_decode($string);
		for ($i = 0; $i<strlen($string); $i++) {
			$kc = substr($this->key, ($i%strlen($this->key)) - 1, 1);
			$this->data .= chr(ord($string{$i})-ord($kc));
		}
		return $this->data;
	}
}
?>